#!/usr/bin/perl -w
# stupid script to generate WWW site.

use FileHandle;
use Getopt::Long;
my $lily_version;
my $footstr;
my $mw_id = "<!make_website!>";

sub get_version
{
    my ($vstr)=("");
    open V, "$depth/.version";
    while (<V>) {
	s/#.*$//g;
	next if (/^ *$/);
	s/^/\$/;
	s/= *(.*)$/=\"$1\";/;
	$vstr .= $_;
    }
    eval ($vstr);
    
    $lily_version= "$TOPLEVEL_MAJOR_VERSION.$TOPLEVEL_MINOR_VERSION.$TOPLEVEL_PATCH_LEVEL$TOPLEVEL_MY_PATCH_LEVEL";
    
    # stupid checks.
    $lily_version= "$TOPLEVEL_MAJOR_VERSION.$TOPLEVEL_MINOR_VERSION.$TOPLEVEL_PATCH_LEVEL$TOPLEVEL_MY_PATCH_LEVEL";


    close V;
}

sub set_html_footer
{
    my $MAILADRESS=$ENV{MAILADRESS};
    my @pw=(getpwuid($<));
    my $username=$pw[6];

    $footstr = 
	"\n<hr>Please take me <a href=index.html>back to the index</a>\n<hr>
<font size=-1>
This page was generated by <code>make_website</code> from lilypond-$lily_version by
<p>
<address><br>$username <a href=mailto:$MAILADRESS>&lt<!bla>$MAILADRESS</a>&gt</address>
<p>" . `date` . "
<p></font>";
}
    

# do something, check return status
sub my_system
{
    my (@cmds) = @_;
    foreach $cmd (@cmds) {
	my ($ignoreret)=0;
	if ( $cmd  =~ /^-/ ) {
	    $ignoreret = 1;
	    $cmd = substr ($cmd, 1);
	}
	
	my $ret =  ( system ($cmd));
	if (!$ignoreret && $ret) {
	    print STDERR "\nmake_website: failed on command \`$cmd\' (status $ret)\n";
	    exit 2;
	}
    }
}


local $base="lilypond/";
local @examples=("wohltemperirt" ,"standchen", "scsii-menuetto", "rhythm", "collisions");


sub gen_html
{
    print "generating HTML\n";
    my_system "make -kC .. html";
}

sub ps_to_gif 
{
    my ($a)=@_;
 
    if (! -f "$a.ps" ) {
	my_system("gunzip -c $a.ps.gz > $a.ps");
    }
    my_system "gs -q -sDEVICE=ppmraw -sOutputFile=- -r200 -dNOPAUSE  $a.ps -c quit |pnmscale 0.5| ppmtogif > $a.gif";
}


sub ps_to_jpeg
{ 
    my ($a)=@_;
    my $in = "$a.ps";
    
    my_system "gs -q -sDEVICE=ppmraw -sOutputFile=- -r200 -dNOPAUSE  $in -c quit |pnmscale 0.5| cjpeg -grayscale > $a.jpeg";
}

sub gen_examples
{
    print "generating examples: \n";
    foreach $a (@examples) {
	$texfile="lelie";
	$tex = "tex $texfile";
	if ($a eq "standchen" || $a eq "scsii-menuetto" ) {
	    $tex = "latex $a";
	    $texfile = "$a";
	}
	
	if ( ! -f "$a.ly.txt" ) {
	    my_system "ln $depth/input/$a.ly ./$a.ly.txt";
	}
	if (! -f "$a.dvi" ) {
	    my_system "lilypond $a;",
	    "$tex", "-mv $texfile.dvi $a.dvi";
	}  
	if ( ! -f "$a.ps.gz" && ! -f "$a.ps" ) {
	    my_system "dvips -o $a.ps $a.dvi";
	}

	my_system "mv lelie.midi $a.midi; " if ( -f "lelie.midi" );

	# generate the pixmap at twice the size, then rescale (for antialiasing)
	if ( ! -f "$a.gif" ) {
	    ps_to_gif($a);
	}
	if ( ! -f "$a.ps.gz" ) {
	    my_system "gzip -f $a.ps";
	}   
    }
}


