#!/usr/bin/perl

# $Id: install-test,v 1.5 1997/07/07 00:38:59 JCL Exp $
# LaTeX2HTML Version 96.2 : install_test

$dd = '/';  # directory delimiter;

# This perl script substitutes the variable $LATEX2HTMLDIR in the main
# latex2html script with the actual path to the latex2html distribution 
# files. It then tests for the availability of the external support programs.

# To do the installation manually:
# (1) Edit the file latex2html.config to set the correct pathnames where 
#     necessary
# (2) Put the path to the latex2html source files in the variable 
#     $LATEX2HTMLDIR in the main latex2html script 
#     OR
#     set an environment variable LATEX2HTMLDIR to point to the latex2html 
#     source files.
 
# 
# Uncomment the following statement if your Linux system requires it.
# use GDBM_File;


print<<"#EOM";

This is install-test for LaTeX2HTML V97.1
=========================================

#EOM




# first, assure this
open(TMP,">TEST.TMP") ||
    die "I need a writeable directory: open TEST.TMP failed: $!\n";
close TMP;
unlink "TEST.TMP";

# Read latex2html.config to get the value of $LATEX2HTML
&read_config_file;

if ($LATEX2HTMLDIR eq "/user" || !$LATEX2HTMLDIR) {
    print <<"#EOM";
Your LaTeX2HTML release needs a directory for installation.
This is defined in LATEX2HTMLDIR in latex2html.config, and will be used to find
perl modules, LaTeX styles, programs, etc.
Before we set this, make sure that everything of LaTeX2HTML is there and you
are in that directory.
The latex2html script itself must not reside here, you can place it elsewhere
after install-test succeeded.
#EOM
    print "Continue?\n";
    <STDIN>;

    local($pwd) = `pwd`; chop $pwd;
    print "What is the LaTeX2HTML directory [$pwd]: ";
    $_ = <STDIN>; chop;
    if (/(\S+)/) {
	$LATEX2HTMLDIR = $1;
    }
    else {
	$LATEX2HTMLDIR = $pwd;
    }
    print "\$LATEX2HTMLDIR will be \"$LATEX2HTMLDIR\"\n";

    print "changing LaTeX2HTML directory in latex2html.config was " .
	(&substitute_dir_var("latex2html.config") ? "" : "not ") .
	    "successful.\n";
}


# Substitute the variable $LATEX2HTMLDIR
print "\nMain script installation was " .
    (&substitute_dir_var("latex2html") ? "" : "not ") .
    "successful.\n";


print "Testing availability of external programs...\n";
&test_external_programs;


sub read_config_file {
    &deal_with_forward_references;
    require("latex2html.config") if
	((-f "latex2html.config") ||
	 die "Could not find file latex2html.config\n");
}

# Changes lines of the form:
# $LATEX2HTMLDIR = <SOMETHING> 
# to
# $LATEX2HTMLDIR = <current value of $LATEX2HTMLDIR> 
sub substitute_dir_var {
    local($file) = @_;
    local($SUCCESS) = 0;

    if ( (-f "$LATEX2HTMLDIR${dd}$file") ||
	die "\nCannot find $LATEX2HTMLDIR${dd}$file\nPlease check the value of \$LATEX2HTMLDIR in latex2html.config\n") {
	open(IN, "<$LATEX2HTMLDIR${dd}$file")
	    || die "Cannot open $LATEX2HTMLDIR${dd}$file";
	rename("$LATEX2HTMLDIR${dd}$file","$LATEX2HTMLDIR${dd}$file.bak");
	open(OUT, ">$LATEX2HTMLDIR${dd}$file")
	    || die "Cannot open $LATEX2HTMLDIR${dd}$file";
	chmod(0755, "$LATEX2HTMLDIR${dd}$file")
	    unless $file =~ /.config$/;
	while (<IN>) {
	    s/\$LATEX2HTMLDIR\s*=.*$/
		do {$SUCCESS = 1;
		    "\$LATEX2HTMLDIR='$LATEX2HTMLDIR';" .
			"# Inserted by installation script"}/eo;
	    print OUT;
	}
	close IN;
	close OUT;
    }
    $SUCCESS;
}


sub deal_with_forward_references {
    foreach ("ignore_commands", "process_commands_in_tex") {
	eval "sub $_ {}"}
}

