#!/usr/bin/bash


##  for2exe
##  =======

#   Compile and link a TABLO-generated Fortran program.


##  bugs
##  ----

#   It is tested only with GEMPACK versions 10 and 11, and with only a few
#   versions and configurations of the available GEMPACK-compatible compilers.


##  Define widely used objects.
##  --------------------------

self='for2exe'

function itcess { #Exit unsuccessfully.
    echo "ERROR: $self: $1" 1>&2
    exit 1
}

declare stacksize #size of the stack area (for LF95)
declare fortpile #Fortran compiler


##  Process options.
##  ---------------

function version {
    cat <<-EOF
	for2exe, version 1.0
	authors: R.A. McDougall, Badri Narayanan G
	date: 2013-01
	EOF
}

function usage {
    cat <<-EOF
	Usage: for2exe [-S SIZE] FILE

	From TABLO-generated Fortran, generate an executable program.

	  -S SIZE, --stack=SIZE
	       [LF95] Set the size of the stack area.
	  --help
	       Output this usage information, and exit.
	  --version
	       Output version information, and exit.
	EOF
}

while [ $# -gt 0 ] ; do
    case $1 in
    -S* | --stack=* | --stac=* | --sta=* | --st=* | --s=*)
        if <<<"$1" grep -q '^-S.'; then
            stacksize="${1#-S}"
        elif <<<"$1" grep -q '^-S'; then
            shift
            stacksize="$1"
        else
            stacksize="${1#--*=}"
        fi
        [ -n "$stacksize" ] || itcess "Can't find stack size."
        <<<"$stacksize" grep -q '^[[:digit:]]\+$' \
            || itcess "Invalid stack size '$stacksize'."
        ;;
    --stack | --stac | --sta | --st | --s)
        itcess "Can't find stack size."
        ;;
    --version | --versio | --versi | --vers | --ver | --ve | --v)
        version
        exit
        ;;
    --help | --hel | --he | --h)
        usage
        exit
        ;;
    --)
        break
        ;;
    -*)
        itcess "Found unknown option \"$1\"."
        ;;
    *)
        break
        ;;
    esac
    shift
done
unset -f usage version


##  Ascertain Fortran compiler.
##  --------------------------

declare gpcuse="$(cygpath -u -- "${GPDIR:-C:\\gp}")/libs/gpcomp.use"
    #'gpcomp.use' file
[ -f "$gpcuse" ] || itcess "Can't find file \"$gpcuse\"."
<"$gpcuse" read fortpile\
    || itcess "Can't read compiler name from \"$gpcuse\"."
[ -n "$fortpile" ] || itcess "Can't ascertain Fortran compiler."
unset gpcuse


##  Execute compiler-specific script.
##  --------------------------------

declare librect="${libdir}" #library directory
declare wordsize #word size targeted by compiler
case "$fortpile" in
IF32 | GF32 | LF95)
    wordsize=32;;
IF64 | GF64)
    wordsize=64;;
*)
    itcess "Don't recognize Fortran compiler \"$fortpile\".";;
esac
declare f2eargs='' #arguments with which to invoke *for2exe
declare arg #argument
for arg in "$@"; do
    f2eargs="$f2eargs \"$arg\""
done; unset arg
f2eargs="${f2eargs# }"
case "$fortpile" in
IF*)
    f2eargs="-w\"$wordsize\" $f2eargs"
    eval "\$librect/ifor2exe" "$f2eargs"\
        || itcess "Can't run ifor2exe with arguments $f2eargs.";;
GF*)
    f2eargs="-w\"$wordsize\" $f2eargs"
    eval "\$librect/gfor2exe" "$f2eargs"\
        || itcess "Can't run gfor2exe with arguments $f2eargs.";;
LF95)
    [ -n "$stacksize" ] && f2eargs="-S\"$stacksize\" $f2eargs"
    eval "\$librect/lfor2exe" "$f2eargs"\
        || itcess "Can't run lfor2exe with arguments $f2eargs."
    ;;
*)
    itcess "Don't recognize Fortran compiler \"$fortpile\".";;
esac
unset f2eargs
unset wordsize librect


##  Clean up.
##  --------

unset fortpile stacksize
unset -f itcess
unset self


##  end of script
##  -------------
