#!/bin/sh

#   May 14/2008 - F.Majaess (Revised for sa/saiph, ib/dorval-ib)
#   Oct 16/2006 - F.Majaess (Revised for ma/maia, ns/naos)
#   Mar 01/2006 - F.Majaess (Revised for rg/rigel)
#   Jun 24/2003 - F.Majaess (Revised for az/azur and "openmp" support)
#   Feb 25/2002 - F.Majaess (Revised for SX6)
#   Feb 12/2002 - F.Majaess (Disable "hr/hires"; since lopgm/lopgmhr are quivalent)
#   Feb 20/2001 - F.Majaess (Allow overriding target platform via "rmtdest")
#   Jan 29/2001 - F.Majaess (Revised to support SXCROSS_KIT/SXF90_VER)
#   Feb 23/2000 - F.Majaess (Revised for kz/kaze and "sxf90")
#   Jun 14/1999 - F.Majaess (Added "modver" support)
#   Jan 21/1999 - F.Majaess (Added yo/yonaka)
#   Jul 14/1997 - F.Majaess (Revise for hiru)
#   May 06/1997 - F.Majaess (Add "multi" switch for SX4)
#   Mar 18/1997 - F.Majaess (Revise for group file ownership)
#   Apr 04/1996 - F.Majaess (revise for SX-4)
#   Dec 08/1995 - F.Majaess (Replace 'hostname' calls by "$HOSTID")
#   Oct 26/1995 - F.Majaess (Implement "nopack" & modify for IBM RS6000)
#   Jun 28/1995 - F.Majaess (Modify for Orion)
#   Feb 06/1995 - F.Majaess (Modify for SGI)
#   Nov 08/1994 - F. Majaess (Modify for sx3r)
#   Jul 08/1993 - F. Majaess (add "$LINKEXT" to library list)
#   Feb 09/1993 - E. Chan (Add hires 'hr' option)
#   Nov 23/1992 - E. Chan (List contents of '.L' files to printout)
#   Aug 21/1992 - E. Chan (Change FLOAT to DFLOAT, AIMAG to IMAG, and COMPLEX
#                          to COMPLEX*16 in code when compiling with "-r8" 
#                          on the MIPS)
#   Feb 26/1992 - E. Chan

#id genabs  - generate executables from one or more Fortran program source
#id           code files

#   AUTHOR  - E. Chan

#hd PURPOSE - "genabs" generates executables from files containing Fortran
#hd           source code (one program per file). Each program must have a
#hd           '.f' extension and should have a program statement on the 
#hd           first line specifying the name of the program. The name of 
#hd           the executable is normally obtained from the program statement.
#hd           However, if the program statement is not the first line, or it 
#hd           is missing, the name of the executable is obtained from the 
#hd           name of the source file with the '.f' extension removed.
#hd           The name of the executable is always converted to lower case.
 
#pr PARAMETERS:
#pr
#pr   POSITIONAL
#pr
#pr     fn1 ... fnm = list of m file names containing source code
#pr                   (filenames may contain path information)
#pr
#pr   PRIMARY
#pr  
#pr             dir = full pathname of the directory on the target
#pr                   machine in which the executables are to be 
#pr                   placed (="$RTEXTBLS/lopgm")
#pr                   [Disabled:(="$RTEXTBLS/lopgm$hires"; 
#pr                               $hires = hr if specified)]
#pr
#pr          modver = Model libary version to target for linking.
#pr                   (=$DEFMODL).
#pr
#pr      fil_grpname= User has the option of specifying one of
#pr                   'ccrn_shr/ccrn_stf/ccrn_net/ccrn_rcm/ccrn_mam/ccrn_gst'
#pr                   group ownership on the generated executable(s).
#pr                   (=$DEFFGRP).
#pr
#pr
#pr   SECONDARY
#pr
#pr          nolist = switch to turn off compiled listings 
#pr                   (=no/yes)
#pr
#pr          nopack = switch used to disable packing by enforcing 
#pr                   packing density of 1 via linking to revised version
#pr                   of "recpk2" subroutine (=no/yes)
#pr
#pr              hr = (No longer valid since "lopgm" and "lopgmhr" 
#pr                    are equivalent; script will abort if specified)
#pr                   switch to enable generation of high resolution
#pr                   set of executables 
#pr
#pr   Note: If neither "float1","float2" [nor "openmp"] 
#pr         (see below) is specified, the default
#pr         for hadar/spica/pollux/castor/mez: 64-bits real/ 32_bits integer;
#pr         (ie. whatever mode "$F77" is set to).
#pr
#pr          openmp = switch to compile the code in "openmp" 32-bits (real/integer)
#pr                   mode if "float1=yes", otherwise 64-bits mode. This switch is
#pr                   currently valid only for hadar,spica,pollux,castor,mez,
#pr                   destinations as well as under AIX O/S.
#pr                   (='no'/'yes')
#pr          float1 = switch to compile the code in 32-bits (real/integer) mode.
#pr                   It can be used in conjunction with "openmp=yes".
#pr                   (='no'/'yes')
#pr          float2 = switch to compile the code in 64-bits (real/integer)
#pr                   It's intended to be used on "hadar/ha","spica/sp","pollux/po",
#pr                   "castort/ca","mez/mz" or other suitable targets.
#pr                   (='no'/'yes')
#pr        noxlfimp = switch valid only under AIX on one of the IBM clusters at CMC.
#pr                   It's used to aid in getting better debugging trace in case of
#pr                   executable abort.
#pr                   (='no'/'yes')
#pr
#pr      p5lib = switch valid only under AIX on one of the IBM clusters at CMC.
#pr              It's used to allow linking to P5 captured "essl/mass/blas" 
#pr              libraries instead of the default set under "/usr/lib".
#pr              (='no'/'yes')
#pr