sub gen_list
{
    print "generating HTML list\n";
    open HTMLLIST, ">example_output.html";
    print HTMLLIST "<html><body><title>LilyPond examples</title>\n";
    foreach $a (@examples) {
	$name=$a;
	print HTMLLIST "<h1>example file: $name</h1>\n<XMP>\n";

	open IF, "$depth/input/$a.ly";
	input_record_separator IF "%}";
	
	$desc = <IF>;
	close IF;
	
	print HTMLLIST "$desc\n</XMP>";

	$inputf="$a.ly.txt";
	$giff="$a.gif";
	$jpegf="$a.jpeg";
	$pngf = "$a.png";
	$psf="$a.ps.gz";
	$midif="$a.midi";
	
	print HTMLLIST "<ul>";

	print HTMLLIST "<li><a href=$inputf> The input file</a>"
	    if ( -f $inputf );
	
	print HTMLLIST "<li><a href=$giff>The output (picture)</a>"	
	    if ( -f $giff );

	print HTMLLIST "<li><a href=$psf>The output (PS)</a>\n"
	    if ( -f $psf );
		
	print HTMLLIST "<li><a href=$midif>The output (MIDI)</a>\n" 
	    if ( -f $midif );
	print HTMLLIST "</ul>";
    }
    print HTMLLIST "</BODY></HTML>";
    close HTMLLIST;
}

sub edit_html
{
    print STDERR "adding footer\n";

    OUTER:
    foreach $a (<*.html>) {
	open H, "$a";
	my $sep="</BODY>";
	input_record_separator H $sep;
	my $file="";
	
	while (<H>) { 
	    if (/$mw_id/) {
		close H;
		next OUTER;
	    }
	    $file .= $_; 

	}
	close H;

	my $subst =  $footstr;
	$subst .= $back if (! $a =~ /index.html/ );
	$file =~ s/$sep/$subst$sep/g ;
	$file =~ s/\.gif/\.$image/g;
	open H, ">$a";
	print H $mw_id;
	print H $file;
	close H;
    }
}

sub copy_files
{  
    print "copying files\n";
    print `ln -s $depth/out ./docxx` if ( ! -x "docxx" ) ;
    my_system "cp $depth/TODO ./TODO.txt",
    "cp $depth/NEWS ./NEWS.txt",
    "cp ../lelie*gif .";
}

sub set_images
{
    for $a (<*.gif>) {
	if ($opt_png) {
	    my_system "gif2png -d $a";
	}
	if ($opt_jpeg) {
	    my $b=$a;
	    $b =~ s/.gif/.jpeg/;
	    my_system "cjpeg -o $b $a";
	}
    }
}

sub docxx_update
{
    my_system "make -C $depth doc++";
}

sub do_tar
{
     print "tarring.\n";
     my_system
	 " tar vhcf website.tar *.html *.$image *.ps.gz *.txt *.midi docxx/*;",
	 "gzip -f9 website.tar;";
}
sub identify
{
    print STDERR "make_website 0.1\n";
    
}
sub main
{
    identify;
    GetOptions("jpeg", "gif", "png");

    local $image="gif" ;
    $image = "png" if ($opt_png);
    $image = "jpeg" if ($opt_jpeg);
    
    $depth = "../";
    my $cwd;
    chomp($cwd = `pwd`);
    die "need to be in directory Documentation\n" if ( ! ($cwd =~ /Documentation$/));
    get_version;
    set_html_footer;


    $depth = "../../";

    chdir ("out");
    $ENV{"TEXINPUTS"} .= ":$depth/input/:";
    $ENV{"LILYINCLUDE"} = "$depth/input/";


    gen_html;
    gen_examples;
    copy_files;
    set_images;
    gen_list;
    edit_html;

    docxx_update;
    do_tar;
}

main;
