#!/usr/bin/perl -w
use strict;
my @defaultres=(1024,768);
my $normx=1024;
my $normy=768;
my $juximg="juxlalabg.png";
my $juxscale="/home/knoppix/.juxscale.dontuse";


my $itarget='/home/knoppix/.idesktop';
my $idir='/etc/skel/.idesktop';

my %doscale=('Width' => 'x' , 'X' => 'x', 'Height' => 'y' , 'Y' => 'y') ;

my $pid=fork() ;
if ($pid == 0) {
  exec("/usr/share/juxlala/scripts/splash.sh -Z /usr/local/lib/knoppix.jpg /bin/true");
}


sub getres {
  my @res=(0,0);
  open(W,"xwininfo -root|") or return(@defaultres); 
  while(<W>) {
    chomp;
    if (/Width: (\d+)/) {
      $res[0]=$1;
    } elsif (/Height: (\d+)/) {
      $res[1]=$1;
    }
  }
  close(W);
  return @res;
}

(my $wd,my $hg)=getres();
my %scalefactor=();
$scalefactor{'y'}=1.0 * $hg/$normy ;
$scalefactor{'x'}=1.0 * $wd/$normx ;

if (-e $juxscale) {
  open(J,"<$juxscale");
  while(<J>) {
    if ( /YSCALE=(\d+)/ ) {
      $scalefactor{'y'}= $1 / 100.0;
    }
    if ( /XSCALE=(\d+)/ ) {
      $scalefactor{'x'}= $1 / 100.0;
    }
  }
  close(J);
  print "using $juxscale\n";
}


foreach my $sf (keys %scalefactor) {
  print "factor $sf = ",$scalefactor{$sf},"\n";
}

print "res=$wd x $hg\n";
opendir(IDIR,$idir) or die "can not read $idir";
my @ilnks = grep { /\.lnk$/ && -f "$idir/$_" } readdir(IDIR);
closedir IDIR;

foreach my $ilnk (@ilnks) {
  my $file="";
  print "rescaling from $idir/$ilnk to $itarget/$ilnk\n";
  open(L,"<$idir/$ilnk") or die "can not read $ilnk";
  while(<L>) {
    my $line=$_;
    if (/^(\s*)([A-Za-z]+)\:(\s*)(\d*)\s*$/) {
      my $ds=$doscale{$2};
      if (defined $ds) {
        my $fac=$scalefactor{$ds};
        my $val=int(0.5 + $4 * $fac);
        # print "got $2 is $4 scaled to $val\n";
        $file .= $1 . $2  . ':' . $3 . $val . "\n";
      } else {
        $file .= $line;
      } 
    } else {
      $file .= $line;
    }
    open(T,">$itarget/$ilnk") or die "can not write to $itarget/$ilnk";
    print T $file;
    close(T);
  }  
  close(L);
}
exec('/usr/bin/idesk');