#ex EXAMPLE:
#ex   
#ex     genabs file1.f $HOME/source$OSbin/file2.f fil_grpname='ccrn_shr' \
#ex                      dir=/users/tor/acrn/rec/bin modver=gcm6u
#ex     
#ex     The above example generates two executables and places them in  
#ex     the directory /users/tor/acrn/rec/bin with "ccrn_shr" group ownership. 
#ex     The linking targets the appropriate "gcm6u" archive library.
#ex   

#    Temporary sample for IBM@CMC:
#    genabs add.f  dir=`pwd` xlfver=xlf10103 lvcode_dir=new_lvcode_dir98 nolist

#  * Reset field separators (otherwise those defined in the parent process
#  * will be used and these may cause problems if they include special 
#  * characters used in this script). 
IFS=' '
export IFS
#  * Initialize for the current directory.

TCwd=`pwd`

#  * Obtain the path name for the executables and generate
#  * the list of source files to be processed. 

for arg in $@ 
do
  case $arg in 
       -*) set $arg                     ;;
      *=*) eval $arg                    ;;
       hr) hires='hr'                   ;;
   nopack) nopack='yes'                 ;;
   nolist) nolist='yes'                 ;;
   float1) float1='yes'                 ;;
   float2) float2='yes'                 ;;
   openmp) openmp='yes'                 ;;
   noxlfimp) noxlfimp='yes'             ;;
    p5lib) p5lib='yes'                  ;;
        *) file_list="$file_list $arg"          
  esac
done
# if [ "$hires" = 'hr' ] ; then
if [ -n "$hires" ] ; then
   echo "" ; echo "  Abort in GENABS: Sorry, 'hr' switch is not valid! Please, correct..."
   exit 1
fi
#  * Set the defaults including setting the default path to the official 
#  * CCRN directory for executables.

modver=${modver:=$DEFMODL}
OS=${OS:=`uname -s`}
nolist=${nolist:='no'}
nopack=${nopack:='no'}
float1=${float1:='no'}
float2=${float2:='no'}
openmp=${openmp:='no'}
noxlfimp=${noxlfimp:='no'}
if [ "$noxlfimp" = 'on' ] ; then
  noxlfimp='yes'
elif [ "$noxlfimp" = 'off' ] ; then
  noxlfimp='no'
fi
p5lib=${p5lib:='no'}
if [ "$p5lib" = 'on' ] ; then
 p5lib='yes'
elif [ "$p5lib" = 'off' ] ; then
 p5lib='no'
fi

if [ "$SITE_ID" = 'Dorval' ] ; then
 HOSTHW=${HOSTHW:=`uname -m`}
fi
# If running at Dorval site, allow group ownership option for
# the executables ...

if [ "$SITE_ID" = 'Dorval' ] ; then
 fil_grpname='ccrn_shr'
else
 unset fil_grpname
fi

if test "$OS" != 'AIX' -a "$OS" != 'Linux' -a "$float2" != 'no'  ; then
  echo "" ; echo "  genabs: Sorry, float2 switch is not a valid option with targeted OS !"
  exit 3
