#!/usr/bin/env bash
# yam2html

# parameters
P=`basename $0`				
DIR=`dirname \`dirname $0\``
USAGE="${P} [-h(elp)] [-i(ndex) index-title] file.yam ... "\
"| -a(ll) | -r(recurse) | -t(alk) |\n"\
"-l(anguage html|tree|latex|pretty file.yam \n"\
"[i.e. either give it a list of yam files, or tell it to do all the .yams \n"\
"in the current directory that are newer than their respective .htmls \n"\
"-i builds an index.yam; -a does all in the current dir; -r recurses dirs \n"\
"-t looks for the script to create a talk within the yam file]"
OPTIONSTRING=hai:nrtl:
DOALL=""
DOINDEX=""
RECURSE=""
VERSION5="(YAM version 5)"
NEW="1"
TALK=""
LANGUAGE="html"
if [ x"$GATEWIKI" = x ]
then
  GATEWIKI=$DIR
fi

# message & exit if exit num present
usage() { echo -e Usage: $USAGE; [ ! -z "$1" ] && exit $1; }

# process options
while getopts $OPTIONSTRING OPTION
do
  case $OPTION in
    h)	usage 0 ;;
    a)	DOALL=1 ;;
    r)	RECURSE=1 ;;
    i)  DOINDEX=1; INDEXTITLE="$OPTARG" ;;
    n)  NEW=1; VERSION5='(YAM version 5)' ;;
    t)  TALK=1 ;;
    l)  LANGUAGE="$OPTARG" ;;
    *)	usage 1 ;;
  esac   
done 
shift `expr $OPTIND - 1`

# convert a single file ($1)
yamconvert() {
  YAMFILE=
  OUTFILE=
  FLAGS=
  IO=
  SUFFIX=html
  case $1 in
    -?) FLAGS="$1 $2"; shift; shift; ;;
  esac
  case $FLAGS in
    *latex) SUFFIX=tex; ;;
    *tree) SUFFIX=tree; ;;
    *pretty) SUFFIX=pretty; ;;
  esac
  ARG=$1
  case $1 in
    *\.) echo "$1 can't end in ."; exit 1; ;;
    *.html) echo "$1 shouldn't be html"; exit 1; ;;
    *.yam) ARG=`echo $1 |sed 's,\.yam$,,'`; ;;
  esac
  YAMFILE=$ARG.yam
  OUTFILE=$ARG.$SUFFIX
  IO="-i $YAMFILE -o $OUTFILE"
  # to get latex output: IO="-l latex -i $YAMFILE -o $OUTFILE"
  echo $P: converting $YAMFILE to $OUTFILE $VERSION5

  # classpath:
  # use the local classes and libs created by maven...
  CPATH=${GATEWIKI}/classes
  for f in ${GATEWIKI}/lib/*.jar; do CPATH=${CPATH}:$f; done

  # ...or if we've got an installed composite jar next to the script, use that
  [ -f `dirname $0`/yam2html-fix-tt.jar ] && \
    CPATH=`dirname $0`/yam2html-fix-tt.jar
  case `uname` in CYGWIN*|cygwin*) CPATH=`cygpath -wp $CPATH`; esac
  # echo; echo $CPATH; echo

  if [ x$NEW != x1 ]
  then
    echo    "java -classpath '${CPATH}' gate.wiki.antlr.Tool $FLAGS ${IO}"
    bash -c "java -classpath '${CPATH}' gate.wiki.antlr.Tool $FLAGS ${IO}"
  else
    echo "java -classpath '${CPATH}' gate.yam.YamCommand $FLAGS ${IO}"
    bash -c "java -classpath '${CPATH}' gate.yam.YamCommand $FLAGS ${IO}" \
      2>&1 |grep -v '^log4j'
  fi
}

# create or update index if required
if [ "$DOINDEX" == 1 ]
then
  [ -f index.yam ] && mv index.yam index.yam.bak
  [ -f index.html ] && mv index.html index.html.bak
  echo "$INDEXTITLE" >index.yam
  echo >>index.yam
  echo >>index.yam
  for f in *.yam
  do
    H=`echo $f |sed 's,\..*,,'`
    echo '- %('${H}'.html)' >>index.yam
  done
fi

# do the work
if [ "$TALK" == 1 ]
then
  COMMAND=`grep '^%%% yam2html' $1 |sed 's,^%%% ,,'`
  echo "$COMMAND"
  sh -c "$COMMAND"
elif [ "$RECURSE" == 1 ]
then
  DIRS=`find -type d |sed 's,^..,,' |egrep -v '/\.|^\.'`
  for d in $DIRS
  do
    # echo $d
    ( cd $d; set *.yam && [ $1 == '*.yam' ] && continue; \
      for f in $*; do $0 -n $f; done )
  done
elif [ "$DOALL" != 1 ]
then
  for f in $*
  do
    yamconvert -l $LANGUAGE $f
  done
else
  for f in *.yam
  do
    [ -f "$f" ] || break
    BASE=`echo $f |sed 's,\.yam$,,'`
    if [ ! -e $BASE.html -o $BASE.yam -nt $BASE.html ]
    then
      yamconvert -l $LANGUAGE $f
    fi
  done
fi
