#!/usr/bin/perl

$II2PKGDIR = $ENV{"II2PKGDIR"};
$II2INSTALLDIR = $ENV{"II2INSTALLDIR"};
$II2RESOURCES = $ENV{"II2RESOURCES"};
$II2DOSERVER = $ENV{"II2DOSERVER"};
$II2BUILDSTAMP = $ENV{"II2BUILDSTAMP"};

$TEXIPKGTEXLIVE = $ENV{"TEXIPKGTEXLIVE"};

$PROGNAME = `"${II2RESOURCES}/basename" "$0"`;
chomp( $PROGNAME);

if (${II2BUILDSTAMP} < 1167840304) {
    warn "### You need at least i-Installer 2.87 for this package. Bailing out...\n";
    exit 1;
}

my $arch = `uname -p`; chomp $arch;

print "### ${PROGNAME}: Started...\n";

# Load shared stuff
require "${II2PKGDIR}/texlive.sharedscript.pm";

checkTeXEnvironmentVariables();

migrateifnecessary();

setConfigUser();

# Add our installed TeX to the beginning of the PATH so it will always
# be found first. It would be better to add it to the end and check
# with which that we have the right one. Too much work for now.
$ENV{"PATH"} = "${II2INSTALLDIR}/bin/${arch}-apple-darwin-current:$ENV{'PATH'}";

print "### ${PROGNAME}: PATH = \"$ENV{'PATH'}\"\n" if $II2LOGLEVEL > 3;

print "### ${PROGNAME}: TeX Live: ${TEXIPKGTEXLIVE}\n";

# Set that contains the set selected during any previous selector phase. This is received
# from i-Installer:
my %set = getSetInfo(); # sets %set

if (not -x "/usr/bin/grep") {
    print "### ${PROGNAME}: TeX needs commands from the BSD subsystem to run. The BSD subsystem seems to be missing on this system. Bailing out...\n";
    exit( 1);
}

if (${II2INSTALLDIR} eq "") {
    ipkgwarn( "Your environment is damaged, your Mac OS X installation or your user account is probably damaged. Try running a disk repair program or install from another user's account. Bailing out...\n");
    exit( 1);
}

if ( not -w "${II2INSTALLDIR}" or ! -d "${II2INSTALLDIR}") {
    ipkgwarn( "Cannot write in directory ${II2INSTALLDIR}. Bailing out...\n");
    exit( 1);
}

`"${II2RESOURCES}/doalerter" -s "${II2DOSERVER}" -V -t IncrementProgress -p 10000`;

if (-d "${II2INSTALLDIR}/texmf.texlive") {
    print "### ${PROGNAME}: You are using my TeX directory layout. Good.\n";

    my $uninstalledSetString = "";

    if ($set{'tl2006-bin-i386'}{'selected'} eq 'YES' or
	$set{'tl2006-bin-powerpc'}{'selected'} eq 'YES') {
	&removetexprogramessentials();
	$uninstalledSetString .= "tl2006-bin-powerpc|tl2006-bin-i386|";
    }

    `"${II2RESOURCES}/doalerter" -s "${II2DOSERVER}" -V -t IncrementProgress -p 20000`;

    if ($set{'tl2006-texmfe'}{'selected'} eq 'YES') {
	&removetexfoundation();
	$uninstalledSetString .= "tl2006-texmfe|";
	if (not $set{'tl2006-texmfdoc'}{'selected'} eq 'YES') {
	    warn "### ${PROGNAME}: TeX documentation removed automatically because its parent, the TeX texmf tree has been removed.\n";
	    $uninstalledSetString .= "tl2006-texmfdoc|";
	}
    }
    elsif ($set{'tl2006-texmfdoc'}{'selected'} eq 'YES') {
	&removetexfoundationdocs();
	$uninstalledSetString .= "tl2006-texmfdoc|";
    }

    `"${II2RESOURCES}/doalerter" -s "${II2DOSERVER}" -V -t IncrementProgress -p 120000`;

    if ($set{'tl2006-gwtex'}{'selected'} eq 'YES' or
	$set{'tl2006-gwtexdoc'}{'selected'} eq 'YES') {
	&removegwtexfoundation();
	$uninstalledSetString .= "tl2006-gwtex|tl2006-gwtexdoc|";
    }

    `"${II2RESOURCES}/doalerter" -s "${II2DOSERVER}" -V -t IncrementProgress -p 30000`;

    if ($set{'tl2006-lib-universal'}{'selected'} eq 'YES') {
	&removetexprogramextras();
	$uninstalledSetString .= "tl2006-lib-universal|";
    }

    `"${II2RESOURCES}/doalerter" -s "${II2DOSERVER}" -V -t IncrementProgress -p 10000`;

    if ($uninstalledSetString ne '') {
	# UnsetInstalled also clears configured state
	chop( $uninstalledSetString) if $uninstalledSetString =~ /\|$/; 
	`"${II2RESOURCES}/doalerter" -s "${II2DOSERVER}" -V -t UnsetInstalledSelectorSets -P "$uninstalledSetString"`;
    }
}
else {
    if (-d "${II2INSTALLDIR}/texmf" or -d "${II2INSTALLDIR}/texmf") {
	ipkgwarn( "Unfamiliar TeX directory layout. No removal before unarchiving. This may cause problems when you use TeX.\n");
	$doresult = `"${II2RESOURCES}/doalerter" -s "${II2DOSERVER}" -V -t Alert -T "Unknown layout!" -I "Your TeX layout is not recognized. No parts will be removed before new parts are installed.\n\nThis may lead to doubles and an unreliable setup." -1 "Continue"`;
    }
    `"${II2RESOURCES}/doalerter" -s "${II2DOSERVER}" -V -t IncrementProgress -p 180000`;
}

`"${II2RESOURCES}/doalerter" -s "${II2DOSERVER}" -V -t IncrementProgress -p 10000`;

warn "### ${PROGNAME}: Finished.\n";
print "### ${PROGNAME}: Finished.\n";

# $Id: tex.prepare 71 2006-10-09 19:46:02Z gctwnl $