fi
if test "$openmp" != 'no' -a "$OS" != 'AIX' -a "$OS" != 'Linux' ; then
  echo "" ; echo "  genabs: Sorry, openmp switch is not a valid option with targeted OS !"
  exit 4
fi
if test "$noxlfimp" = 'yes' -a "$OS" != 'AIX' ; then
  echo "" ; echo "  genabs: Sorry, noxlfimp=$noxlfimp can only be used under AIX!"
  exit 4
fi
if test "$p5lib" = 'yes' -a "$OS" != 'AIX' ; then
  echo "" ; echo "  genabs: Sorry, p5lib=$p5lib can only be used under AIX!"
  exit 4
fi

if [ "$SITE_ID" = 'Dorval' -a "$OS" = 'AIX' ] ; then
 xlfver=${xlfver:="$XLFVER"}
#xlfver=${xlfver:='xlf12104'}
 # HOSTIDf=`echo $HOSTID | cut -c 1-3`
 HOSTIDf=`echo $HOSTID | sed -e 's/eccc.*-ppp/cs/g' -e 's/^ppp/cs/g' | cut -c 1-3`
#if [ "$HOSTIDf" = 'c8f' -o "$HOSTIDf" = 'c1f' -o "$HOSTIDf" = 'c1h' -o "$HOSTIDf" = 'c1r' -o "$HOSTIDf" = 'c1s' -o "$HOSTIDf" = 'c2f' -o "$HOSTIDf" = 'c2h' -o "$HOSTIDf" = 'c2r' -o "$HOSTIDf" = 'c2s' ] ; then
# xlfver=${xlfver:='xlf13108'}
#else
# xlfver=${xlfver:='xlf12104'}
#fi
 if [ "$xlfver" = 'xlf10103' ] ; then
  echo " GENABS: Altered XLF target to version 10.1.0.3"
 elif [ "$xlfver" = 'xlf12104' ] ; then
  echo " GENABS: Altered XLF target to version 12.1.0.4"
 elif [ "$xlfver" = 'xlf13108' ] ; then
  echo " GENABS: Altered XLF target to version 13.1.0.5"
 else
  xlfver=""
 fi
else
 xlfver=""
fi

if [ -n "$lvcode_dir" ] ; then
 if [ "$SITE_ID" = 'Dorval' -o "$SITE_ID" = 'DrvlSC' ] ; then
  if [ ! -d "$CCRNSRC/$lvcode_dir/." ] ; then
   echo "" ; echo "  genabs: $CCRNSRC/$lvcode_dir is not a valid subdirectory!"
   exit 5  
  fi
 else
  echo "genabs: lvcode_dir=$lvcode_dir is not valid on local site and will be ignored"
  lvcode_dir=''
 fi
fi

if [ "$float1" = 'no' -a "$openmp" = 'no' ] ; then
  if [ "$float2" = 'no' ] ; then
   dir=${dir:=$RTEXTBLS/lopgm}
   unset float
  else
   dir=${dir:=$RTEXTBLS/lopgm_float2}
   float='_float2'
  fi
elif [ "$float1" = 'no' ] ; then
  dir=${dir:=$RTEXTBLS/lopgm_openmp}
  float='_openmp'
elif [ "$openmp" = 'no' ] ; then
  dir=${dir:=$RTEXTBLS/lopgm_float1}
  float='_float1'
else
  dir=${dir:=$RTEXTBLS/lopgm_float1_openmp}
  float='_float1_openmp'
  if [ "$modver" = 'gcm6u' ] ; then
#  modver='gcm15f'
   :
  fi
fi

