#!/usr/bin/perl -w
# Scriptwise Installer by Mitch
# June, 2005

use strict;
use Net::FTP;

system 'cls';
system 'title Scriptwise Installer by Mitch © 2005';

print 'This will install Scriptwise, continue?';
my $an = input('yes');
exit if ($an =~ m/^no$/i);

print 'Where should I install the application?';
my $installDir = input('C:\Scriptwise');

print 'Do you want me to download the source file aswell?';
my $getSource = input('no');

message('Connecting to download server');
my $ftpSession = Net::FTP->new('mitch.ocean-hosting.com')
  || error('Can\'t connect to download server: '.$!);

$ftpSession->login('anonymous@mitch.ocean-hosting.com', 'billgates@microsoft.com')
  || error('Can\'t login: '.$ftpSession->message());

unless (-d './tmp/')
{
  mkdir('./tmp/') || error('Can\'t create tmp directory: '.$!);
}

$ftpSession->binary();

message('Downloading setup');
$ftpSession->get('setup', './tmp/setup')
  || error($ftpSession->message());

message('Generating download list');
my (%downloadList, $customMessage);
if (open SETUP, './tmp/setup')
{
  my ($downloadDir, $lineCount);
  while (<SETUP>)
  {
    $lineCount++;
    if (m/^#/ || m/^\s+$/)
    {
      next;
    }
    elsif (m/^\[(.+)\]$/) {
      $downloadDir = $1;
    }
    elsif (m/^\?(.+)$/) {
      $customMessage = $1;
    }
    elsif (m/^(.+)$/) {
      push @{$downloadList{$downloadDir}}, $1;
    }
    else {
      message('Error found in setup, line '.$lineCount);
    }
  }
  close SETUP;
  if ($getSource =~ m/^yes$/i)
  {
    @{$downloadList{'/source'}} = qw(scriptwise_source.pl install_source.pl);
  }
}
else {
  error('Can\'t open setup file for input: '.$!);
}

$installDir =~ s[\\][/]g;
$installDir .= '/' unless ($installDir =~ m[/$]);
if (! -d $installDir)
{
  print ("\n".'Installation directory does not exist, create it?');
  my $a = input('yes');
  if ($a =~ m/^no$/i)
  {
    error('Exiting installer');
  }
  else {
    mkdir($installDir) || error('Can\'t create installation directory: '.$!);
  }
}

foreach my $currentDir (keys %downloadList)
{
  mkdir $installDir.$currentDir unless -d $installDir.$currentDir;
  foreach my $currentFile (@{$downloadList{$currentDir}})
  {
    message('Downloading '.$currentFile);
    $ftpSession->get($currentFile, $installDir.$currentDir.'/'.$currentFile)
      || message('Can\'t get '.$currentFile);
  }
}

message('Closing connection');
$ftpSession->quit();

message('Removing temporary files');
unlink './tmp/setup' || warn 'Can\'t remove setup';
rmdir './tmp' || warn 'Can\t remove tmp directory';

message("\n".$customMessage) if (defined $customMessage);
sleep 2;

$installDir =~ s[/][\\]g;
message("\n".'Installation successfully completed');
message('Scriptwise is located in '.$installDir.'Scriptwise.exe');
sleep 2;

print "\n".'Installation complete, see what\'s new?';
$an = input('yes');
if ($an =~ m/^yes$/i)
{
  exec 'notepad '.$installDir.'New.txt';
}

print "\nHit enter to exit";
<STDIN>;

sub message
{
  my $message = shift;
  print $message."\n";
}

sub input
{
  my $default = shift;
  print ' ['.$default.'] '."\n".'> ';
  my $a = <STDIN>;
  chomp($a);
  $a = $default if ($a eq '');
  print "\n";
  return $a;
}

sub error
{
  my $error = shift;
  message('Error: '.$error);
  print 'Hit enter to exit';
  <STDIN>;
  exit;
}