#!/usr/bin/perl
###########################################################################
# This script is used to install Marathon Infinity Scenarios, such as
# Evil and Tempus Irae.
#
# It relies on data stored in the install_data file.
#
# Feel free to modify, improve this script.
#
# Written by Craig N. Caroon, Sep 13, 2000
#   Modified: Oct 5 2000
###########################################################################
use strict;

my %config;

# read config file
open IN, "install_data" or die "Could not open install data file: $!";
while (<IN>)
{
    chomp;
    my ($name,$value) = split /=/;

    #strip trailing whitespace
    $name =~ s/\s*$//g;

    #strip leading whitespace
    $value =~ s/^\s*//g;
    #strip double quotes
    $value =~ s/"//g;

    $config{$name} = $value;
}
close IN;

# where is their Aleph One Data dir
#print "Where is your Aleph One Data dir [$config{def_a1_data_dir}]: ";
#my $ask_a1_data_dir = <STDIN>;
#chop ($ask_a1_data_dir);
#my $a1_data_dir = $ask_a1_data_dir || $config{def_a1_data_dir};
#
#if (! -d $a1_data_dir)
#{
#    die "The Aleph One Data directory you specified, [$a1_data_dir], does not exist.\n";
#}

#print "\n\n";

# where do they want to install
print "Where would you like to install $config{name} [$config{def_install_dir}]: ";
my $ask_install_dir = <STDIN>;
chop ($ask_install_dir);
my $install_dir = $ask_install_dir || $config{def_install_dir};

if (-e $install_dir)
{
    print "Install Directory already exists, not creating\n";
}
else
{   
    print "Creating $install_dir...\n";
    mkdir $install_dir or die "Error creating Install Directory: $!";
}


# create symlinks
#symlink "$a1_data_dir/Fonts", "$install_dir/Fonts";
#symlink "$a1_data_dir/Themes", "$install_dir/Themes";
#
#if (-e "$a1_data_dir/Resources")
#{
#    symlink "$a1_data_dir/Resources", "$install_dir/Resources";
#}
#
#if (-e "$a1_data_dir/Pfhortran_Language_Definition")
#{
#    symlink "$a1_data_dir/Pfhortran_Language_Definition", "$install_dir/Pfhortran_Language_Definition";
#}


#create startup script
print "Creating startup script...\n";

open OUT, ">$install_dir/$config{startup_script}" or die "Error opening $install_dir/$config{startup_script} for writing: $!";
print OUT "#!/bin/sh\n";
print OUT "export ALEPHONE_DATA=\$ALEPHONE_DATA:$install_dir\n";
print OUT "/usr/local/bin/alephone \$*\n";
close OUT;
chmod 0755, "$install_dir/$config{startup_script}";


# unpack tarball
print "Unpacking data...\n";
system "cp ./$config{data} $install_dir";
chdir $install_dir;
system "tar xvfz $config{data}";
unlink "$install_dir/$config{data}";


#tell 'em all about it.
print <<EOF;


$config{name} was sucessfully installed!
For more info. about $config{name}, visit $config{creator_url}.

To start $config{name}, run $install_dir/$config{startup_script}.
$config{startup_script} takes the same command-line options as the alephone
application.

Things you should do:
0.  Make sure your ALEPHONE_DATA environment variable is set to
    the directory that contains the Aleph One base data files.
    Normally something like /usr/local/share/AlephOne.
1.  Start $config{name}, and set your preferences.
2.  Tell your friends.
EOF