if [ "$SITE_ID" = 'Dorval' -a "$OS" = 'AIX' ] ; then
  # HOSTIDf=`echo $HOSTID | cut -c 1-3`
  HOSTIDf=`echo $HOSTID | sed -e 's/eccc.*-ppp/cs/g' -e 's/^ppp/cs/g' | cut -c 1-3`
  F77Tmp="F77""$float"
  eval "F77Tmp=${F77Tmp}"
  if [ "$xlfver" = 'xlf10103' ] ; then
    eval "F77${float}=\`echo \${$F77Tmp} | sed -e 's/-qfloat=nans:/-qfloat=/g' -e 's/-qflttrap=nanq:/-qflttrap=/g'\`"
  elif [ "$xlfver" = 'xlf12104' ] ; then
    if [ "\$HOSTIDf" = 'c8f' -o "\$HOSTIDf" = 'c1f' -o "\$HOSTIDf" = 'c1h' -o "\$HOSTIDf" = 'c1r' -o "\$HOSTIDf" = 'c1s' -o "\$HOSTIDf" = 'c2f' -o "\$HOSTIDf" = 'c2h' -o "\$HOSTIDf" = 'c2r' -o "\$HOSTIDf" = 'c2s' ] ; then
     # eval "F77${float}=\`echo \${$F77Tmp} | sed -e 's/-qfloat=rrm:/-qfloat=nans:rrm:/g' -e 's/ -qxflag=ngenstub / /' -e 's/ -qfullpath / /' -e 's/ -qreport / /' -e 's/ -qnoescape / -qnoescape -qxflag=ngenstub /' -e 's/ -qdbg / -qdbg -qfullpath /' -e 's/:nofold /:nofold -qreport /'\`"
     eval "F77${float}=\`echo \${$F77Tmp} | sed -e 's/-qspillsize=[0-9]* /-qspillsize=32648 /' -e 's/-qfloat=rrm:/-qfloat=nans:rrm:/g' -e 's/ -qxflag=ngenstub / /' -e 's/ -qfullpath / /' -e 's/ -qreport / /' -e 's/ -qnoescape / -qnoescape -qxflag=ngenstub /' -e 's/ -qdbg / -qdbg -qfullpath /' -e 's/:nofold /:nofold -qreport /' -e 's/-qarch=auto /-qarch=pwr5x /' -e 's/-qarch=pwr7 /-qarch=pwr5x /' -e 's/-qtune=auto /-qtune=pwr5 /' -e 's/-qtune=pwr7 /-qtune=pwr5 /' -e 's/-qsimd=auto / /' \`"
    else
     eval "F77${float}=\`echo \${$F77Tmp} | sed -e 's/-qfloat=rrm:/-qfloat=nans:rrm:/g' -e 's/ -qxflag=ngenstub / /' -e 's/ -qfullpath / /' -e 's/ -qreport / /' -e 's/ -qnoescape / -qnoescape -qxflag=ngenstub /' -e 's/ -qdbg / -qdbg -qfullpath /' -e 's/:nofold /:nofold -qreport /'\`"
    fi
  elif [ "$xlfver" = 'xlf13108' ] ; then
    eval "F77${float}=\`echo \${$F77Tmp} | sed -e 's/-qfloat=rrm:/-qfloat=nans:rrm:/g' -e 's/ -qxflag=ngenstub / /' -e 's/ -qfullpath / /' -e 's/ -qreport / /' -e 's/ -qnoescape / -qnoescape -qxflag=ngenstub /' -e 's/ -qdbg / -qdbg -qfullpath /' -e 's/:nofold /:nofold -qreport /'\`"
# else
#  echo "" ; echo "  genabs: Sorry, a valid XLF version must be specified via 'xlfver' parameter!"
#  touch haltit
# (echo "" ; echo "  genabs: Sorry, a valid XLF version must be specified via 'xlfver' parameter!") >> haltit
#  exit 6 
  fi
  if [ "$noxlfimp" = 'yes' ] ; then
   eval "F77${float}=\`echo \${F77${float}} | sed -e 's/:imprecise//g' | sed -n -e 's/:imp//g' -e 's/-bnoquiet//g' -e '1,\$p'\`"
  fi
fi
export float
F77cut="F77""${float}"
eval "F77cut=\${${F77cut}}"
F77cut=`echo "${F77cut}" | $AWK '{ print $1 ; }'`
F77cut=`basename ${F77cut} | sed -e 's/^.*\(pgf\).*$/\1/g'`
if [ ! -d "${dir}/." ] ; then
  mkdir -m 755 $dir  2>/dev/null
  if [ ! -d "${dir}/." ] ; then
    echo "" ; echo "  genabs: Unable to create $dir subdirectory!"
    exit 5
  fi
  if [ "$SITE_ID" = 'Dorval' ] ; then
    chgrp $fil_grpname $dir
  fi
  chmod a+rx $dir
fi

spath=`expr $dir : '\(.*\)/'`
sdir=`expr //$dir : '.*/\(.*\)'`

#  * Move into a temporary directory to execute the script.

Stamp="${HOSTID}_"`date +%Y%j%H%M%S`
if [ "$OS" = 'Linux' ] ; then
  mkdir -m 755 /tmp/tmp_genabs_$$_$Stamp
  cd /tmp/tmp_genabs_$$_$Stamp
else
  mkdir -m 755 tmp_genabs_$$_$Stamp
  cd tmp_genabs_$$_$Stamp
