# Load shared stuff
if (not -e "$ENV{'II2RESOURCES'}/ii2plfuncs.pm") {
    my $msg = "i-Installer is damaged. Perl support routines are missing.";
    warn "### ${PROGNAME}: ${msg}";
    if (exists $ENV{'II2DOSERVER'}) {
	`"$ENV{'II2RESOURCES'}/doalerter" -s "$ENV{'II2DOSERVER'}" -V -t Alert -T "i-Installer isd  damaged" -I "${msg}" -1 "Accept"`;
    }
    exit 1;
}
require "$ENV{'II2RESOURCES'}/ii2plfuncs.pm";

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

sub findTeXDirs
{
    # TODO not symlink safe:
    my $orgtestdir = shift;
    my $testdir = $orgtestdir;
    my $parentdir = `/usr/bin/dirname \"$testdir\"`; chomp $parentdir;
    while ($testdir ne $parentdir) {
	# Check for tex (the program) and kpsewhich to see if we have a
	# TeX Programs install there:
	if (-f "$parentdir/bin/${arch}-apple-darwin-current/tex" and
	    -f "$parentdir/bin/${arch}-apple-darwin-current/kpsewhich") {
	    if ($ENV{'II2LOGLEVEL'} > 3) {
		print "### ${PROGNAME}: texdir = $parentdir, texbindir = \"$parentdir/bin/${arch}-apple-darwin-current\".\n";
	    }
	    return ("$parentdir", "$parentdir/bin/${arch}-apple-darwin-current");
	}
	$testdir = $parentdir;
	$parentdir = `/usr/bin/dirname \"$testdir\"`; chomp $parentdir;
    }
    warn "### ${PROGNAME}: Problem: testdir = \"$orgtestdir\", texdir = \"$texdir\", texbindir = \"$texbindir\".\n";
    return ('', '');
}

sub setConfigUser
{
    if (-e "${texbindir}/texconfig-sys") {
	if ($> == 0) {
	    print "### ${PROGNAME}: System wide configuration, TL2005-style.\n";
	    ${configforuser} = 'root';
	    ${sudocmd} = "sudo -H -u ${configforuser}";
	}
	else {
	    print "### ${PROGNAME}: Configuring system-wide for user $ENV{'II2USER'}, TL2005-style.\n";
	    ${configforuser} = $ENV{'II2USER'};
	    ${sudocmd} = "";
	}
	${texconfigcmd} = "${sudocmd} texconfig-sys";
	${fmtutilcmd} = "${sudocmd} fmtutil-sys";
	${updmapcmd} = "${sudocmd} updmap-sys";
	${kpsewhichcmd} = "${sudocmd} kpsewhich";
	${mktexlsrcmd} = "${sudocmd} mktexlsr";
	${texmfvardir} = `${kpsewhichcmd} --expand-var=\'\$TEXMFSYSVAR\'`; chomp ${texmfvardir};
    }
    else {
	if ($> == 0) {
	    print "### ${PROGNAME}: System wide configuration, pre-TL2005-style.\n";
	    ${configforuser} = 'root';
	    ${sudocmd} = "sudo -H -u ${configforuser}";
	}
	else {
	    print "### ${PROGNAME}: Configuring system-wide for user $ENV{'II2USER'}, pre-TL2005-style.\n";
	    ${configforuser} = $ENV{'II2USER'};
	    ${sudocmd} = "";
	}
	${texconfigcmd} = "${sudocmd} texconfig";
	${fmtutilcmd} = "${sudocmd} fmtutil";
	${updmapcmd} = "${sudocmd} updmap";
	${kpsewhichcmd} = "${sudocmd} kpsewhich";
	${mktexlsrcmd} = "${sudocmd} mktexlsr";
	${texmfvardir} = `${kpsewhichcmd} --expand-var=\'\$TEXMFVAR\'`; chomp ${texmfvardir};
    }
}

