#!/bin/sh
export PATH || exec /bin/sh $0 $argv:q

# sqlj - shell for invoking the new integrated command line version
# of the SQLJ translator 
# Usage:  sqlj  <-J-javavm-options> <-P-profiler-options> <-C-compiler-options>
#               <-sqlj-options> <*.java, *.sqlj, and *.ser files>
#
# Note: The options are automatically prefixed with any options contained
#       in the environment variable SQLJ_OPTIONS
#
# Note: if you need to pass quoted arguments unchanged, or you
#       do not need to pass options to the Java interpreter,
#       just uncomment the lines below.
#
#exec java sqlj.tools.Sqlj "$@" 

switches=

files=
arg=
s_option=
javavm=java
echo_args=
passes=
f_next=
s_next=

###                                                        ###
### Process options from SQLJ_OPTIONS environment variable ###
###                                                        ###
for s_option in $SQLJ_OPTIONS
do
  if [ "$f_next" ]
  then
    files="$files $f_next$s_option"
    f_next=
    if [ "$s_next" ]
    then
      switches="$switches $s_next$s_option"
      s_next=
    fi
  else
    if [ "$s_next" ]
    then
      switches="$switches $s_next$s_option"
      s_next=
    else
      case "$s_option" in
    
       ###  Switches for SQLJ processing ###
    
       -vm=echo)       echo_args="ShowArgs" ;;
       -n)             echo_args="ShowArgs" ;;
       -vm=*)          javavm=`echo $s_option | cut -c5-` ;;
       -passes)        passes="SQLJPass";;
    
       ### Switches to the JavaVM ###
    
       -classpath)     		s_next="-classpath "
                       		f_next="-C-classpath=";;
       -classpath=*)   		arg=`echo $s_option | cut -c12-`
                       		files="$files -C-classpath=$arg"
                       		switches="$switches -classpath $arg";;
       -J-classpath)   		s_next="-classpath ";;
       -J-classpath=*) 		arg=`echo $s_option | cut -c14-` 
                       		switches="$switches -classpath $arg";;
       -J-*)           		arg=`echo $s_option | cut -c3-` 
                       		switches="$switches $arg";;
    
       ### Better compatibility with loadjava ###
    
       -h)             files="$files -help";;
       -v)             files="$files -status";;
       -e)             f_next="-encoding=";;
       -u)             f_next="-user=";;
       -p)             f_next="-password=";;
    
       ### And for those that are confused between loadjava a7d sqlj ###
    
       -u=*)           arg=`echo $s_option | cut -c4-` 
                       files="$files -user=$arg";;
       -p=*)           arg=`echo $s_option | cut -c4-` 
                       files="$files -password=$arg";;
       -e=*)           arg=`echo $s_option | cut -c4-` 
                       files="$files -encoding=$arg";;
    
       ### Better compatibility with javac ###
    
       -encoding)      f_next="-encoding=";;
       -d)             f_next="-d=";;
       -g)             files="$files -C-g -linemap=true";;
       -O)             files="$files -C-O -linemap=false";;
       -nowarn)        files="$files -warn=none -C-nowarn";;
       -verbose)       files="$files -status -C-verbose";;
       -depend)        files="$files -checksource -C-depend";;
       -deprecation)   files="$files -C-deprecation";;
    
       ### Better compatibility for those with Unix fingers ###
    
       -user)          f_next="-user=";;
       -url)           f_next="-url=";;
       -password)      f_next="-password=";;
       -C-bootclasspath)   f_next="-C-bootclasspath=";;
       -C-classpath)   f_next="-C-classpath=";;
       -C-extdirs)   f_next="-C-extdirs=";;
       -C-target)   f_next="-C-target=";;
       -dir)           f_next="-dir=";;
       -driver)        f_next="-driver=";;
    
       ### Better compatibility, even with tags ###
    
       -u@*=*)         arg=`echo $s_option | cut -c4-`
                       files="$files -user@$arg";;
       -u@*)           arg=`echo $s_option | cut -c4-`
                       f_next="-user@$arg=";;
       -user@*=*)      files="$files $s_option";;
       -user@*)        arg=`echo $s_option | cut -c7-`
                       f_next="-user@$arg=";;
       -url@*=*)       files="$files $s_option";;
       -url@*)         arg=`echo $s_option | cut -c6-`
                       f_next="-url@$arg=";;
       -password@*=*)  files="$files $s_option";;
       -password@*)    arg=`echo $s_option | cut -c11-`
                       f_next="-password@$arg=";;
       -p@*=*)         arg=`echo $s_option | cut -c4-`
                       files="$files -password@$arg";;
       -p@*)           arg=`echo $s_option | cut -c4-`
                       f_next="-password@$arg=";;
    
       #############
    
       *)              files="$files $s_option";;
     esac
   fi
 fi
done
### End Process options from SQLJ_OPTIONS ###

