#!/bin/bash

# xgfconfig: A configuration program for Xgridfit
# by Peter S. Baker
# Copyright (c) 2008
# GNU Public License

COMPILE_TEMPLATE=''
PARAM_TEMPLATE=''
VALID_TEMPLATE=''
OUTFILE_TEMPLATE=''
JAR_LOCATION=/usr/share/java/
COMPILE_JAR=''
VALID_JAR=''
SCHEMA_TYPE=''
COMPILE_EXECUTABLE=''
VALID_EXECUTABLE=''

usage() {
    cat <<Here-is-the-usage-text

usage: xgfconfig [options]

Options:
 -h        Display this help message
 -l        List processors and validators known to xgfconfig
 -p xsltproc | saxon-6 | saxon-9 | xalan-j | xalan-c | 4xslt
           Select an XSLT processor for Xgridfit to invoke
 -j file   Path to jar file for Java-based XSLT processor
 -V xmllint | jing | msv | rnv | xmlstarlet
           Select a validator for Xgridfit to invoke
 -J file   Path to jar file for Java-based validator
 -c        Clears all settings (restores defaults)

Example:
  xgfconfig -c -p saxon-9 -j ~/java/saxon/saxon9.jar -V jing

  Clears all settings, then sets saxon-9 as the XSLT processor,
  running from a jar file in the user's account. Sets Jing as
  the validator, with the jar file to be located by xgfconfig.

Here-is-the-usage-text
}

gettemplates() {
    if [ -f $1 ]
        then
        if grep -q COMPILE_TEMPLATE $1
            then
	    COMPILE_TEMPLATE=`grep COMPILE_TEMPLATE $1 | sed s/COMPILE_TEMPLATE=//`
        fi
        if grep -q PARAM_TEMPLATE $1
            then
	    PARAM_TEMPLATE=`grep PARAM_TEMPLATE $1 | sed s/PARAM_TEMPLATE=//`
        fi
	if grep -q OUTFILE_TEMPLATE $1
            then
	    OUTFILE_TEMPLATE=`grep OUTFILE_TEMPLATE $1 | sed s/OUTFILE_TEMPLATE=//`
        fi
        if grep -q VALID_TEMPLATE $1
            then
	    VALID_TEMPLATE=`grep VALID_TEMPLATE $1 | sed s/VALID_TEMPLATE=//`
        fi
        if grep -q COMPILE_JAR $1
            then
	    COMPILE_JAR=`grep COMPILE_JAR $1 | sed s/COMPILE_JAR=//`
        fi
        if grep -q VALID_JAR $1
            then
	    VALID_JAR=`grep VALID_JAR $1 | sed s/VALID_JAR=//`
        fi
        if grep -q SCHEMA_TYPE $1
            then
	    SCHEMA_TYPE=`grep SCHEMA_TYPE $1 | sed s/SCHEMA_TYPE=//`
        fi
    fi
}

getdefault() {
    TMP_STRING=`xsltproc --stringparam $1 $2 \
        @xgridfit_dir@/utils/configure.xsl \
        @xgridfit_dir@/utils/defaults.xml`
}