sub checkPrerequisites
{
    my $name = shift;
    if ($ENV{'II2INSTALLDIR'} eq "") {
	ipkgwarn( "$ENV{'II2INSTALLDIR'} is empty. No TeX directory found.\nI cannot configure a nonexistent TeX. Bailing out gracefully...\n");
	exit( 0);
    }

    if (not -d "$ENV{'II2INSTALLDIR'}") {
	warn "### ${PROGNAME}: $ENV{'II2INSTALLDIR'} is not a directory. Bailing out...\n";
	exit( 1);
    }

    if (not $0 =~ /\.selector$/ and not -w "$ENV{'II2INSTALLDIR'}") {
	warn "### ${PROGNAME}: $ENV{'II2INSTALLDIR'} is not writable. This may lead to problems. Trying to proceed...\n";
    }

    (${texdir}, ${texbindir}) = findTeXDirs( $ENV{'II2INSTALLDIR'});

    if (${texdir} eq '' or ${texbindir} eq '') {
	ipkgwarn( "No TeX Programs found. Configuration cannot proceed.\n${name} configuration will be possible when both TeX Programs and TeX Foundation are available. Bailing out gracefully...\n");
	exit( 0);
    }

    $ENV{'PATH'} = "${texbindir}:$ENV{'PATH'}";

    setConfigUser();

    # Check is updmap is recent enough
    ipkgsystem( "${updmapcmd} --help </dev/null 2>&1 | grep listmaps >/dev/null");
    if (($? >> 8) != 0) {
	warn "### ${PROGNAME}: This TeX does not have a recent updmap. Bailing out...\n";
	exit( 1);
    }

    ${texmftedir} = `${kpsewhichcmd} --expand-var=\'\$TEXMFTE\'`; chomp ${texmftedir};
    ${texmftldir} = `${kpsewhichcmd} --expand-var=\'\$TEXMFTL\'`; chomp ${texmftldir};
    ${texmfgwdir} = `${kpsewhichcmd} --expand-var=\'\$TEXMFGW\'`; chomp ${texmfgwdir};
    ${texmfpkgsdir} = `${kpsewhichcmd} --expand-var=\'\$TEXMFPKGS\'`; chomp ${texmfpkgsdir};
    ${texmflocaldir} = `${kpsewhichcmd} --expand-var=\'\$TEXMFLOCAL\'`; chomp ${texmflocaldir};
    ${vartexfonts} = `${kpsewhichcmd} --expand-var=\'\$VARTEXFONTS\'`; chomp ${texmflocaldir};

    if (${II2LOGLEVEL} > 3) {
	print "### ${PROGNAME}: texmftedir = \"${texmftedir}\"\n";
	print "### ${PROGNAME}: texmftldir = \"${texmftldir}\"\n";
	print "### ${PROGNAME}: texmfgwdir = \"${texmfgwdir}\"\n";
	print "### ${PROGNAME}: texmfpkgsdir = \"${texmfpkgsdir}\"\n";
	print "### ${PROGNAME}: texmflocaldir = \"${texmflocaldir}\"\n";
    }

    if (not ((${texmftldir} ne '' and -d ${texmftldir}) or
	     (${texmftedir} ne '' and -d ${texmftedir}))) {
	ipkgwarn( warn "No TeX Foundation found (${texmftldir},${texmftedir}). Configuration cannot proceed.\n${name} configuration will be possible when both TeX Programs and TeX Foundation are available. Bailing out gracefully...\n");
	exit( 0);
    }

    if (${texmflocaldir} eq '' or not -d ${texmflocaldir} ) {
	ipkgwarn( "No local texmf tree (${texmflocaldir}) found. Configuration cannot proceed.\n${name} configuration will be possible when TeX has been configured first. Bailing out gracefully...\n");
	exit( 0);
    }

    if (not $0 =~ /\.selector$/ and not -w ${texmflocaldir}) {
	ipkgwarn( "${texmflocaldir} is not writable.\nI cannot change a nonwritable TeX. Bailing out...\n");
	exit( 1);
    }

    if ($0 =~ /\.configure$/ and $ENV{'II2USERACTION'} eq 'install') {
	print "### ${PROGNAME}: Files have possibly been added during install, the index needs updating. Please wait....\n";
	ipkgsystem( "${mktexlsrcmd} \"$ENV{'II2INSTALLDIR'}\"");
	if (($? >> 8) != 0) {
	    ipkgwarn( "Failed to run mktexlsr. Bailing out...\n");
	    exit( 1);
	}
    }
}

1;

# $Id$