while [ $# -gt 0 ]
do
  case "$1" in

   ###  Switches for SQLJ processing ###

   -vm=echo)       echo_args="ShowArgs";;
   -n)             echo_args="ShowArgs";;
   -vm=*)          javavm=`echo $1 | cut -c5-` ;;
   -passes)        passes="SQLJPass";;

   ### Switches to the JavaVM ###

   -classpath)     	switches="$switches -classpath $2"
                   	files="$files -C-classpath=$2"
                   	shift;;
   -classpath=*)   	arg=`echo $1 | cut -c12-`
                   	files="$files -C-classpath=$arg"
                   	switches="$switches -classpath $arg";;
   -J-classpath)   	switches="$switches -classpath $2"
                   	shift;;
   -J-classpath=*) 	arg=`echo $1 | cut -c14-` 
                   	switches="$switches -classpath $arg";;
   -J-*)           	arg=`echo $1 | cut -c3-` 
                   	switches="$switches $arg";;

   ### Better compatibility with loadjava ###

   -h)             files="$files -help";;
   -v)             files="$files -status";;
   -e)             files="$files -encoding=$2"
                   shift;;
   -u)             files="$files -user=$2"
                   shift;;
   -p)             files="$files -password=$2"
                   shift;;

   ### And for those that are confused between loadjava and sqlj ###

   -u=*)           arg=`echo $1 | cut -c4-` 
                   files="$files -user=$arg";;
   -p=*)           arg=`echo $1 | cut -c4-` 
                   files="$files -password=$arg";;
   -e=*)           arg=`echo $1 | cut -c4-` 
                   files="$files -encoding=$arg";;

   ### Better compatibility with javac ###

   -encoding)      files="$files -encoding=$2"
                   shift;;
   -d)             files="$files -d=$2"
                   shift;;
   -g)             files="$files -C-g -linemap=true";;
   -O)             files="$files -C-O -linemap=false";;
   -nowarn)        files="$files -warn=none -C-nowarn";;
   -verbose)       files="$files -status -C-verbose";;
   -depend)        files="$files -checksource -C-depend";;
   -deprecation)   files="$files -C-deprecation";;

   ### Better compatibility for those with Unix fingers ###

   -user)          files="$files -user=$2"
                   shift;;
   -url)           files="$files -url=$2"
                   shift;;
   -password)      files="$files -password=$2"
                   shift;;
   -C-bootclasspath)   files="$files -C-bootclasspath=$2"
                   shift;;
   -C-classpath)   files="$files -C-classpath=$2"
                   shift;;
   -C-extdirs)   files="$files -C-extdirs=$2"
                   shift;;
   -C-target)   files="$files -C-target=$2"
                   shift;;
   -dir)           files="$files -dir=$2"
                   shift;;
   -driver)        files="$files -driver=$2"
                   shift;;

   ### Better compatibility, even with tags ###

   -u@*=*)         arg=`echo $1 | cut -c4-`
                   files="$files -user@$arg";;
   -u@*)           arg=`echo $1 | cut -c4-`
                   files="$files -user@$arg=$2"
                   shift;;
   -user@*=*)      files="$files $1";;
   -user@*)        arg=`echo $1 | cut -c7-`
                   files="$files -user@$arg=$2"
                   shift;;
   -url@*=*)       files="$files $1";;
   -url@*)         arg=`echo $1 | cut -c6-`
                   files="$files -url@$arg=$2"
                   shift;;
   -password@*=*)  files="$files $1";;
   -password@*)    arg=`echo $1 | cut -c11-`
                   files="$files -password@$arg=$2"
                   shift;;
   -p@*=*)         arg=`echo $1 | cut -c4-`
                   files="$files -password@$arg";;
   -p@*)           arg=`echo $1 | cut -c4-`
                   files="$files -password@$arg=$2"
                   shift;;

### End of compatibility with javac and loadjava ###

   *)              files="$files $1";;
 esac
 shift
done

###                                 ###
### Should we expand the CLASSPATH? ###
###                                 ###

if [ "$ORACLE_HOME" ]
then
  if [ "$CLASSPATH" ]
  then
    CLASSPATH="$CLASSPATH:$ORACLE_HOME/sqlj/lib/translator.jar"
  else
    CLASSPATH="$ORACLE_HOME/sqlj/lib/translator.jar"
  fi
  export CLASSPATH
fi

###                                                     ###
### Running SQLJ in a single pass or in two passes with ###
###     explicit invocation of the Java compiler        ###
###                                                     ###
if [ "$echo_args" ]
then
  echo $javavm $switches sqlj.tools.Sqlj $files
else
  if [ "$passes" ]
  then
    $javavm $switches sqlj.tools.Sqlj "-pass1=$passes" $files
    if [ -f "${passes}.run" ]
    then
      javac=`cat ${passes}.run`
      # echo "[Info: COMPILER: ${javac}]"
      if $javac >"${passes}.log"
      then
        $javavm $switches sqlj.tools.Sqlj "-pass2=$passes"
        if [ -f "${passes}.run" ]
        then
          javac=`cat ${passes}.run`
          # echo "[Info: COMPILER: ${javac}]"
          $javac
          $javavm $switches sqlj.tools.Sqlj "-pass3=$passes"
        else
         :
        fi 
      else
        $javavm $switches sqlj.tools.Sqlj "-fail2=$passes"
      fi
      # rm -f "${passes}.run" "${passes}.ser" "${passes}.log"
    else
      # nothing left to do
      :
    fi
  else
    $javavm $switches sqlj.tools.Sqlj $files
  fi
fi
