#!/usr/bin/bash


##  lfor2exe
##  ========

#   With LF95, compile and link a TABLO-generated program "BASENAME.for".
#   Usage: lfor2exe [-S STACKSIZE] BASENAME


##  bugs
##  ----

#   The identification of object files to be removed is fragile and liable to
#   be defeated by interrelated file names.


##  Set up.
##  ------

self='lfor2exe'

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

declare stacksize=400000 #size of stack area
declare gbasestem #stem of base name for Fortran source file
declare gramrect #directory containing Fortran source file
declare gprectwin=${GPDIR:-C:\\gp} #Windows-form pathname for GEMPACK directory
declare gprect=$(cygpath -u -- "$gprectwin") #GEMPACK directory

#   Check that "u2d" is available.
which u2d > /dev/null 2>/dev/null\
    || itcess "Can't find required command \"u2d\"."


##  Process arguments.
##  -----------------

#   Process options.
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."
        ;;
    --)
        shift; break
        ;;
    -*)
        itcess "Found unknown option \"$1\"."
        ;;
    *)
        break
        ;;
    esac
    shift
done

#   Process filename argument.
declare gfnameread #program name, as read
declare gfstemread #stem of program name, as read
[ -n "$1" ] || itcess "Can't find argument."
if <<<"$1" grep -iq '\.for$' ; then
    gfnameread="$1"
    gfstemread="$(<<<"$1" sed 's/\.for$//I;')"
else
    gfstemread="$1"
    gfnameread="$1.for"
fi
[ -f "$(cygpath -u -- "$gfnameread")" ] \
    || itcess "Can't find source file \"$gfnameread\"."
gfilestem="$(cygpath -u -- "$gfstemread")"
gbasestem="$(basename -- "$gfilestem")"
gramrect="$(dirname -- "$gfilestem")"
unset gfilestem gfstemread gfnameread


##  Change directory.
##  ----------------

cd -- "$gramrect"


##  Set options.
##  -----------

#   Copy configuration file.
declare fig="$gprect/lf95ltg.fig" #configuration file
[ -f "$fig" ] || itcess "Can't find configuration file \"$fig\"."
cp "$fig" opt || itcess "Can't copy \"$fig\" to \"opt\"."
unset fig

#   Set include path.
echo "-i \"$gprectwin\\includes\"" >>opt

#   Declare library files.
declare lib #library file
for lib in "$gprect"/{tbgdum,gpa,gpb,modlib}.lib; do
    [ -f "$lib" ] || itcess "Can't find GEMPACK library \"$lib\"."
    echo "-lib \"$(cygpath -w "$lib")\"" >>opt
done; unset lib

#   Set module path.
echo "-mod .;\"$gprectwin\\modules\"" >>opt

#   Set stack size.
echo "-Stack $stacksize" >>opt

#   Call Windows API directly.
echo "-ml winapi" >>opt

#   Search Fortran library directory for libraries.
[ -n "$GPFLIB" ] && echo "-libpath \"$(cygpath -w "$GPFLIB")\"" >>opt

#   Beautify.
u2d opt &>/dev/null


##  Define compilation and linking tasks.
##  ------------------------------------

gptask="$gprect/gptask.exe"
[ -f "$gptask" ] || itcess "Can't find gptask file \"$gptask\"."
"$gptask" ltg95 "$gbasestem" &>"gptask.stderr"\
    || itcess "Can't run gptask."
rm -f gptask.stderr
[ -f "opt95" ] || itcess "Can't find options file\"opt95\"."
unset gptask


##  Compile and link.
##  ----------------

lf95 @opt95 >"$gbasestem.stdout" 2>"$gbasestem.stderr"\
    || itcess "Can't compile and link \"$gbasestem\"."
rm -f -- "$gbasestem.stdout" "$gbasestem.stderr"


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

rm -f --\
    "$gbasestem"__[0-9][0-9].obj\
    "$gbasestem"__[0-9].obj\
    "$gbasestem"_.mod\
    "$gbasestem"_[cmuv][0-9][0-9].{mod,obj}\
    "$gbasestem"_[cmuv][0-9].{mod,obj}\
    opt95\
    "$gbasestem"__[0-9][0-9].for\
    "$gbasestem"__[0-9].for\
    opt
unset gprectwin gprect stacksize gbasestem gramrect
unset -f itcess
unset self
exit 0


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