#!/bin/sh
OPTIONS="$8"
if [ "$OPTIONS" = "V" ]; then
    set -v -x
fi
#
# @(#)$Id: gmodule.in 5797 2010-04-07 16:20:46Z Freddie Akeroyd $
#
# gmodule.  Generated from gmodule.in by configure.
#
# Purpose:	Compilation procedure for external user programs (MODULES)
# Author:	Freddie Akeroyd, ISIS Facility, RAL, UK
# Usage: 	gmodule  'source files' 'GENIE shared library' 
#                        'extra compiler flags' 'extra ld flags' 
#                        'function symbols' 'source code type' 
#                        'output filename' 'other options'
#
# If more than one FORTRAN file is specified, it must be quoted to 
# appear as one argument e.g. gmodule "a.f b.f" libgenie.so
#
prefix=/home/faa59/GENIE/installroot/usr/local
exec_prefix=${prefix}
bindir=${exec_prefix}/bin
LIBS="-lm "
host="i686-pc-linux-gnu"
LIBDIRS="-L/usr/local/hdf/lib"
LDFLAGS="-L/usr/local/hdf4/lib/ "
LIB_VER=1.2
LIB_MAJOR_VER=1
NAME_LIBRARY_SCRIPT="-Wl,-soname,libgenie.so.${LIB_MAJOR_VER}"
TYPE="$6"		# FORTRAN or C source code
OUT="$7"		# output file name for library
TMPDIR=${TMPDIR-/tmp}	# where to create temporary files	
if [ "$GENIE_INCLUDE_DIR" = "" ]; then
    GENIE_INCLUDE_DIR=${prefix}/genie/library/include
fi
COMPILE_FLAGS="$3 -I. -I${GENIE_INCLUDE_DIR}"
LINK_FLAGS=$4
# $2 is a passed GENIE_LIBRARY
if [ -r "$2" ]; then 
    GENIE_LIBRARY="$2"
else
    if [ -f ${GENIE_DIR}/genie.so.1.2 ]; then
        GENIE_LIBRARY=${GENIE_DIR}/genie.so.1.2
    else
        GENIE_LIBRARY=${prefix}/genie/genie.so.1.2
    fi
fi
#
# Any host specific bits 
#
case $host in
    *-hp-hpux*)
# On HP, if we give a full library path it embeds the name
# in the object - not useful!
	GENIE_LIBRARY=""
       	FORT77_FLAGS="-O +Z +es +ppu -K"	# for HP fort77 compiler
	SHARED=-b
	;;
    *-apple-darwin*)
	SYMBOL_PREFIX="_"
	SPECIAL_OS=darwin
	SHARED="-bundle -flat_namespace" #-undefined suppress
	;;
    *)
	SHARED=-shared
	;;
esac
#
if [ "$TYPE" = "C" ]; then
    COMPILE=gcc
    if [ $COMPILE = gcc -o $COMPILE = gcc296 -o $COMPILE = gcc32 ]; then COMPILE_FLAGS="$COMPILE_FLAGS -O -fPIC"; fi
else
# With f2c and gcc we need to pass some flags to the CC stage
    if [ gcc = gcc -o gcc = gcc296 -o gcc = gcc32 ]; then CFLAGS="-fPIC -O"; export CFLAGS; fi
    COMPILE=gfortran
    if [ $COMPILE = g77 ]; then
        COMPILE_FLAGS="$COMPILE_FLAGS -O -fPIC -fno-automatic -ffixed-line-length-132 -Wall -Wsurprising"
    elif [ $COMPILE = g95 ]; then
        COMPILE_FLAGS="$COMPILE_FLAGS -O -fPIC -ffixed-line-length-132 -Wall -fno-second-underscore -fstatic -fzero"
    elif [ $COMPILE = gfortran ]; then
        COMPILE_FLAGS="$COMPILE_FLAGS -O -fPIC -ffixed-line-length-132 -Wall -Wtabs -fno-automatic -finit-local-zero" # -fsecond-underscore
    elif [ $COMPILE = fort77 ]; then
       	COMPILE_FLAGS="$COMPILE_FLAGS $FORT77_FLAGS"
    elif [ `basename $COMPILE` = genie_f77 ]; then
	COMPILE_FLAGS="$COMPILE_FLAGS"
    else	# f77
# SGI needs the -I passed through to the FORTRAN compile phase with -Wf. A 
# -I on the command line will only resolve "#include" in a .f file and 
# not INCLUDE ' '
        COMPILE_FLAGS="$COMPILE_FLAGS -O -static -extend_source -Wf,-I${GENIE_INCLUDE_DIR}"
    fi
fi
#
LIBOBJ=""
for i in $1; do
    name=`expr $i : "\(.*\)\..*"`	# remove .f or .for suffix
    name=`basename $name`         # remove any directory path
    echo "*** Compiling \"$i\""
    ${COMPILE} ${COMPILE_FLAGS} -o ${TMPDIR}/${name}.o -c $i
    LIBOBJ="${LIBOBJ} ${TMPDIR}/${name}.o"
done
if [ -z "$OUT" ]; then
    so_file=${name}.so
else
    so_file=$OUT
fi
so_name=`basename $so_file`
if [ -r $so_file ]; then
    echo "*** Renaming \"$so_file\" to \"${so_file}_old\""
    mv -f $so_file ${so_file}_old
fi
#
echo "*** Making new shared library \"$so_file\""
if [ -w . -o -w ./so_locations ]; then
    so_locations=./so_locations
else
    so_locations=/tmp/so_locations.$$
fi
#####touch $so_locations
# -update_registry to /dev/null to avoid problems of not being able to create
# an so_locations file in the current directory
##### remove -Wl,-update_registry,${so_locations} and -update_registry ${so_locations}
if [ gcc = gcc -o gcc = gcc296 -o gcc = gcc32 ]; then
    gcc ${SHARED} -o ${so_file} -fPIC ${LDFLAGS} ${LIBDIRS} ${LIBOBJ} ${LINK_FLAGS} ${GENIE_LIBRARY} ${LIBS} # GNU ld
else
    ld ${SHARED} -o ${so_file} ${NAME_LIBRARY_SCRIPT} ${so_name} ${LDFLAGS} ${LIBDIRS} ${LIBOBJ} ${LINK_FLAGS} ${GENIE_LIBRARY} ${LIBS}
fi
[ -z "$LIBOBJ" ] || rm -f $LIBOBJ
# build symbolic link if doing Linux versioning
#if [ "${so_file}" != "${so_name}" ]; then
#    ln -fs ${so_file} ${so_name}
#fi
echo "*** Module now compiled - load with:  MODULE/LOAD \"$so_file\""