fi

#  * Enable production of compiler listing output ...

if [ "$OS" = 'AIX' ] ; then
 lstswtch=' -qsource '
 AIX_LOCAL=$LOCAL
elif [ "$OS" = 'Linux' -a \( "$SITE_ID" = 'ORNS' -o "$F77cut" = 'pgf' \) ] ; then
 lstswtch=' -Mlist '
 if [ "$SITE_ID" = 'Dorval' ] ; then
 #pgf90_722 -V || :
  $PGF90 -V || :
 fi
fi

# Setup needed parameter for generating the executable(s) ...

#  * Force nopacking in the executable if so requested ....

if [ "$nopack" = 'yes' ] ; then
 pkdoto="$RTEXTBLS/m_recpk2${float}.o"
fi

#  * Compile and link.

if [ "$float1" = 'no' -a "$float2" = 'no' -a "$openmp" = 'no' ] ; then
  if [ -n "$modver" ] ; then
   LOMODEL_ver=`echo $LOMODEL | eval sed 's/_model/_model_${modver}/g'`
  else
   LOMODEL_ver="$LOMODEL"
  fi
  if [ "$xlfver" = 'xlf10103' ] ; then
   LOPLOT=`echo $LOPLOT | sed -e 's/\.a/_xlf10103.a/'`
   LODIAG=`echo $LODIAG | sed -e 's/\.a/_xlf10103.a/'`
   LOCOMM=`echo $LOCOMM | sed -e 's/\.a/_xlf10103.a/'`
   LOMODEL_ver=`echo $LOMODEL_ver | sed -e 's/\.a/_xlf10103.a/'`
  elif [ "$xlfver" = 'xlf12104' ] ; then
   LOPLOT=`echo $LOPLOT | sed -e 's/\.a/_xlf12104.a/'`
   LODIAG=`echo $LODIAG | sed -e 's/\.a/_xlf12104.a/'`
   LOCOMM=`echo $LOCOMM | sed -e 's/\.a/_xlf12104.a/'`
   LOMODEL_ver=`echo $LOMODEL_ver | sed -e 's/\.a/_xlf12104.a/'`
  elif [ "$xlfver" = 'xlf13108' ] ; then
   LOPLOT=`echo $LOPLOT | sed -e 's/\.a/_xlf13108.a/'`
   LODIAG=`echo $LODIAG | sed -e 's/\.a/_xlf13108.a/'`
   LOCOMM=`echo $LOCOMM | sed -e 's/\.a/_xlf13108.a/'`
   LOMODEL_ver=`echo $LOMODEL_ver | sed -e 's/\.a/_xlf13108.a/'`
  fi
elif [ "$float1" = 'no' -a "$openmp" = 'no' ] ; then
  if [ -n "$modver" ] ; then
   LOMODEL_ver=`echo $LOMODEL_float2 | eval sed 's/_model/_model_${modver}/g'`
  else
   LOMODEL_ver="$LOMODEL_float2"
  fi
  if [ "$xlfver" = 'xlf10103' ] ; then
   LOPLOT_float2=`echo $LOPLOT_float2 | sed -e 's/\.a/_xlf10103.a/'`
   LODIAG_float2=`echo $LODIAG_float2 | sed -e 's/\.a/_xlf10103.a/'`
   LOCOMM_float2=`echo $LOCOMM_float2 | sed -e 's/\.a/_xlf10103.a/'`
   LOMODEL_ver=`echo $LOMODEL_ver | sed -e 's/\.a/_xlf10103.a/'`
  elif [ "$xlfver" = 'xlf12104' ] ; then
   LOPLOT_float2=`echo $LOPLOT_float2 | sed -e 's/\.a/_xlf12104.a/'`
   LODIAG_float2=`echo $LODIAG_float2 | sed -e 's/\.a/_xlf12104.a/'`
   LOCOMM_float2=`echo $LOCOMM_float2 | sed -e 's/\.a/_xlf12104.a/'`
   LOMODEL_ver=`echo $LOMODEL_ver | sed -e 's/\.a/_xlf12104.a/'`
  elif [ "$xlfver" = 'xlf13108' ] ; then
   LOPLOT_float2=`echo $LOPLOT_float2 | sed -e 's/\.a/_xlf13108.a/'`
   LODIAG_float2=`echo $LODIAG_float2 | sed -e 's/\.a/_xlf13108.a/'`
   LOCOMM_float2=`echo $LOCOMM_float2 | sed -e 's/\.a/_xlf13108.a/'`
   LOMODEL_ver=`echo $LOMODEL_ver | sed -e 's/\.a/_xlf13108.a/'`
  fi
