#!/bin/bash
#<pre><b>
# Script     : bigxps
# Version    : 1.0
# Author     : Roland Rashleigh-Berry
# Date       : 15-Jul-2007
# Purpose    : Production script to convert *.lis* files listed in xdonelist.txt
#              (or listed in a xdonelist section file) to one large postscript
#              file.
# SubScripts : lis2ps
# Notes      : This must be run from the directory where both "xdonelist.txt" and
#              the output files are. Only files of the pattern *.lis* are
#              selected for conversion to postscript. Donelist section files 
#              referenced using the -n option must be named according to the
#              convention xdonesect1.txt, xdonesect2.txt etc. where the numeric
#              digit is the value supplied to the -n option.
# Usage      : bigxps > bigx.ps
#              bigxps -1 > bigx.ps
#              bigxps -s > selbigx.ps
#              bigxps -r -s > selbigx.ps
#              bigxps -n 2 > bigx2.ps
#================================================================================
# OPTIONS:
#-opt- -------------------------------description--------------------------------
#  1   (switch) extract only one title for the PDF bookmark
#  r   (switch) reference number only (i.e. no TABLE or Appendix) for PDF bookmark
#  s   (switch) select on file pattern
#  n   (value)  donelist section number
#================================================================================
# PARAMETERS:
#-pos- -------------------------------description--------------------------------
# N/A  Do not supply any parameters  
#================================================================================
# AMENDMENT HISTORY:
# init --date-- mod-id ----------------------description-------------------------
# 
#================================================================================
# This is public domain software. No guarantee as to suitability or accuracy is
# given or implied. User uses this code entirely at their own risk.
#================================================================================

# Define give-usage-message-then-exit function
usage () {
  echo "Usage: bigxps -1 -r -s -n <xdonelist section number>" 1>&2
  exit 1
}


# The following variable names and defaults have been generated for you
# to help you start. You can rename the variables to something more 
# meaningful but be sure to change all occurrences of that name in the code.
switchs=0                           # Default switch=off for select on file pattern                            
switch1=0                           # Default switch=off for extracting one title only
valuen=                             # Default value for donelist section number                           
switchr=0                           # Default switch=off for reference number only


# "case" statement for action to take on selected options
while getopts "s1rn:" param ; do
  case $param in
   "?") # bad option supplied
        usage
        ;;
   "s") # (switch) select on file pattern
        switchs=1
        ;;
   "1") # (switch) extract one title only for PDF bookmark
        switch1=1
        ;;
   "r") # (switch) extract reference number only for PDF bookmark
        switchr=1
        ;;
   "n") # (value) donelist section number
        valuen=$OPTARG
        ;;
  esac
done


# shift to bring first parameter to position 1
shift $(($OPTIND - 1))


# No parameters allowed
if [ $# -gt 0 ] ; then
  echo "Error: (bigxps) Do not supply any parameters" 1>&2
  usage
fi


# set the donefile to use
if [ ! -z "$valuen" ] ; then
  donefile=xdonesect${valuen}.txt
else
  donefile=xdonelist.txt
fi


# extract one title only for PDF bookmark
onet=
if [ $switch1 -eq 1 ] ; then
  onet=-1
fi


# reference number only
refonly=
if [ $switchr -eq 1 ] ; then
  refonly=-r
fi


# select only valid entries and sort in order
grep '^...-...-...-...-...-... ' $donefile | sort > ~/bigxps_$$.tmp


#################### SWITCH OFF SHELL GLOBBING #######################
set -f


# if option set then prompt for file pattern else default to *
patt=*
if [ "$switchs" == "1" ] ; then
  if [ -z $valuen ] ; then
    echo -n "Enter your file pattern: " 1>&2
    read patt
  else
    echo "You requested a file pattern but this is not for xdonelist section
files" 1>&2
  fi
fi


for filename in $(cat ~/bigxps_$$.tmp | gawk '{print $2}') ; do
  case "$filename" in
    $patt)
      if [[ "$filename" == *.lis* ]] ; then
        lis2ps $onet $refonly "$filename"
      elif [[ "$filename" == *.ps* ]] ; then
        cat "$filename"
      fi
    ;;
  esac
done


rm -f ~/bigxps_$$.tmp