locatejar() {
    TMP_STRING=$JAR_LOCATION$1
    if [ ! -f $TMP_STRING ]
    then
	which locate > /dev/null
	if [ $? -eq 0 ]
	then
	    TMP_STRING_A=`locate -l 1 /$1`
	    if [ ${#TMP_STRING_A} -gt 0 ]
		then
		TMP_STRING=$TMP_STRING_A
	    fi
	fi
    fi
}

parseopts() {
    while getopts "chlp:j:V:J:" optname
    do
      case "$optname" in
	  "h")
	      usage
	      exit 0
	      ;;
	  "c")
	      COMPILE_TEMPLATE=''
	      PARAM_TEMPLATE=''
	      OUTFILE_TEMPLATE=''
	      COMPILE_JAR=''
	      VALID_TEMPLATE=''
	      VALID_JAR=''
	      SCHEMA_TYPE=''
	      ;;
	  "l")
	      xsltproc --stringparam list yes \
		  @xgridfit_dir@/utils/configure.xsl \
		  @xgridfit_dir@/utils/defaults.xml
	      ;;
	  "p")
	      O_COMPILE_TEMPLATE=$COMPILE_TEMPLATE
	      getdefault processor $OPTARG
	      if [ ${#TMP_STRING} -gt 0 ]
	      then
		  COMPILE_TEMPLATE=$TMP_STRING
		  getdefault param-template $OPTARG
		  PARAM_TEMPLATE=$TMP_STRING
		  getdefault processor-executable $OPTARG
		  COMPILE_EXECUTABLE=$TMP_STRING
		  getdefault outfile-template $OPTARG
		  OUTFILE_TEMPLATE=$TMP_STRING
		  if [ ${#COMPILE_JAR} -eq 0 -o "$O_COMPILE_TEMPLATE" != "$COMPILE_TEMPLATE" ]
		      then
		      getdefault processor-jar $OPTARG
		      if [ ${#TMP_STRING} -gt 0 ]
			  then
			  locatejar $TMP_STRING
		      fi
		      COMPILE_JAR=$TMP_STRING
		  fi
	      else
		  echo "Warning: I do not recognize -p $OPTARG"
	      fi
	      ;;
	  "j")
	      getdefault processor-jar $OPTARG
	      if [ ${#TMP_STRING} -gt 0 ]
	      then
		  if ! echo $TMP_STRING | grep -q "^\/"
		      then
		      locatejar $TMP_STRING
		  fi
		  COMPILE_JAR=$TMP_STRING
	      fi
	      ;;
	  "V")
	      O_VALID_TEMPLATE=$VALID_TEMPLATE
	      getdefault validator $OPTARG
	      if [ ${#TMP_STRING} -gt 0 ]
	      then
		  VALID_TEMPLATE=$TMP_STRING
		  if [ ${#VALID_JAR} -eq 0 -o "$O_VALID_TEMPLATE" != "$VALID_TEMPLATE" ]
		      then
		      getdefault validator-jar $OPTARG
		      if [ ${#TMP_STRING} -gt 0 ]
			  then
			  locatejar $TMP_STRING
		      fi
		      VALID_JAR=$TMP_STRING
		  fi
		  getdefault validator-executable $OPTARG
		  VALID_EXECUTABLE=$TMP_STRING
		  getdefault schema-type $OPTARG
		  SCHEMA_TYPE=$TMP_STRING
	      else
		  echo "Warning: I do not recognize -V $OPTARG"
	      fi
	      ;;
	  "J")
	      getdefault validator-jar $OPTARG
	      if [ ${#TMP_STRING} -gt 0 ]
	      then
		  if ! echo $TMP_STRING | grep -q "^\/"
		      then
		      locatejar $TMP_STRING
		  fi
		  VALID_JAR=$TMP_STRING
	      fi
	      ;;
	  "?")
              usage 1>&2
              exit 1
              ;;
	  ":")
              usage 1>&2
              exit 1
              ;;
	  *)
              echo "mysterious error"
	      usage 1>&2
	      exit 1
              ;;
      esac
    done
    return $OPTIND
}

runscript() {
    rm -f ${CONFIG_FILE}.bak
    if [ -f $CONFIG_FILE ]
    then
	mv -f $CONFIG_FILE ${CONFIG_FILE}.bak
    fi
    if [ ${#COMPILE_TEMPLATE} -gt 0 ]
    then
	echo 'COMPILE_TEMPLATE='$COMPILE_TEMPLATE >> $CONFIG_FILE
	if [ ${#COMPILE_EXECUTABLE} -gt 0 ]
	    then
	    which $COMPILE_EXECUTABLE > /dev/null
	    if [ $? -ne 0 ]
		then
		echo "Warning: Program $COMPILE_EXECUTABLE not found."
	    fi
	fi
    fi
    if [ ${#PARAM_TEMPLATE} -gt 0 ]
    then
	echo 'PARAM_TEMPLATE='$PARAM_TEMPLATE >> $CONFIG_FILE
    fi
    if [ ${#OUTFILE_TEMPLATE} -gt 0 ]
    then
	echo 'OUTFILE_TEMPLATE='$OUTFILE_TEMPLATE >> $CONFIG_FILE
    fi
    if [ ${#COMPILE_JAR} -gt 0 ]
    then
	echo "Will compile with "$COMPILE_JAR
	if [ ! -f $COMPILE_JAR ]
	then
	    echo "Warning: ${COMPILE_JAR} not found."
	fi
	echo 'COMPILE_JAR='$COMPILE_JAR >> $CONFIG_FILE
    fi
    if [ ${#VALID_TEMPLATE} -gt 0 ]
	then
	echo 'VALID_TEMPLATE='$VALID_TEMPLATE >> $CONFIG_FILE
	if [ ${#VALID_EXECUTABLE} -gt 0 ]
	    then
	    which $VALID_EXECUTABLE > /dev/null
	    if [ $? -ne 0 ]
		then
		echo "Warning: Program $VALID_EXECUTABLE not found."
	    fi
	fi
    fi
    if [ ${#SCHEMA_TYPE} -gt 0 ]
	then
	echo 'SCHEMA_TYPE='$SCHEMA_TYPE >> $CONFIG_FILE
    fi
    if [ ${#VALID_JAR} -gt 0 ]
    then
	echo "Will validate with "$VALID_JAR
	if [ ! -f $VALID_JAR ]
	then
	    echo "Warning: ${VALID_JAR} not found."
	fi
	echo 'VALID_JAR='$VALID_JAR >> $CONFIG_FILE
    fi
}

if [ $# -lt 1 ]
    then
    usage 1>&2
    exit 1
fi
if [ $EUID -eq 0 ]
    then
    CONFIG_FILE=@xgridfit_dir@/settings
    else
    CONFIG_FILE=~/.xgridfit
fi
gettemplates $CONFIG_FILE
parseopts "$@"
argstart=$?
runscript "${@:$argstart}"
