#!/usr/bin/perl
###!/usr/local/bin/perl

$dd = '/';  # directory-delimiter

### $Id: makemap,v 1.4 1997/07/03 14:16:18 RRM Exp $
### File:  makemap
### Written by Herbert W. Swan <dprhws@edp.Arco.com>
### Version 1.0,  December 22, 1995
### This is part of the 96.1 release of LaTeX2HTML by Nikos Drakos

## Copyright (C) 1995 by Herbert W. Swan
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2 of the License, or
## (at your option) any later version.

## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
## You should have received a copy of the GNU General Public License
## along with this program; if not, write to the Free Software
## Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

## Change Log (for initials see file Changes):
## $Log: makemap,v $
## Revision 1.4  1997/07/03 14:16:18  RRM
##  -  added HTML comments with the USEMAP option
##  -  the $labels can be given as a filename (test existence) or a prefix
##
## Revision 1.3  1997/07/03 07:24:47  RRM
##  -  Extended to also process for client-side image-maps, via <USEMAP> tags
##  -  introduced a  $dd  directory-delimiter, for customisation
##
## Revision 1.2  1996/12/23 01:41:16  JCL
## o added informative comments and CVS log history
## o changed usage of <date> to an OS independent construction, the
##   patch is from Piet van Oostrum.
## o moved most of the code into a main function
##
##
## HWS -- Dec 22, 1995 -- created

#
#  LaTeX2HTML image map script
#

#  NOTE:  The following configuration variable MUST be set to either
#	"NCSA" or "CERN" to reflect the type of server you are using;
#        or "USEMAP"  (AXR)
$SERVER = "NCSA";

#
#  Get the command-line arguments:
#
die "Usage:  makemap map_template [dest_directory]\n" unless @ARGV;
$template_file = $ARGV[0];
$dest_dir = ".";
$dest_dir = $ARGV[1] if ($#ARGV >= 1);
# print $#ARGV, ":", $dest_dir, "\n";
$map_file = "";
$Date = &get_date;

&main;

sub main {
#
#  Next, try to open the template file.
#
    open (TEMPLATE, "<$template_file") || die "Can't open $template_file\n";
    while (<TEMPLATE>) {
	next if /^(\s*\#.*|\s*)$/; # Ignore comments;

#
#  Associate the contents of a labels.pl file with a URL.
#
	if (/^\+(\S+)\s+(\S+)/) {
	    $labels = (-f ${1})? ${1} : "${1}labels.pl";
	    $URL    = $2;
	    open (LABELS, "<$labels") || die "Can't open $labels\n";
	    while (<LABELS>) {
		eval if (/^\$/);
	    }
	    close (LABELS);
	    next;
	}
#
#  Proceed to the next map file.
#
	elsif (/(\S*):\s*$/) {
	    close (MAP) if (length($map_file));
	    $map_file = "$dest_dir${dd}$1";
	    open (MAP, ">$map_file") || 
		die "Can't open $map_file for output.";
	    if ( $SERVER eq "USEMAP" ) {
		($tag_name = $1) =~ s/\.map//g;
		print (MAP "<!-- Image map, generated by makemap 1.2 at $Date\n");
		print (MAP "     from source  $template_file  using  $labels  -->\n\n");
		print (MAP "<MAP NAME=\"$tag_name\">\n");
	    } else {
		print (MAP "# Created by makemap 1.2\n");
		print (MAP "# ", $Date);
		print (MAP "#\n\n\n");
	    }
	    next;
	}
#
#  Continue mapping coordinates to URL's:
#
	else {
	    $url = $_;
	    @array = split(/[ \t\n]+/);
	    $op    = @array[1];
	    if (!/\//) {
		($symbol = @array[0]) =~ s/://g;
		$url = $symbol unless ($url = $external_labels{$symbol});
		next unless $url;
		if ($SERVER eq "NCSA") {
		    print MAP ("$op $url @array[2..$#array]\n");
		} elsif ($SERVER eq "CERN") {
		    print MAP ("$op @array[2..$#array] $url\n");
		} elsif ($SERVER eq "USEMAP") {
		    print MAP ("<AREA SHAPE=\"$op\" ALT=\"\" "
		        . "COORDS=\"@array[2],@array[3]\"\n HREF=\"$url\">\n");		    
		} else {
		    print STDERR "Unknown map-server  $SERVER, ignoring:\n";
		    print STDERR ("$op @array[2..$#array] $url\n");
		}
	    } elsif ($SERVER eq "USEMAP") {
		print MAP ("<AREA SHAPE=\"$op\" ALT=\"\" "
		       . "COORDS=\"@array[2],@array[3]\"\n HREF=\"@array[0]\">\n");		    
#
#  Explicit URL's simply get copied.
#
	    } else {
		print MAP ("$op @array[0] @array[2..$#array]\n");
	    }
	}
    }
#
#  EOF on input.  Finish up.
#

    close (TEMPLATE);
    print MAP "</MAP>\n" if ($SERVER eq "USEMAP");
    close (MAP) if (length($map_file));
}

sub get_date {
    local(@lt) = localtime;
    local($d,$m,$y) = @lt[3,4,5];
    sprintf("%d/%d/%d", $m+1, $d, 1900+$y);
}

