#!/usr/local2/bin/perl
#
#  takes the bibliographies $0 and print 
#	using style $1 (default = 'alpha')
#	making the title $2 (default = "Bibliography file $bib")
#
#  options:
#	-html root-html root-gif-or-text address	save as HTML file
#	-tex      	save TeX file and quit	
#	-ps     	make PostScript file and quit
#	-P printer      print to the PostScript printer
#	-check		runs bibtex on the full biblio to check it
#

$dvi2ps = "dvi2ps" ;

sub mysystem{
	($arg1) = @_ ;
	$ret = system($arg1) /256 ;
	if ( $ret != 0 ) {
		print STDERR "Bad call:  $arg1\n" ;
		exit( $ret );
	}
}

$printer = $ENV{'PRINTER'} ; 
$tex = 0 ;
$ps = 0 ;
$html = 0 ;
$check = 0 ;
if ( $#ARGV >= 0 ) {
    
    #    set the $printer from the first arg. or from the environment

    if ( $ARGV[0] =~ /^-/ ) {
	$left = $' ;
	if ( $left eq 'tex' ) {
		$tex = 1 ;
	        shift ;
		print STDERR "Leaving $bib.{tex,dvi} files.\n" ;
	} elsif ( $left eq 'html' ) {
		$html = 1 ;
	        shift ;
		$rh = $ARGV[0];  shift;
		$rg = $ARGV[0];  shift;
		$ad = $ARGV[0];  shift;
		print STDERR "Leaving $bib.html file.\n" ;
	} elsif ( $left eq 'check' ) {
		$check = 1 ;
	        shift ;
	} elsif ( $left eq 'ps' ) {
		$ps = 1 ;
	        shift ;
		print STDERR "Leaving $bib.ps file.\n" ;
	} elsif ( $left =~ /^P/ )  {
	    $printer = $' ;
	    shift ;
	    if ( length($printer) <= 0 ) {
		$printer = $ARGV[0] ;
		shift ; 
	    }
	} else {
	    print STDERR "Unrecognized option.\n" ;
	    exit(1) ;
        }
    } 
}
    #    set the bib list and styles from the remaining arguments
if ( $#ARGV >= 0 ) {
    $bib = $ARGV[0] ;
    $title = "Bibliography file $bib" ;
    if ( $#ARGV >= 1 ) {
	$style = $ARGV[1] ;
	if ( $#ARGV > 1 ) {
            $title = $ARGV[2] ;
	    if ( $#ARGV > 2 ) {
	        print STDERR "Too many command line arguments.\n" ;
	        exit(1) ;
	    }
	}
    } else { 
	$style = 'alpha' ;
    }
} else { 
	print STDERR "No biblio.\n" ;
	exit(1) ;
}

$date = `date '+%a %d-%h-%y %H:%M'` ;
open(TEXOUT,">$bib.tex") ;
print TEXOUT "\\documentstyle[proc,html]{article}\n" ;
print TEXOUT "\\begin{document}\n" ;
print TEXOUT "\\title{$title}\n" ;
print TEXOUT "\\author{Version of $date}" ;
print TEXOUT "\\maketitle" ;
print TEXOUT "\\nocite{*}\n" ;
print TEXOUT "\\bibliographystyle{$style}\n" ;
print TEXOUT "\\bibliography{$bib}\n" ;
print TEXOUT "\\end{document}\n" ;
close(TEXOUT) ;
&mysystem("latex $bib") ;
&mysystem("bibtex $bib") ;
if ( $check == 1 ) {  exit(0) ; }
&mysystem("latex $bib") ;
if ( $tex == 1 ) {  exit(0) ; }
elsif ( $html == 1 ) {
	&mysystem("l2html $bib rh rg \"$ad\" ") ;
	($firstf) = <$bib/bibliography*.html> ;
	if ( open(HTMLIN,"$firstf")  != 1 ) {
		print STDERR "Cant open $bib/bibliography*.html.\n" ;
        	exit(1) ;
	}
	open(HTMLOUT, ">$bib.html" );
	$started = 0;
	print STDERR  "Extracting $bib.html from $firstf.\n" ;
	print  HTMLOUT "<HEAD> <TITLE>$title</TITLE> </HEAD> <BODY><P>\n" ;
	print  HTMLOUT "<A HREF=\"$rh\"><IMG SRC=\"$rg\"></A>\n" ;
        print  HTMLOUT "<P><H1>$title</H1><P>\n" ;
        print  HTMLOUT "<P><H2>for the Handbook of Probability in Computing</H2><P>\n" ;
        print  HTMLOUT "<P><H2>  Date $date</H2><P><HR><P>\n" ;
	while( <HTMLIN> ) {
                if ( $started == 1 && /^<HR>/ ) {
		   print HTMLOUT "</BODY>\n<P><HR><ADDRESS>\n$ad\n</ADDRESS>" ;
		   $started = 0 ;
		}
		if ( $started == 1 || /^<DT>/  ) {
			$started = 1;
			print HTMLOUT;
		} 
	}	
	close(HTMLIN);
	close(HTMLOUT);
	&mysystem("rm -r $bib");
} elsif ( $ps == 1 ) {
	&mysystem("latex $bib") ;
	&mysystem("$dvi2ps -o $bib.ps $bib.dvi");
} else {
	&mysystem("latex $bib") ;
	&mysystem("$dvi2ps -P$printer $bib.dvi");
}
&mysystem("rm -f $bib.dvi $bib.tex $bib.bbl $bib.aux $bib.log $bib.blg");
