#! /bin/sh
# #############################################################################

# Script information
NAME_="wrapfile"
VERSION_="0.1"
USAGE_="$NAME_ <file> [<file>...]"

# Default values
HEADERFILE="header.shtml"
NAVFILE="navigation.shtml"
CSSFILE="../../../style.css"
ID="GAPDoc"

# #############################################################################

tmpfile=/tmp/tmp.${RANDOM}$$
tmpfile2=/tmp/tmp.${RANDOM}$$
newfile=/tmp/tmp.${RANDOM}$$

########### Usage information ######################
help()
{
  echo >&2 "usage: $USAGE_ 
Performs several tasks to wrap a plain html page into a CSS layout:
  - Replaces the stylesheet link in the header with a different one
  - Wraps the html content in a <div> section with an identifier
  - Adds the contents of a header file to the top of the page
  - Add the contents of a navigation file to the bottom of the page
This is written to wrap GAPDoc output into the CHA project wreb pages, but
could be used more generally. 

The various settings (header file to use etc) are set by user interaction.

Options:
  -h  Display this help"
  exit 1;
}
  
########### Process the argiments ################
case "$1" in
   ############### Test for arguments
  -h) help ;;

  -*) echo invalid argument, type $NAME_ -h for help exit 1 ;;

  *)
    if test $# = 0
    then 
      echo >&2 missing argument: you must specify at least one file. Type $NAME -h for help.
      exit 1;
    fi
 
    ############### OK - we assume we have some files as arguments

    # Now get the other information through user interation
    # stylesheet
    retry=1
    while test $retry -eq 1
    do
      echo Enter the style sheet file to insert [$CSSFILE]...
      read answer
      if test ! $answer = ""
      then 
        CSSFILE=$answer
      fi
      # Now test that it exists
      if test ! -r $CSSFILE
      then
        echo >&2 File $CSSFILE cannot be read. Please enter another file
      else
        retry=0
      fi
    done  
    # ID
    echo Enter the ID to wrap around the body [$ID]...
    read answer
    if test ! $answer = ""
    then 
      ID=$answer
    fi
    # header
    retry=1
    while test $retry -eq 1
    do
      echo Enter the header file to insert [$HEADERFILE]...
      read answer
      if test ! $answer = ""
      then 
        HEADERFILE=$answer
      fi
      # Now test that it exists
      if test ! -r $HEADERFILE
      then
        echo >&2 File $HEADERFILE cannot be read. Please enter another file
      else
        retry=0
      fi
    done  
    # navigation
    retry=1
    while test $retry -eq 1
    do
      echo Enter the navigation file to insert [$NAVFILE]...
      read answer
      if test ! $answer = ""
      then 
        NAVFILE=$answer
      fi
      # Now test that it exists
      if test ! -r $NAVFILE
      then
        echo >&2 File $NAVFILE cannot be read. Please enter another file
      else
        retry=0
      fi
    done  

    # Now process the files
    for file in $*
    do
      # Test that it exists
      if test ! -r $file
      then
        echo >&2 Cannot read $file for processing. Skipping this file.
      else
        echo Processing $file

        # Extract out the lines as far as <body>
        bodystart=`grep -n -i '<body>' $file | cut -f1 -d:`
        head -n $bodystart $file > $tmpfile

        # now use sed on tmpfile to change the stylesheet
        sed 's|\(\s*<link.*href="\).*\.css\(".*\)|\1'$CSSFILE'\2|' $tmpfile > $newfile

        # then insert the header
        cat $HEADERFILE >> $newfile

        # Now add the ID line
        echo >> $newfile
        echo '<div id="GAPDoc">' >> $newfile

        # then the rest of the file to </body> 
        bodyend=`grep -n -i '</body>' $file | cut -f1 -d:`
        bodyend=$[ $bodyend - 1 ]

        head -n $bodyend $file > $tmpfile
        length=`wc -l $tmpfile | cut -f1 -d' '`
        tail -n $[ $length - $bodystart ] $tmpfile > $tmpfile2

        cat $tmpfile2 >> $newfile

        # Now close the ID line
        echo >> $newfile
        echo '</div>' >> $newfile

        # Now the navigation
        cat $NAVFILE >> $newfile

        # and then the last bit of the file
        length=`wc -l $file | cut -f1 -d' '`
        tail -n $[ $length - $bodyend ] $file > $tmpfile2

        cat $tmpfile2 >> $newfile

        # Now rename the input file with .bak and copy tmpfile2 in its place
        cp $file $file.bak
        cp $newfile $file
      fi
    done
esac;