elif [ "$float1" = 'no' ] ; then
  if [ -n "$modver" ] ; then
   LOMODEL_ver=`echo $LOMODEL_openmp | eval sed 's/_model/_model_${modver}/g'`
  else
   LOMODEL_ver="$LOMODEL_openmp"
  fi
  if [ "$xlfver" = 'xlf10103' ] ; then
   LOPLOT_openmp=`echo $LOPLOT_openmp | sed -e 's/\.a/_xlf10103.a/'`
   LODIAG_openmp=`echo $LODIAG_openmp | sed -e 's/\.a/_xlf10103.a/'`
   LOCOMM_openmp=`echo $LOCOMM_openmp | sed -e 's/\.a/_xlf10103.a/'`
   LOMODEL_ver=`echo $LOMODEL_ver | sed -e 's/\.a/_xlf10103.a/'`
  elif [ "$xlfver" = 'xlf12104' ] ; then
   LOPLOT_openmp=`echo $LOPLOT_openmp | sed -e 's/\.a/_xlf12104.a/'`
   LODIAG_openmp=`echo $LODIAG_openmp | sed -e 's/\.a/_xlf12104.a/'`
   LOCOMM_openmp=`echo $LOCOMM_openmp | sed -e 's/\.a/_xlf12104.a/'`
   LOMODEL_ver=`echo $LOMODEL_ver | sed -e 's/\.a/_xlf12104.a/'`
  elif [ "$xlfver" = 'xlf13108' ] ; then
   LOPLOT_openmp=`echo $LOPLOT_openmp | sed -e 's/\.a/_xlf13108.a/'`
   LODIAG_openmp=`echo $LODIAG_openmp | sed -e 's/\.a/_xlf13108.a/'`
   LOCOMM_openmp=`echo $LOCOMM_openmp | sed -e 's/\.a/_xlf13108.a/'`
   LOMODEL_ver=`echo $LOMODEL_ver | sed -e 's/\.a/_xlf13108.a/'`
  fi
elif [ "$openmp" = 'no' ] ; then
  if [ -n "$modver" ] ; then
   LOMODEL_ver=`echo $LOMODEL_float1 | eval sed 's/_model/_model_${modver}/g'`
  else
   LOMODEL_ver="$LOMODEL_float1"
  fi
  if [ "$OS" = 'AIX' ] ; then
   OBJECT_MODE='32' ; export OBJECT_MODE
  fi
  if [ "$xlfver" = 'xlf10103' ] ; then
   LOPLOT_float1=`echo $LOPLOT_float1 | sed -e 's/\.a/_xlf10103.a/'`
   LODIAG_float1=`echo $LODIAG_float1 | sed -e 's/\.a/_xlf10103.a/'`
   LOCOMM_float1=`echo $LOCOMM_float1 | sed -e 's/\.a/_xlf10103.a/'`
   LOMODEL_ver=`echo $LOMODEL_ver | sed -e 's/\.a/_xlf10103.a/'`
  elif [ "$xlfver" = 'xlf12104' ] ; then
   LOPLOT_float1=`echo $LOPLOT_float1 | sed -e 's/\.a/_xlf12104.a/'`
   LODIAG_float1=`echo $LODIAG_float1 | sed -e 's/\.a/_xlf12104.a/'`
   LOCOMM_float1=`echo $LOCOMM_float1 | sed -e 's/\.a/_xlf12104.a/'`
   LOMODEL_ver=`echo $LOMODEL_ver | sed -e 's/\.a/_xlf12104.a/'`
  elif [ "$xlfver" = 'xlf13108' ] ; then
   LOPLOT_float1=`echo $LOPLOT_float1 | sed -e 's/\.a/_xlf13108.a/'`
   LODIAG_float1=`echo $LODIAG_float1 | sed -e 's/\.a/_xlf13108.a/'`
   LOCOMM_float1=`echo $LOCOMM_float1 | sed -e 's/\.a/_xlf13108.a/'`
   LOMODEL_ver=`echo $LOMODEL_ver | sed -e 's/\.a/_xlf13108.a/'`
  fi