sub test_external_programs {
    local ($nolatex2html, $version, $revision, $nopic, $nojust, $tmp) = (0,0);
    # Perl
    $ver = $] * 1.0;
    $vers = sprintf("%4.3f", $ver);
    if ($ver < 4.036) {
	print "\n*** ERROR: You are using Perl version $vers\n".
	    "\nThis may cause problems." .
		"Please upgrade to version 4 at patch level 36\n";}
    else {print "Perl version $vers is OK. \n"}

    print "\n*** ERROR: Will not be able to run latex2html: ".
	"Cannot find texexpand.\n"
	    if ($nolatex2html = &look_for($TEXEXPAND, "exec", "texexpand"));

    print "Checking for availability of DBM or NDBM (Unix DataBase Management)...\n";
    if ((! eval 'dbmopen(%array, "DBM.tst",0755)') && $@) {
        print "*** ERROR: $@ \nYou will not be able to use latex2html.\n";
    } else {
#JCL: we do not need to write sth into it, if Perl lacks DBM we
# detected it with dbmopen before...
	dbmclose(%array);
	print "DBM was found.\n";
    }
    unlink ('DBM.tst.dir', 'DBM.tst.pag');

    print "Checking if globbing works... ";
    open(TMP,">TEST1.TMP"); close TMP;
    open(TMP,">TEST2.TMP"); close TMP;
    local(@list) = (<TEST*.TMP>);
    if ($#list == 1) {
	print "globbing is ok.\n";
    }
    else {
	print "globbing failed.\n";
	print "LaTeX2HTML will *not* work.\n";
	print "checking what's wrong...\n";
	# Perl uses full_csh to perform globbing, this may change of course
	local($csh) = eval('require "Config.pm"; &Config::FETCH("","full_csh")');
	if ($csh) {
	    print "your csh is $csh\n";
	    if (! -f $csh) {
		print "$csh does not exist!\n";
		print "Find the csh or tcsh on your system and link it to $csh.\n";
		die "Then restart install-test.\n";
	    }
	    elsif (! -x $csh) {
		die "$csh is not executable!\n";
	    }
	    else {
		print "$csh exists and is executable... I can't help here.\n";
		print "Maybe you should continue and check LaTeX2HTML by translating the manual.\n";
		print "Continue?\n";
		<STDIN>;
	    }
	}
	else {
	    print "I can't help with that Perl version.\n";
	    print "You need more detailed advise on what went wrong here.\n";
	    print "Continue?\n";
	    <STDIN>;
	}
    }
    unlink("TEST1.TMP","TEST2.TMP");

    $version=undef;
    $_ = &get_first_line_of_stderr("$DVIPS -f DUMMY");
    s/dvipsk?\s*([\d\.]+)/$version = $1;/eo;
    $patch = $version;
    if ($patch && ($patch < 5.516)) {
	$nopic = 1;
	print "\n*** Warning: You are using DVIPS $version.".
	    "\nThis may cause problems when generating inlined images" .
		"\nif your DVIPS does not support the command line options".
		    "\n-S, -i and -o".
			"\nSee the manual for more details.\n";}
    elsif (! $patch) {
        print "Warning:  I was not able to find DVIPS. ".
	    "\nPlease make sure that your version of DVIPS".
		"\nsupports the command line options -S, -i and -o.".
		    "\nVersion 5.516 or higher is OK.".
			"\nI was unable to verify your version.\n";}
    else {
	print "DVIPS version $version is OK.\n"}

    $nopic = &look_for($PSTOIMG,"util", "pstoimg");

# Substitute the variable $LATEX2HTMLDIR in pstoimg
    if (!$nopic) {
	print "Setting up pstoimg script...",
	    (&substitute_dir_var("pstoimg") ? "succeeded\n" : "\n*failed*\n");
	print "Setting up configure-pstoimg script...",
	    (&substitute_dir_var("configure-pstoimg") ? "succeeded\n" : "\n*failed*\n");
    }

    $tmp = &look_for($LATEX, "util", $LATEX);
    $nopic = $tmp unless $nopic;

    print "Warning: May not be able to convert equations, figures, tables ".
	"\nand unknown environments to inlined images.\n" if $nopic;

    print "Warning: Will not be able to deal with some style files.\n"
	if &look_for($LATEX2HTMLSTYLES, "dir", "Styles directory");

    print "\nMain set-up done.\n";

    unless ($nopic) {
	print "You may complete this set-up by configuring pstoimg now...proceed?\n";
	<STDIN>;
	require "configure-pstoimg";
    }
}

sub look_for {
    local($what,$type, $name) = @_;
    local($fail,$_);
    if (($type eq "exec") && (! -x $what)) {
	print "Making $what executable...\n";
	chmod 0755, $what;
	if (! -x $what) {
	    $fail = "Error: Could not make $what executable.\n";
	}
    }
    elsif (($type eq "util") && (! -f $what)) {
	print "Looking for $name...\n";
	$_ = `sh -c "type  $what"`;
	chop;
	if ((/no /) || (/not found/) || (/^\s*$/)) {
	    $fail = "Warning: $name was * NOT * found.\n";}
	else {
	    $what = $_;}
    }
    elsif (($type eq "dir") && (! -d $what)) {
	$fail = "Error: directory $name was * NOT * found.\n";
    }
    ($fail ? print $fail : print "$name was found.\n");
    $fail;
}	

# Page 164 of the Camel book.     
sub get_first_line_of_stderr {
    local($prog) = @_[0];
    open(SO, ">&STDOUT");
    open(SE, ">&STDERR");

    open(STDOUT, ">${dd}tmp${dd}foo$$");
    open(STDERR,">&STDOUT");

    select(STDERR); $| = 1;
    select(STDOUT); $| = 1;

    `$prog`;

    close(STDOUT);
    close(STDERR);

    open(STDOUT, ">&SO");
    open(STDERR, ">&SE");

    open(FOO,"<${dd}tmp${dd}foo$$");
    $_ = <FOO> ;
    close(FOO);
    $_;
}


