#!/usr/bin/perl -w

$user = $ENV{USER};
$id = $ENV{ID};
$url = "$ENV{PROTO}://$ENV{HTTP_HOST}";
$fi = 'fexitinstaller.cmd';
$fe = 'http://fex.belwue.de/download/fexit.exe';
$ps = '%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe';
$cmd = <<EOD;
mkdir "%USERPROFILE%\\fex"
cd "%USERPROFILE%\\fex"
if not exist id (
  echo $url>id
  echo $user>>id
  echo $id>>id
)
cd "%USERPROFILE%\\Desktop"
$ps -command "& { (New-Object Net.WebClient).DownloadFile('$fe','fexit.exe') }"
\@explorer "%USERPROFILE%\\Desktop"
\@set /p x="press [ENTER]"
EOD
$cmd =~ s/\n/\r\n/g;

if (chdir "$spooldir/$user" and open $fi,'>',$fi) {
  print {$fi} $cmd;
  close $fi;
  system "$FEXHOME/bin/fexsend ".
         "-oKq -C 'fexit for your Windows desktop' $fi $user >/dev/null";
  if ($? == 0) {
    print "<p>\n";
    print "<h3>A fexit installer has been sent to you. Check your email.</h3>\n";
  }
  unlink $fi;
}