else
  if [ -n "$modver" ] ; then
   LOMODEL_ver=`echo $LOMODEL_float1_openmp | eval sed 's/_model/_model_${modver}/g'`
  else
   LOMODEL_ver="$LOMODEL_float1_openmp"
  fi
  if [ "$xlfver" = 'xlf10103' ] ; then
   LOPLOT_float1_openmp=`echo $LOPLOT_float1_openmp | sed -e 's/\.a/_xlf10103.a/'`
   LODIAG_float1_openmp=`echo $LODIAG_float1_openmp | sed -e 's/\.a/_xlf10103.a/'`
   LOCOMM_float1_openmp=`echo $LOCOMM_float1_openmp | sed -e 's/\.a/_xlf10103.a/'`
   LOMODEL_ver=`echo $LOMODEL_ver | sed -e 's/\.a/_xlf10103.a/'`
  elif [ "$xlfver" = 'xlf12104' ] ; then
   LOPLOT_float1_openmp=`echo $LOPLOT_float1_openmp | sed -e 's/\.a/_xlf12104.a/'`
   LODIAG_float1_openmp=`echo $LODIAG_float1_openmp | sed -e 's/\.a/_xlf12104.a/'`
   LOCOMM_float1_openmp=`echo $LOCOMM_float1_openmp | sed -e 's/\.a/_xlf12104.a/'`
   LOMODEL_ver=`echo $LOMODEL_ver | sed -e 's/\.a/_xlf12104.a/'`
  elif [ "$xlfver" = 'xlf13108' ] ; then
   LOPLOT_float1_openmp=`echo $LOPLOT_float1_openmp | sed -e 's/\.a/_xlf13108.a/'`
   LODIAG_float1_openmp=`echo $LODIAG_float1_openmp | sed -e 's/\.a/_xlf13108.a/'`
   LOCOMM_float1_openmp=`echo $LOCOMM_float1_openmp | sed -e 's/\.a/_xlf13108.a/'`
   LOMODEL_ver=`echo $LOMODEL_ver | sed -e 's/\.a/_xlf13108.a/'`
  fi
  # if [ "$OS" = 'AIX' ] ; then
  #  OBJECT_MODE='32' ; export OBJECT_MODE
  # fi
fi 

if [ "$SITE_ID" = 'Dorval' -a "$OS" = 'AIX' ] ; then
 if [ "$xlfver" = 'xlf10103' ] ; then
   . suppress_compilers_setup
   . xlf10103_setup
 elif [ "$xlfver" = 'xlf12104' ] ; then
   . suppress_compilers_setup
   . xlf12104_setup
 elif [ "$xlfver" = 'xlf13108' ] ; then
   . suppress_compilers_setup
   . xlf13108_setup
# else
#  . suppress_compilers_setup
#  . xlf12104_setup
 fi
 xlf90 -qversion
fi
First_exec='yes'
#  * Loop over all source files.

