#! /bin/sh
#
# GT -- Grammar Tools
# Copyright (C) 2000 Merijn de Jonge <mdejonge@cwi.nl>
#                    Eelco Visser <visser@acm.org>
#                    Joost Visser <jvisser@cwi.nl>
#
# 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, 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., 59 Temple Place - Suite 330, Boston, MA
# 02111-1307, USA.

# $Id: tohtml.sh.src,v 1.7 2001/09/18 12:46:31 mdejonge Exp $

#
#
# usage:
#    tohtml [-d] [-e ext] [-f] [-h] [-I dir] [-o dir] [-p pp-table ...] 
#           [-q] [-t title] [-v] <module>
#    where
#       -d           Enable debugging: temporary files are not removed and
#                    shell script is executed with -x option
#       -e ext       Modules hvae extension ".ext"
#       -f           Force overwrite of css files
#       -h           Displays this help
#       -I dir       Specifies search path
#       -o dir       Specifies output directory
#       -p pp-table  Used to specify additional pretty-print tables
#       -q           Runs quietly
#       -t title     Specifies title of html document
#       -v           Prints version information
#       <module>     AsFix module to be pretty-printed
#
#

BINDIR=/usr/bin
DATADIR=/usr/share/StrategoXT/gpp
LIBEXEC=/usr/libexec
GRAPHTOOLS=/usr
SGLR=/usr

GRAPHVIZ=
MAKE=/usr/bin/make
VERSION=2.10

# Extend PATH to include bin directories of additional tools
PATH=${BINDIR}:${GRAPHTOOLS}/bin:${GRAPHVIZ}/bin:${SGLR}/bin:${PATH}
export PATH

# Programs
ASFIX2ABOX="asfix2abox"
AST2ABOX="ast2abox"
ABOX2TEXT="abox2text"
ABOX2HTML="abox2html"
GXML2DOT="GraphXML2dot"
DOT_PP_TABLE=${GRAPHTOOLS}/share/graph-tools/dot.pp
DOT="dot"
ISMAP2USEMAP="${LIBEXEC}/ismap2usemap"
DEBUG=false
VERBOSE=true
OVERWRITE=false
SEARCHPATH=
OUTPUT=.
TOPFILE="index"
MENU="specification"
IGRAPH="import-graph"
CSS="tohtml"

PWD=`pwd`
tmp=/tmp/`basename $0`.$$

# Display usage message
usage()
{
   cat << ENDCAT
`basename $0` (C) 1998-2000, Merijn de Jonge (mdejonge@cwi.nl)

usage:
   `basename $0` [-d] [-e ext] [-f] [-h] [-I dir] [-o dir] [-p pp-table ...] 
                  [-v] [-t] [-q] <module>
   where
      -d           Enable debugging: temporary files are not removed and
                   shell script is executed with -x option
      -e ext       Modules hvae extension ".ext"
      -f           Force overwrite of css files
      -h           Displays this help
      -I dir       Specifies search path
      -o dir       Specifies output directory
      -p pp-table  Used to specify additional pretty-print tables
      -q           Runs quietly
      -t title     Specifies title of html document
      -v           Prints version information
      <module>     AsFix module to be pretty-printed
ENDCAT
}

copyright ()
{
   cat << ENDCAT
AsFix to HTML Translator (C) 1999-2000, Merijn de Jonge (mdejonge@cwi.nl)
ENDCAT
}

version ()
{
   echo `basename $0` version ${VERSION} >&2
}

# Parse command line arguments
for opt in $*
do
   case $1 in
      -d )  DEBUG=true; shift ;;
      -e )  shift; MOD_EXTENSION1="$1"; shift ;;
      -f )  OVERWRITE=true; shift ;;
      -h )  usage; exit 0 ;;
      -I )  SEARCHPATH="${SEARCHPATH} -I `(cd $2; pwd)`"; shift; shift ;;
      -o )  shift ; OUTPUT="$1" ; shift ;;
      -p )  shift ; PPTABLES="$PPTABLES $PWD/$1" ; shift ;;
      -q )  VERBOSE=false; shift ;;
      -t ) shift; DOCTITLE=$1; shift ;;
      -v )  version; exit 0 ;;
      -* )  echo "Invalid option: $1" >&2 ; exit 1;;
       * )  break ;;
   esac
done

if [ "$VERBOSE" = "true" ]
then
   # Display copyright message
   copyright >&2
fi

# when -d options was specified, we continue execution with the -x
# option (i.e., commands and their arguments are printed as they are
# executed). Furthermore, in debuging mode temporary files are not
# removed.
if [ $DEBUG = "true" ]
then
   set -x
else
   trap "rm -f $tmp.*" 0 1 2 3 4 5 6 7 8 9 10
fi

# Did user specify a module name
if [ "a$1" = "a" ]
then
   echo "No or invalid module name specified" >&2
   exit 1
fi


# Return full path name when '-o' option was not specified. Otherwise only
# return its basename
make_path () 
{
   if [ "a$OUTPUT" = "a." ]
   then
      echo $1
   else
      echo "`basename $1`"
   fi
}

# make absolute path from module name
MODULE=`basename $1 | sed 's/\.[^.]*$//'`
shift

if [ "a$DOCTITLE" = "a" ]
then
   DOCTITLE="$MODULE"
fi

# Create output directory
mkdir -p ${OUTPUT}

$IMPORTS -l ${SEARCHPATH} -i $MODULE -e $MOD_EXTENSION1 \
    | tr -d '"\[\]' | tr "[,]" "[\\n]" > $tmp.modules || exit 1
MODULES=`sort < $tmp.modules`

if [ $DEBUG = "true" ]
then
   DEBUGFLAG=-d
else
   DEBUGFLAG=
fi

