#!/usr/bin/perl

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

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

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

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

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

require "${II2PKGDIR}/texpkgs.sharedscript.pm";

checkPrerequisites( "ConTeXt");

if (not $ENV{'TEXIPKGUSDEPRECATED'} eq 'YES') {
    if (${II2INSTALLDIR} =~ m|^/usr/local/teTeX|) {
	my $msg = "You are now configuring in the location for de deprecated gwTeX 2003-2005 i-Package in part based on teTeX. This is probably not what you want. I suggest you make sure you have the gwTeX i-Package based on TeX Live installed. Then the best thing is to reset this i-Package to its default package properties. It will then pick up the new default location.";
	$doresult = `"${II2RESOURCES}/doalerter" -s "${II2DOSERVER}" -V -t Alert -T "Ar you sure you want to install in this location?" -I \"${msg}\" -1 "Yes" -2 "No"`;
	chomp( $doresult);
	if (($? >> 8) != 0) {
	    print "i-Package process aborted at user request\n";
	    exit 0;
	}
	`"${II2RESOURCES}/doalerter" -s "${II2DOSERVER}" -V -t Environment -E "TEXIPKGUSEDEPRECATED=YES"`;
    }
    if (${II2INSTALLDIR} =~ m|^/usr/local/TeXLive|) {
	my $msg = "You are now configuring in the old location for gwTeX based on TeX Live. This is probably not what you want. I suggest you make sure your TeX i-Package has migrated to /usr/local/gwTeX, then the best thing is to reset this i-Package to its default package properties. It will then pick up the new default location.";
	$doresult = `"${II2RESOURCES}/doalerter" -s "${II2DOSERVER}" -V -t Alert -T "Ar you sure you want to install in this location?" -I \"${msg}\" -1 "Yes" -2 "No"`;
	chomp( $doresult);
	if (($? >> 8) != 0) {
	    print "i-Package process aborted at user request\n";
	    exit 0;
	}
	`"${II2RESOURCES}/doalerter" -s "${II2DOSERVER}" -V -t Environment -E "TEXIPKGUSEDEPRECATED=YES"`;
    }
}

print "### ${PROGNAME}: Checking availability of texmfstart...\n";
if (not -x "${texbindir}/texmfstart") {
    print "### ${PROGNAME}: No texmfstart, installing replacement...\n";
    ipkgsystem( "cp \"${II2PKGDIR}/texmfstart\" \"${texbindir}/\"");
}

print "### ${PROGNAME}: Preparing TeX for ConTeXt configuration...\n";

if (not -d "${II2INSTALLDIR}/tex/context") {
    ipkgwarn( "### ${PROGNAME}: No ConTeXt found. Configuration cannot proceed. Bailing out..\n");
    exit( 1);
}

my ${systexmfdir} = ${texmftldir};
my ${systexmfdir} = ${texmftedir} if ${systexmfdir} eq '';

if (-d "${systemfdir}/tex/generic/context" and
    -d "${II2INSTALLDIR}/tex/generic/context") {
    # Recent versions use \edef
    my $localversion = `grep '\\\\e\\?def\\\\contextversion' "${II2INSTALLDIR}/tex/context/base/context.tex"`;
    chomp( $localversion);
    my $systexversion = `grep '\\\\e\\?def\\\\contextversion' "${systexmfdir}/tex/context/base/context.tex"`;
    chomp( $systexversion);
    $localversion =~ s/^.*\\e?def\\contextversion\{//;
    $localversion =~ s/\}.*$//;
    $systexversion =~ s/^.*\\e?def\\contextversion\{//;
    $systexversion =~ s/\}.*$//;

    $doresult = `${kpsewhichcmd} context.tex`;
    chomp( $doresult);
    if ($doresult eq "${INSTALLDIR}/tex/context/base/context.tex") {
	my $contextdir = $doresult;
	$contextdir =~ s/\/tex\/context\/base\/context\.tex$//;
	`"${II2RESOURCES}/doalerter" -s "${II2DOSERVER}" -V -t Alert -T "Nonfunctional ConTeXt Install." -I "You have just installed a ConTeXt installation ${localversion} in ${II2INSTALLDIR}. Sadly, this is nonfunctional for your TeX setup as another one in ${contextdir} will be found first." -1 "Accept"`;
    }
    else {
	`"${II2RESOURCES}/doalerter" -s "${II2DOSERVER}" -V -t Alert -T "Reporting ConTeXt versions." -I "You have just installed a ConTeXt installation $localversion in ${II2INSTALLDIR}. The version in the ${systexmfdir} is ${systexversion}.\n\nThe one you have just installed is the one that will be used." -1 "OK"`;
    }
}

foreach my $ctxfmt (('cont-en', 'cont-nl', 'cont-cz', 'cont-de', 'cont-uk', 'cont-it', 'cont-ro', 'cont-fr', 'mptopdf', 'metafun')) {
    ipkgsystem( "${fmtutilcmd} --disablefmt $ctxfmt </dev/null") == 0
	or warn "### ${PROGNAME}: failed to disable ConTeXt ($ctxfmt) in fmtutil\n";
}

my $texmfvardir;
if (defined $configforuser and $configforuser ne 'root') {
    $texmfvardir = `kpsewhich -var-value=TEXMFVAR`;
    if (not defined $texmfvardir) {
	$texmfvardir = `kpsewhich -var-value=TEXMFLOCAL`;
    }
    chomp( $texmfvardir);
    ipkgsystem( "/usr/bin/env TEXFORMATS=\"${texmfvardir}/web2c\" texexec --xetex --make --all --fast en</dev/null");
    ipkgsystem( "/usr/bin/env TEXFORMATS=\"${texmfvardir}/web2c\" texexec --xetex --make --all --fast nl</dev/null");
    ipkgsystem( "/usr/bin/env TEXFORMATS=\"${texmfvardir}/web2c\" texexec --make --all --fast</dev/null");
}
else {
    my $sudostring = "sudo -H -u root";
    $texmfvardir = `${sudostring} kpsewhich -var-value=TEXMFSYSVAR`;
    if (not defined $texmfvardir) {
	$texmfvardir = `kpsewhich -var-value=TEXMFLOCAL`;
    }
    chomp( $texmfvardir);
    ipkgsystem( "${sudostring} /usr/bin/env TEXFORMATS=\"${texmfvardir}/web2c\" texexec --xetex --make --all --fast en</dev/null");
    ipkgsystem( "${sudostring} /usr/bin/env TEXFORMATS=\"${texmfvardir}/web2c\" texexec --xetex --make --all --fast nl</dev/null");
    ipkgsystem( "${sudostring} /usr/bin/env TEXFORMATS=\"${texmfvardir}/web2c\" texexec --make --all --fast</dev/null");
}

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

exit( 0);

# $Id: context-update.configure 106 2006-10-31 21:00:31Z gctwnl $