for name in $file_list
do            

  #  * Extract name and path information from the source filename. The 
  #  * default directory containing the source file is set to the parent
  #  * directory. 

  path=`expr $name : '\(.*\)/'`
  srcfile=`expr //$name : '.*/\(.*\)'`

  path=${path:=${TCwd}}

  #  * Extract the name of program from the program statement on the
  #  * first line of the source file.  If a valid program statement
  #  * is not available on the first line, then the name is extracted
  #  * from the filename. 

  line=`head -1 $path/$srcfile`
  pgm=`expr "$line" : '.*PROGRAM *\([^ ]*\)' | tr "[A-Z]" "[a-z]"`
  pgm=${pgm:=`expr "$srcfile" : '\(.*\)\.f' | tr "[A-Z]" "[a-z]"`}  

  if [ -n "$pgm" ] ; then

    \cp $path/$srcfile ${pgm}.f
    chmod u+w ${pgm}.f ; chmod a+r ${pgm}.f
    if [ -n "$lvcode_dir" ] ; then
     cat $CCRNSRC/$lvcode_dir/lv*.f >> ${pgm}.f
    fi

    #  * Possibly massage the source code ...

    fix77 ${pgm}.f

    if [ "$First_exec" = 'yes' ] ; then
     set -x
    fi
    #  * Compile and link.

    if [ "$float1" = 'no' -a "$float2" = 'no' -a "$openmp" = 'no' ] ; then
      if [ "$OS" = 'AIX' -a "$p5lib" = 'yes' ] ; then
        LOCOMM=`echo $LOCOMM | sed -e 's/ \/usr\/lib\/lib/ \/home\/crb_ccrn\/pollux\/acrn\/src\/plib\/p5lib\/lib/g' | sed -e 's/massvp[1-9]/massvp4/g'`
      fi
      $F77 $lstswtch -o $pgm ${pgm}.f $pkdoto \
              $LOMODEL_ver $LOPLOT $LODIAG $LOCOMM \
              $AIX_LOCAL $LINKNCAR $LINKEXT
    elif [ "$float1" = 'no' -a "$openmp" = 'no' ] ; then
      if [ "$OS" = 'AIX' -a "$p5lib" = 'yes' ] ; then
        LOCOMM_float2=`echo $LOCOMM_float2 | sed -e 's/ \/usr\/lib\/lib/ \/home\/crb_ccrn\/pollux\/acrn\/src\/plib\/p5lib\/lib/g' | sed -e 's/massvp[1-9]/massvp4/g'`
      fi
      $F77_float2 $lstswtch -o $pgm ${pgm}.f $pkdoto \
              $LOMODEL_ver $LOPLOT_float2 $LODIAG_float2 $LOCOMM_float2 \
              $LINKNCAR_float2 $LINKEXT_float2
    elif [ "$float1" = 'no' ] ; then
      if [ "$OS" = 'AIX' -a "$p5lib" = 'yes' ] ; then
        LOCOMM_openmp=`echo $LOCOMM_openmp | sed -e 's/ \/usr\/lib\/lib/ \/home\/crb_ccrn\/pollux\/acrn\/src\/plib\/p5lib\/lib/g' | sed -e 's/massvp[1-9]/massvp4/g'`
      fi
      $F77_openmp $lstswtch -o $pgm ${pgm}.f $pkdoto \
              $LOMODEL_ver $LOPLOT_openmp $LODIAG_openmp $LOCOMM_openmp \
              $LINKNCAR_openmp $LINKEXT_openmp
    elif [ "$openmp" = 'no' ] ; then
      if [ "$OS" = 'AIX' -a "$p5lib" = 'yes' ] ; then
        LOCOMM_float1=`echo $LOCOMM_float1 | sed -e 's/ \/usr\/lib\/lib/ \/home\/crb_ccrn\/pollux\/acrn\/src\/plib\/p5lib\/lib/g' | sed -e 's/massvp[1-9]/massvp4/g'`
      fi
      $F77_float1 $lstswtch -o $pgm ${pgm}.f $pkdoto \
              $LOMODEL_ver $LOPLOT_float1 $LODIAG_float1 $LOCOMM_float1 \
              $LINKNCAR_float1 $LINKEXT_float1
    else
      if [ "$OS" = 'AIX' -a "$p5lib" = 'yes' ] ; then
        LOCOMM_float1_openmp=`echo $LOCOMM_float1_openmp | sed -e 's/ \/usr\/lib\/lib/ \/home\/crb_ccrn\/pollux\/acrn\/src\/plib\/p5lib\/lib/g' | sed -e 's/massvp[1-9]/massvp4/g'`
      fi
      $F77_float1_openmp $lstswtch -o $pgm ${pgm}.f $pkdoto \
              $LOMODEL_ver $LOPLOT_float1_openmp $LODIAG_float1_openmp $LOCOMM_float1_openmp \
              $LINKNCAR_float1_openmp $LINKEXT_float1_openmp
    fi 
    if [ "$First_exec" = 'yes' ] ; then
     set +x
     First_exec='no'
    fi


    #  * If successful, put executable in the appropriate directory and set
    #  * the file permissions.

    if [ "$?" -eq 0 ] ; then
      if [ "$SITE_ID" = 'Dorval' ] ; then
       chgrp $fil_grpname $pgm
      fi
      chmod 755 $pgm
      mv $pgm $dir
      trnsfr_file_list="$trnsfr_file_list $pgm"
    else
      echo "Error: program $pgm not generated"
    fi

  else 

    echo "Error: cannot extract program name from source file $srcfile"
    echo 
    echo "       --- must have a valid program statement on the ---   "
    echo "       --- first line of the file or the filename     ---   "
    echo "       --- must contain a .f extension                ---   "

  fi

done


#  * Echo compiled listings to standard output  ...

if [ "$nolist" = 'no' -a \( "$OS" = 'AIX' -o \( "$OS" = 'Linux' -a \( "$SITE_ID" = 'ORNS' -o "$F77cut" = 'pgf' \) \) \)  ] ; then
  for arg in *.lst
  do
    echo 
    cat $arg
  done
fi

#  * Move back into parent directory and clean up temporary directory
#  * before exiting from script.

cd ..
\rm -r tmp_genabs_$$_$Stamp

#  * Move into the original location.

cd $TCwd

exit