cat <<ENDCAT >$tmp.makefile
PPTABLES=${PPTABLES}
.SUFFIXES: .asfix .asfix.box .html .$MOD_EXTENSION1 \
        ${MOD_EXTENSION2:+${MOD_EXTENSION2}} \
        ${MOD_EXTENSION3:+${MOD_EXTENSION3}}

${OUTPUT}/%.asfix : %.$MOD_EXTENSION1
	sglr -2 -p ${PARSETABLE} -i \$< -o \$@

${OUTPUT}/%.asfix : %.$MOD_EXTENSION2
	sglr -2 -p ${PARSETABLE} -i \$< -o \$@

${OUTPUT}/%.asfix : %.$MOD_EXTENSION3
	sglr -2 -p ${PARSETABLE} -i \$< -o \$@

${OUTPUT}/%.asfix.box : ${OUTPUT}/%.asfix
	${ASFIX2ABOX} -i \$< \$(PPTABLES:%=-p %) -o \$@

${OUTPUT}/%.html:${OUTPUT}/%.asfix.box
	@${ABOX2HTML} -t "tohtml generated file" -i \$< -o \$@
ENDCAT

# Don't use MAKEFLAGS variable because it might interfere with the make
# that we use.
unset MAKEFLAGS
unset MAKELEVEL

# Generate all HTML files using the generated Makefile. We use VPATH to
# support the '-o' option (it enables us to store the generated files in an
# other location than the corresponding source file). 
for d in $MODULES
do
   f="`dirname $d`/`basename $d | sed 's/\.[^.]*$//'`"
   f="`make_path ${OUTPUT}/$f`"
   echo "Generating ${OUTPUT}/$f.html"
   VPATH=`dirname $d` ${MAKE} -s -f $tmp.makefile ${OUTPUT}/$f.html || exit 1
done

# Create Cascading style sheet for HTML modules
if [ "$OVERWRITE" = true -o ! -f ${OUTPUT}/boxstyle.css ]
then
   ${ABOX2HTML} -c > ${OUTPUT}/boxstyle.css || exit 1
fi

# AFter all individual modules have been converted to HTML, we now generate
# the main HTML pages, the import-graph page, and a cascading style-sheet.
make_head()
{
   cat << ENDCAT
<!--
 This file is generated automatically on `date +%c` by

    tohtml (C) 1999 Merijn de Jonge (mdejonge@cwi.nl)
-->
<head>
<title>$DOCTITLE</title>
<link rel="stylesheet" type="text/css" href="$CSS.css">
</head>
ENDCAT
}
   

(
cat <<ENDCAT
<!DOCTYPE HTML PUBLIC "-//W3C//DTD WWW HTML 4.0//EN"
          "http://www.w3.org/TR/REC-html40/loose.dtd">
<html>
`make_head`
<frameset cols="20%,80%">
   <frame src="$MENU.html">
   <frame src="$IGRAPH.html" name="dataframe">
</frameset>
</html>
ENDCAT
) > ${OUTPUT}/$TOPFILE.html


(
cat <<ENDCAT
<!DOCTYPE HTML PUBLIC "-//W3C//DTD WWW HTML 4.0//EN"
          "http://www.w3.org/TR/REC-html40/loose.dtd">
<html>
`make_head`

<body>
   <style type="text/css">
      .copyright {font-family: times ;font-size: 8pt}
   </style>

   <p class="copyright">Generated by
   <a href="http://www.cwi.nl/~mdejonge/projects/gpp/" target="gpp">tohtml</a>
   (<a href="mailto:mdejonge@cwi.nl">mdejonge@cwi.nl</a>)</p>
   <br>
   <p></p>
   <p><span class="modules-label">$DOCTITLE</span>
   </p>
      
   &nbsp;&nbsp;<a href="$IGRAPH.html" class="module-entry" target="dataframe">Import Graph</a>
   <br>
   <p></p>
   <span class="modules-label">Modules</span>
   <br>
   <span class="modules-menu">
ENDCAT

for d in `echo $MODULES`
do
   module=`basename $d | sed 's/\.[^.]*$//'`
   echo "&nbsp;&nbsp;<a href=\"$module.html\" class=\"module-entry\" target=\"dataframe\">$module</a>"
   echo "<br>"
done
cat <<ENDCAT
</span>
</body>
</html>
ENDCAT
) > ${OUTPUT}/$MENU.html

# Generate page with import graph
# first create gif image

#$IMPORTS $SEARCHPATH --graphxml -i $MODULE -e $MOD_EXTENSION1 -o $tmp.modules || exit 1
spec2adot "$SEARCHPATH" "$MODULE" "$MOD_EXTENSION1" "$tmp.modules" 
$AST2ABOX -p ${DOT_PP_TABLE} -i $tmp.modules \
   | ${ABOX2TEXT} \
   | $DOT -Tgif > ${OUTPUT}/$IGRAPH.gif

# Then create page with usemap
(
cat << ENDCAT
<!DOCTYPE HTML PUBLIC "-//W3C//DTD WWW HTML 4.0//EN"
          "http://www.w3.org/TR/REC-html40/loose.dtd">
<html>
`make_head`
<body>
<center>
<img src="$IGRAPH.gif" usemap="#map1" border="0">
<map name="map1">
`${AST2ABOX} -p ${DOT_PP_TABLE} -i $tmp.modules|\
   ${ABOX2TEXT} |\
   $DOT -Tismap |\
   $ISMAP2USEMAP`
</map>
</center>
</body>
</html>
ENDCAT
) > ${OUTPUT}/$IGRAPH.html

# Create Cascading Style Sheet for main HTML page
if [ "$OVERWRITE" = true -o ! -f ${OUTPUT}/$CSS.css ]
then
   cp ${DATADIR}/$CSS.css ${OUTPUT}
fi
