#! /bin/csh -f
#
#  16 Apr 02 - MWS - C-shell script to compile one MODULE of GAMESS.
#
#------------------------
#  23 Mar 1993 - MH
#  Modified to be part of CHARMM make procedure
#------------------------
#  24 Jul 1997 - MH
#  Modified for automatic creation for various systems
#  from CHARMM install.com
#------------------------
#  15 Jun 2002 - MH
#  Upgrade to current comp script
#
#  Invoke this script by 'comp MODULE >& MODULE.log &'.
#  Sometimes systems with the Rand MH installed already have a "comp"
#  command as part of the system.  The best fix for this is to put the 
#  current path (.) ahead of all system paths in your PATH variable.
#
#        ----- The next two lines require your attention -----
#  1. Choose your machine, the legal values for TARGET are
#      compaq-axp,compaq-shmem,convex-spp,cray-pvp,cray-t3e,
#      fuji-ap,fuji-vpp,hitachi,hp9000,ibm32,ibm64,ibm32-sp,ibm64-sp,
#      linux-pc,macosx,necsx,sgi32,sun,win32
#  2. Choose your GAMESS directory tree.
#
set TARGET=linux-pc
#
unset echo
#set MODULE=$1
set   MODULE=$1:t
set   MODULE=$MODULE:r
set   ENVMT=charmm
set   chmsrc=$chmsrc/gamint
set   PARALLEL=true
set ACTONLY=$2   # this argument is almost always omitted
if (null$MODULE == null) then
   echo No compile module name given...
   exit
endif
#
echo ======================== $MODULE ==============================
date
#
#   ----- Is this a vector machine? -----
#
set VECTORIZE=false
if ($TARGET == cray-pvp) set VECTORIZE=true
if ($TARGET == fuji-vpp) set VECTORIZE=true
if ($TARGET == necsx)    set VECTORIZE=true
#
#   ----- on sequential scalar machines only -----
#   Are level 3 BLAS in a system library?
#   If so, matrix multiplies in MTHLIB.SRC can use DGEMM calls
#
set BLAS3=false
if  ($TARGET == compaq-shmem) set BLAS3=true
if (($TARGET == compaq-axp) && (-e /lib/libdxml.a)) set BLAS3=true
if  ($TARGET == cray-pvp)     set BLAS3=true
if  ($TARGET == cray-t3e)     set BLAS3=true
if  ($TARGET == fuji-vpp)     set BLAS3=true
if  ($TARGET == hitachi)      set BLAS3=true
if  ($TARGET == hp9000)       set BLAS3=true
if  ($TARGET == ibm32)        set BLAS3=true
if  ($TARGET == ibm64)        set BLAS3=true
if  ($TARGET == ibm32-sp)     set BLAS3=true
if  ($TARGET == ibm64-sp)     set BLAS3=true
if (($TARGET == linux-pc) && (-e /usr/local/bin/libblas-asci.a)) set BLAS3=true
if  ($TARGET == sgi32)        set BLAS3=true
if  ($TARGET == sgi64)        set BLAS3=true
if (($TARGET == sun)    && (-e /opt/SUNWspro/lib/libsunperf.so)) set BLAS3=true
#
if ($MODULE == mthlib) then
  if ($BLAS3 == true) then
    echo Compiling matrix multiply routines to use a system BLAS library...
  else
    echo FORTRAN matrix multiply routines will be used, not a BLAS library...
  endif
endif
#
#   ----- delete scalar code before compiling on vector systems -----
#   UNIX's 'ld' loader is incapable of loading two copies of the
#   object code for a routine, and simply using the first one.
#   So, if we are using VECTOR.SRC on this machine, we must split
#   up some of the modules, delete the scalar code for the routines
#   which are found in VECTOR.SRC, and then recombine the rest of
#   the code in that module.  Ugh!
#
if ($VECTORIZE == true) then
   rm -f *.f
   switch ($MODULE)
      case int2a:
         echo Deleting scalar code forms.f
         cp $chmsrc/gamess/int2a.src int2a.src
         fsplit int2a.src
         rm -f int2a.src
         if (-e forms.f) rm forms.f
         if (-e FORMS.f) rm FORMS.f
         cat *.f > int2a.src
         rm -f *.f
         set SRCDIR=.
         breaksw
      case grd2c:
         echo Deleting scalar code dspdfv, jdxyzv, jkgnmv, jkxyzv
         cp $chmsrc/gamess/grd2c.src grd2c.src
         fsplit grd2c.src
         rm -f grd2c.src
         if (-e dspdfv.f) rm -f dspdfv.f jdxyzv.f jkgnmv.f jkxyzv.f
         if (-e DSPDFV.f) rm -f DSPDFV.f JDXYZV.f JKGNMV.f JKXYZV.f
         cat *.f > grd2c.src
         rm -f *.f
         set SRCDIR=.
         breaksw
      case hss2b:
         echo Deleting scalar code jddspd.f
         cp $chmsrc/gamess/hss2b.src hss2b.src
         fsplit hss2b.src
         rm -f hss2b.src
         if (-e jddspd.f) rm -f jddspd.f
         if (-e JDDSPD.f) rm -f JDDSPD.f
         cat *.f > hss2b.src
         rm -f *.f
         set SRCDIR=.
         breaksw
      case eigen:
         echo Deleting scalar code gldiag.f
         cp source/eigen.src eigen.src
         fsplit eigen.src
         rm -f eigen.src
         if (-e gldiag.f) rm -f gldiag.f
         if (-e GLDIAG.f) rm -f GLDIAG.f
         cat *.f > eigen.src
         rm -f *.f
         set SRCDIR=.
         breaksw
      case mthlib:
         echo Deleting scalar code mrarbr, mrtrbr, mtarbr, tfsqb, tftri.
         cp source/mthlib.src mthlib.src
         fsplit mthlib.src
         rm -f mthlib.src
         if (-e mrarbr.f) rm -f mrarbr.f mrtrbr.f mtarbr.f tfsqb.f tftri.f
         if (-e MRARBR.f) rm -f MRARBR.f MRTRBR.f MTARBR.f TFSQB.f TFTRI.f
         cat *.f > mthlib.src
         rm -f *.f
         set SRCDIR=.
         breaksw
      default:
         set SRCDIR=$chmsrc/gamess
         breaksw
   endsw
else
   set SRCDIR=$chmsrc/gamess
#   if ($MODULE == blaswrap) set SRCDIR=misc
endif
#
#        optional SIMOMM method using Tinker MM program
set TINKER=false
if ($MODULE == Libtad)  set TINKER=true
if ($MODULE == Libteac) set TINKER=true
if ($MODULE == Libtedl) set TINKER=true
if ($MODULE == Libtemo) set TINKER=true
if ($MODULE == Libterx) set TINKER=true
if ($MODULE == Libtfi)  set TINKER=true
if ($MODULE == Libtjo)  set TINKER=true
if ($MODULE == Libtpr)  set TINKER=true
if ($MODULE == Libtsx)  set TINKER=true
if ($MODULE == Tdrive)  set TINKER=true
if ($MODULE == Tinkin)  set TINKER=true
if ($MODULE == Toys)    set TINKER=true
if ($TINKER == true) then
   set SRCDIR=.
   set OBJDIR=../object
   chdir tinker
   goto cmp
endif
set OBJDIR=object
#
#   ----- Does this module need to be activated? -----
#   Note that if "DOUBLE PRECISION" translates to more than a 64
#   bit floating point quantity, all modules must be passed into
#   the activator to ensure the source is made single precision.
#
if ($TARGET == cray-pvp) goto act
if ($TARGET == cray-t3e) goto act
if ($TARGET == ibm64)    goto act
if ($TARGET == ibm64-sp) goto act
if ($TARGET == necsx)    goto act
if ($TARGET == sgi64)    goto act
#
#   Any module with machine dependent code must be activated...
#
if ($MODULE == aldeci) goto act
if ($MODULE == ccsdt)  goto act
if ($MODULE == cphf)   goto act
if ($MODULE == cprohf) goto act
if ($MODULE == ddi)    goto act
if ($MODULE == ddishm) goto act
if ($MODULE == dft)    goto act
if ($MODULE == dftint) goto act
if ($MODULE == grd2a)  goto act
if ($MODULE == gugdga) goto act
if ($MODULE == gugdgb) goto act
if ($MODULE == gugdm2) goto act
if ($MODULE == gugem)  goto act
if ($MODULE == gugsrt) goto act
if ($MODULE == gvb)    goto act
if ($MODULE == hss2a)  goto act
if ($MODULE == int2a)  goto act
if ($MODULE == iolib)  goto act
if ($MODULE == lagran) goto act
if ($MODULE == local)  goto act
if ($MODULE == locpol) goto act
if ($MODULE == mccas)  goto act
if ($MODULE == mcqdpt) goto act
if ($MODULE == mcqud)  goto act
if ($MODULE == mcscf)  goto act
if ($MODULE == mctwo)  goto act
if ($MODULE == morokm) goto act
if ($MODULE == mp2)    goto act
if ($MODULE == mp2grd) goto act
if ($MODULE == ordint) goto act
if ($MODULE == rhfuhf) goto act
if ($MODULE == tdhf)   goto act
if ($MODULE == trans)  goto act
if ($MODULE == trfdm2) goto act
if ($MODULE == unport) goto act
if ($MODULE == vector) goto act
#
#   We may need to do a hack below on the matrix multiply routines
#
if (($BLAS3 == true) && ($MODULE == mthlib)) goto act
#
#   On Hitachi, zheev.src should be changed by a hack below
#
if (($MODULE == zheev) && ($TARGET == hitachi)) goto act
#
#   All other modules are straight FORTRAN, just copy them.
#
echo Copying source code, $MODULE.src does not require activation.
cp $SRCDIR/$MODULE.src $MODULE.f
goto cmp
#
#   ----- Choose which version is to be activated -----
#
act:
#
if ($TARGET == convex-spp) then
                          setenv MACHIN '*I32'
   if ($MODULE == ddi)    setenv MACHIN '*XXX'
   if ($MODULE == iolib)  setenv MACHIN '*UNX'
   if ($MODULE == unport) setenv MACHIN '*HP '
endif
if ($TARGET == cray-pvp) then
                          setenv MACHIN '*I64'
   if ($MODULE == ddi)    setenv MACHIN '*SOC'
   if ($MODULE == iolib)  setenv MACHIN '*UNX'
   if ($MODULE == unport) setenv MACHIN '*CRY'
   if ($MODULE == vector) setenv MACHIN '*CRY'
endif
#       the T3E has a special SHMEM replacement for ddi.src
if ($TARGET == cray-t3e) then
                          setenv MACHIN '*I64'
   if ($MODULE == ddishm) setenv MACHIN '*T3E'
   if ($MODULE == iolib)  setenv MACHIN '*UNX'
   if ($MODULE == unport) setenv MACHIN '*T3E'
endif
if ($TARGET == compaq-axp) then
                          setenv MACHIN '*I64'
   if ($MODULE == ddi)    setenv MACHIN '*SOC'
   if ($MODULE == iolib)  setenv MACHIN '*UNX'
   if ($MODULE == unport) setenv MACHIN '*DEC'
endif
#       SHMEM on Compaq has a special SHMEM replacement for ddi.src
if ($TARGET == compaq-shmem) then
                          setenv MACHIN '*I64'
   if ($MODULE == ddishm) setenv MACHIN '*DEC'
   if ($MODULE == iolib)  setenv MACHIN '*UNX'
   if ($MODULE == unport) setenv MACHIN '*DEC'
endif
if ($TARGET == fuji-vpp) then
                          setenv MACHIN '*I32'
   if ($MODULE == ddi)    setenv MACHIN '*XXX'
   if ($MODULE == iolib)  setenv MACHIN '*UNX'
   if ($MODULE == unport) setenv MACHIN '*FUJ'
   if ($MODULE == vector) setenv MACHIN '*FUJ'
endif
if ($TARGET == fuji-ap) then
                          setenv MACHIN '*I32'
   if ($MODULE == ddi)    setenv MACHIN '*XXX'
   if ($MODULE == iolib)  setenv MACHIN '*UNX'
   if ($MODULE == unport) setenv MACHIN '*SUN'
endif
if ($TARGET == hitachi) then
                          setenv MACHIN '*I32'
   if ($MODULE == ddi)    setenv MACHIN '*XXX'
   if ($MODULE == iolib)  setenv MACHIN '*UNX'
   if ($MODULE == unport) setenv MACHIN '*HIT'
endif
if ($TARGET == hp9000) then
                          setenv MACHIN '*I32'
   if ($MODULE == ddi)    setenv MACHIN '*SOC'
   if ($MODULE == iolib)  setenv MACHIN '*UNX'
   if ($MODULE == unport) setenv MACHIN '*HP '
endif
if (($TARGET == ibm32) || ($TARGET == ibm64)) then
                          setenv MACHIN '*I32'
   if ($TARGET == ibm64)  setenv MACHIN '*I64'
   if ($MODULE == ddi)    setenv MACHIN '*SOC'  # workstations use TCP/IP
   if ($MODULE == iolib)  setenv MACHIN '*UNX'
   if ($MODULE == unport) setenv MACHIN '*AIX'
endif
if (($TARGET == ibm32-sp) || ($TARGET == ibm64-sp)) then
                            setenv MACHIN '*I32'
   if ($TARGET == ibm64-sp) setenv MACHIN '*I64'
   if ($MODULE == ddi)      setenv MACHIN '*MPI' # but the SP uses MPI-1
   if ($MODULE == iolib)    setenv MACHIN '*UNX'
   if ($MODULE == unport)   setenv MACHIN '*AIX'
endif
if ($TARGET == linux-pc) then
                          setenv MACHIN '*I32'
   if ($MODULE == ddi)    setenv MACHIN '*MPI'
   if ($MODULE == iolib)  setenv MACHIN '*UNX'
   if ($MODULE == unport) setenv MACHIN '*F2C'
endif
if ($TARGET == macosx) then
                          setenv MACHIN '*I32'
   if ($MODULE == ddi)    setenv MACHIN '*SOC'
   if ($MODULE == iolib)  setenv MACHIN '*UNX'
   if ($MODULE == unport) setenv MACHIN '*F2C'
endif
if ($TARGET == necsx) then
                          setenv MACHIN '*I64'
   if ($MODULE == ddi)    setenv MACHIN '*SEQ'
   if ($MODULE == iolib)  setenv MACHIN '*UNX'
   if ($MODULE == unport) setenv MACHIN '*NEC'
   if ($MODULE == vector) setenv MACHIN '*NEC'
endif
if ($TARGET == sgi32) then
                          setenv MACHIN '*I32'
   if ($MODULE == ddi)    setenv MACHIN '*SOC'
   if ($MODULE == iolib)  setenv MACHIN '*UNX'
   if ($MODULE == unport) setenv MACHIN '*SGI'
endif
if ($TARGET == sgi64) then
                          setenv MACHIN '*I64'
   if ($MODULE == ddi)    setenv MACHIN '*MPI'
   if ($MODULE == iolib)  setenv MACHIN '*UNX'
   if ($MODULE == unport) setenv MACHIN '*SGI'
endif
if ($TARGET == sun) then
                          setenv MACHIN '*I32'
   if ($MODULE == ddi)    setenv MACHIN '*SOC'
   if ($MODULE == iolib)  setenv MACHIN '*UNX'
   if ($MODULE == unport) setenv MACHIN '*SUN'
endif
if ($TARGET == win32) then
                          setenv MACHIN '*I32'
   if ($MODULE == ddi)    setenv MACHIN '*SEQ'
   if ($MODULE == iolib)  setenv MACHIN '*UNX'
   if ($MODULE == unport) setenv MACHIN '*WIN'
endif
#
#   Here we catch machines for which porting of message passing has
#   not yet been done...
#   Please note that this is not just as simple as picking the
#   string to be activated in DDI above, as you will need to at
#   least think about your compiling clause below.
#
if (($MODULE == ddi) && ("$MACHIN" == "*XXX")) then
   echo "--------------------- error ----------------------------"
   echo The 'comp' script does not know how to compile DDI.SRC.
   echo              Compilation is bombing...
   echo "--------------------- error ----------------------------"
   exit 8
endif
#
#   ----- Activate the source code to produce FORTRAN -----
#
#   On Hitachi, to get the normal Unix calling arguments for -getenv-
#   which is called by ACTVTE, we need a special run time option:
if ($TARGET == hitachi) setenv HF_90OPTS '-FPORT(GETENV)'
#
set echo
setenv   SRCIN $SRCDIR/$MODULE.src
setenv CODEOUT $MODULE.f
$chmtool/actvte.x_gnu
unset echo
#
#   --------------- special machine "sed hacks" --------------------
#   Not everything can be done with ACTVTE, so we use "sed" (ugh!).
#
#    Direct access record length is measured in bytes on most Unix,
#    but some kinds insist in measuring 4-byte words.  This changes
#    only one line in RAOPEN and one line in OPENDA in IOLIB.SRC.
#    SGI's f90 should not execute the following, but SGI's f77 must.
#
if (($MODULE == iolib) && \
   ($TARGET == compaq-axp || $TARGET == compaq-shmem || \
    $TARGET == sgi32 || $TARGET == sgi64)) then
   mv iolib.f iolib.junk
   sed -e "s/     \*      RECL=8\*IRECLN/     *      RECL=2*IRECLN/" \
     iolib.junk > iolib.f
   rm -f iolib.junk
endif
#
#   Use DGEMM matrix multiply if possible.
#
if (($MODULE == mthlib) && ($BLAS3 == true)) then
   mv mthlib.f mthlib.junk
   sed -e "s/\*BL3/    /" mthlib.junk > mthlib.f
   rm -f mthlib.junk
endif
#
#   For 64 bit IBM or SGI machines, select 64 bit integers.
#   This corrects the source code's assumption of 32 bits for these.
#
if (($MODULE == unport) && \
    (($TARGET == ibm64) || ($TARGET == ibm64-sp) || ($TARGET == sgi64))) then
   mv unport.f unport.junk
   sed -e "s/      NWDVAR = 2/      NWDVAR = 1/" unport.junk > unport.f
   rm -f unport.junk
endif
#
#   This hack is necessary only if compiler flags below request the
#   use of 64 bit integers in GAMESS, and if the vendor's BLAS library
#   insists that its integer arguments be 32 bit quantities.
#   There is a special wrapper file gamess/misc/blaswrap.src that deals
#   with this problem, provided we hack the source to call the wrappers.
#
if (($MODULE != blaswrap) && \
   (($TARGET == ibm64) || ($TARGET == ibm64-sp) || ($TARGET == sgi64))) then
   sed -e "s/DASUM/XASUM/g"   -e "s/dasum/XASUM/g" \
       -e "s/DAXPY/XAXPY/g"   -e "s/daxpy/XAXPY/g" \
       -e "s/DCOPY/XCOPY/g"   -e "s/dcopy/XCOPY/g" \
       -e "s/DDOT/XDOT/g"     -e "s/ddot/XDOT/g"   \
       -e "s/DNRM2/XNRM2/g"   -e "s/dnrm2/XNRM2/g" \
       -e "s/DROT/XROT/g"     -e "s/drot/XROT/g"   \
       -e "s/DROTG/XROTG/g"   -e "s/drotg/XROTG/g" \
       -e "s/DSCAL/XSCAL/g"   -e "s/dscal/XSCAL/g" \
       -e "s/DSWAP/XSWAP/g"   -e "s/dswap/XSWAP/g" \
       -e "s/IDAMAX/IXAMAX/g" -e "s/idamax/IXAMAX/g" \
       -e "s/DGER/XGER/g"     -e "s/dger/XGER/g"   \
       -e "s/DTRMM/XTRMM/g"   -e "s/dtrmm/XTRMM/g" \
       -e "s/DTRMV/XTRMV/g"   -e "s/dtrmv/XTRMV/g" \
       -e "s/DTRSM/XTRSM/g"   -e "s/dtrsm/XTRSM/g" \
       -e "s/DGEMV/XGEMV/g"   -e "s/dgemv/XGEMV/g" \
       -e "s/DGEMM/XGEMM/g"   -e "s/dgemm/XGEMM/g" \
       $MODULE.f >> $MODULE.munged
   mv $MODULE.munged $MODULE.f
endif
#
#   The following hack selects AOINTS=DIST as the default on high end
#   Compaq and IBM systems, where we know we have a high quality network.
#
if (($MODULE == unport) &&  \
   (($TARGET == compaq-shmem) || ($TARGET == ibm32-sp) || \
    ($TARGET == ibm64-sp))) then
   mv unport.f unport.junk
   sed -e "s/\*TRF/    /" unport.junk > unport.f
   rm -f unport.junk
endif
#
#   Fujitsu AP3000, enable stripping of path information for local I/O files
#
if (($MODULE == iolib) && ($TARGET == fuji-ap)) then
   mv iolib.f iolib.junk
   sed -e "s/ISTRIP=1/ISTRIP=0/" iolib.junk > iolib.f
   rm -f iolib.junk
endif
#
#   Hitachi prefers a different double precision complex syntax
#
if (($MODULE == zheev) && ($TARGET == hitachi)) then
   mv zheev.f zheev.junk
   sed -e "s/COMPLEX\*16 FUNCTION ZLADIV/COMPLEX FUNCTION ZLADIV\*16/" \
       -e "s/COMPLEX\*16 FUNCTION ZDOTC/COMPLEX FUNCTION ZDOTC\*16/" \
       zheev.junk > zheev.f
   rm -f zheev.junk
endif
#
#   On Hitachi, the first argument of HF_CFUNC is actually a name of C
#   routine, so it is case sensitive.
#
if (($MODULE == unport) && ($TARGET == hitachi)) then
   mv unport.f unport.junk
   sed -e "s/HF_CFUNC('ABORT'/HF_CFUNC('abort'/" unport.junk > unport.f
   rm -f unport.junk
endif
#
#   On Sun, avoid ugly warning about possible floating point errors issued
#   by every data server process, which come from a FORTRAN STOP statement.
#
if (($MODULE == ddi) && ($TARGET == sun)) then
   mv ddi.f ddi.junk
   sed -e "s/      STOP/      CALL EXIT(0)/" ddi.junk > ddi.f
   rm -f ddi.junk
endif
#
#   ...and this is the end of the ugly "sed hack"s.
#
#   --------- Now we are ready to compile on the target machine ----------
#
cmp:
#
#   Optionally, save pure FORTRAN in temporary directory for 
#   syntax scan by the nifty FORTRAN analysis program FTNCHEK.
#
if ($ACTONLY == true) then
   mv $MODULE.f ~/scr
   exit
endif
#
if (-e $OBJDIR/$MODULE.o) rm -f $OBJDIR/$MODULE.o
#
#  Options for the Compaq AXP FORTRAN 5.2 compiler under Tru64 V4.0F are
#     -O0 turns off all optimization
#     -O1 does local optimizations
#     -O2 also does global optimizations
#     -O3 also does additional global optimizations
#     -O4 also does routine inlining (default)
#     -O5 also does software pipelining
#     -fast implies less accurate but faster libraries, etc. and -O4.
#     -c means compile only
#     -i8 selects use of 64 bit integers
#     -v gives compiler phase information, and timings
#     -automatic and -static use/avoid stack for local variables
#     -col72 chops source lines past 72 columns
#     -stand nosemantic means don't warn of nonANSI usage (default)
#     -stand nosource_form means don't warn of tabs/lowercase (default)
#     -stand nosyntax means don't warn of nonANSI syntax (default)
#
#  Note that we've tried f77 3.3, 3.7, 3.8, and 5.2, but nothing in between.
#  This is an excellent compiler, and probably all versions work.
#
if ($TARGET == compaq-axp) then
   set OPT = '-O4'
   set echo
   f77 -c -i8 -v -automatic -col72 $OPT $MODULE.f
   unset echo
endif
#      The SuperCluster assumes a specific AXP chip...
if ($TARGET == compaq-shmem) then
   set OPT = '-fast -tune ev67 -arch ev67'
   if ($MODULE == zheev) set OPT='-O0'
   set echo
   f77 -c -i8 -v -col72 $OPT $MODULE.f
   unset echo
endif
#
#    Cray parallel vector processor (e.g. T90, J90, SV1...)
#    Cray T90 tested in February of 2000
#    Cray SV1 rev C with CF90 3.4.0.0.4 tested in March of 2000
#    Note that all Cray clauses now use f90 compiler only.
#
if ($TARGET == cray-pvp) then
   set OPT = '-O2 '
   if ($MODULE == int2a)  set OPT='-Ovector0,scalar3 '
   if ($MODULE == grd2a)  set OPT='-Ovector0,scalar2 '  # Jerry says
   if ($MODULE == grd2b)  set OPT='-Ovector0,scalar3 '
   if ($MODULE == grd2c)  set OPT='-Ovector0,scalar3 '
   if ($MODULE == gugem)  set OPT='-Ovector0,scalar3 '
   if ($MODULE == vector) set OPT='-O1 '
   if ($MODULE == gvb)    set OPT='-O1 '   # use -O0 if exam15,16,21 fail
   if ($MODULE == gamess) set OPT='-O0 '
   if ($MODULE == efinp)  set OPT='-O0 '
   if ($MODULE == efinta) set OPT='-Ovector0,scalar3 '
   if ($MODULE == efintb) set OPT='-Ovector0,scalar3 '
   if ($MODULE == efgrda) set OPT='-Ovector0,scalar3 '
   if ($MODULE == efgrdb) set OPT='-Ovector0,scalar3 '
   if ($MODULE == efgrdc) set OPT='-Ovector0,scalar3 '
   if ($MODULE == efgrd2) set OPT='-Ovector0,scalar3 '
   set echo
   f90 -c -V $OPT $MODULE.f
   unset echo
endif
#
#  Compiler flags for Cray T3E under UNICOS/mk 2.0.3.18, cf90 3.1.0.3
#  -Oscalarn is various scalar optimizations (n=0,1,2,3)
#  -Oaggress increases table sizes used during optimizations
#  -O[no]msgs,[no]negmsgs [disables]enables compiler optimization messages
#  -dp disables double precision
#
if ($TARGET == cray-t3e) then
   set OPT = '-Oscalar3,aggress'
   if ($MODULE == grd2c) set OPT='-O1'   # for cf90 3.1.0.3 and others
   if ($MODULE == hss1b) set OPT='-O1'   # for cf90 3.4.0.0
   set echo
   env TARGET=cray-t3e f90 -c $OPT -dp -Onomsgs,nonegmsgs $MODULE.f
   unset echo
endif
#
#  Fujitsu VPP300 options
#  Modified by Anthony J. Russell, ANU Supercomputer Facility, 20/1/98
#
#  Should also be suitable for other VPP and VX supercomputers.
#              
#  Options for Fujitsu's UXP/V Fortran90 compiler under UXP/M are
#    -Ab           means reference dummy arguments by address
#    -c            means compile only
#    -Oe           is for extended scalar optimization
#    -Ss           turns off I and W level messages
#    -Kfast        produces code optimized for speed of execution
#                  on the compile time machine
#    Vectorization options
#    -Wv,-Of,-te,-ilfunc    invokes the vector compiler
#                  with full optimization for vector intrinsic functions
#    -te           Expands the work area for vectorization
#    -ilfunc       inlines function calls
#
if ($TARGET == fuji-vpp) then
   set OPT = '-Oe -Kfast -Wv,-Of,-te,-ilfunc'
#         next ones are faster in scalar
   if ($MODULE == int2a)  set OPT='-Oe'
   if ($MODULE == grd2a)  set OPT='-Oe'
   if ($MODULE == grd2b)  set OPT='-Oe'
   if ($MODULE == grd2c)  set OPT='-Oe'
   if ($MODULE == gugem)  set OPT='-Oe'
   if ($MODULE == mctwo)  set OPT='-Oe'
   set INCPATH='-I/usr/lang/mpi/include'
   set echo
   frt -c -Ab -Ss $OPT $MODULE.f $INCPATH
   unset echo
endif
#
#  Fujitsu AP-3000 options
#  this is an array of UltraSPARC chips used to construct a parallel computer
#  Anthony J. Russell, ANU Supercomputer Facility, 5/2/98
#  
#  The OPT string is fastest compiler generated code
#  -c compile only
#  -w66 suppress the messages generated by using F66 features
#  -Nl indicates continuation line
#
if ($TARGET == fuji-ap) then
   set OPT = '-fast -xarch=v8plus'
   set echo
   f77 -c -w66 -Nl30 $OPT $MODULE.f
   unset echo
endif
#
#    Options for HITACH SR2201 machine's HI-UX/MPP compiler
#      blank implies full scalar optimization
#      -W0,'OPT(O(0))' turns off optimization
#      -W0,'OPT(O(ss)),PVEC(DIAG(1))' is vectorization (not tested yet)
#
if ($TARGET == hitachi) then
   set FORT='f90'
   set INCPATH='-I/usr/mpi/include'
   set OPT=" "
   if ($MODULE == sobrt) set OPT="-W0,'OPT(O(0))'"
   set echo
   $FORT -c $OPT $INCPATH $MODULE.f
   unset echo
endif
#
#  HP-UX B.11.0, using f90 2.4.10 (f90 +version reports version number)
#  The options mean:
#    +[no]save governs automatic variable allocation strategy
#    +On   selects optimization level; n=0,1,2,3,4  (-O means +O2)
#          0 is none, 1 is block transforms, 2 is full within subprograms
#          3 crosses subprograms (includes inlining) also loop transforms
#    +Oaggressive selects the following four additions to +O2:
#    +Oentrysched  = instruction scheduling on entry/exit
#    +Olibcalls    = special trig and power functions (lacking error hand.)
#    +Onofltacc    = permits use of a faster single multiply/add instruction.
#    +Onoinitcheck = disable initialization of any unitialized datum
#       other optimizations include
#    +Onolimit     = ignores time and memory limits when optimizing
#    +Ofastaccess  = gives fast access to global data
#    +U77 gives access to BSD's 3F library
#
if ($TARGET == hp9000) then
                          set OPT='+O2 +Oaggressive +Onolimit +Ofastaccess'
   if ($MODULE == unport) set OPT='+O2 +Oaggressive +Onolimit +Ofastaccess +U77'
   set echo
   f90 -c $OPT +nosave $MODULE.f
   unset echo
endif
#
#  The following was used for compiler version 10.20 under HP-UX 10.
#
#---if ($TARGET == hp9000) then
#---   set OPT='+O2'
#---   if($MODULE == hss1a)  set OPT='+O1'
#---   set echo
#---   f77 -c $OPT +U77 +Oentrysched +Ofastaccess \
#---           +Olibcalls +Onofltacc +Onolimit $MODULE.f
#---   unset echo
#---endif
#
if ($TARGET == convex-spp) then
   set OPT='+O2 +Obb600'
   if ($TARGET == convex-spp) set OPT='$OPT -K'
   if ($MODULE == guess)  set OPT='+O1'
   if ($MODULE == hss2a)  set OPT='+O1'
   if ($MODULE == inputa) set OPT='+O1'
   set echo
   f77 -c $OPT +FPD +E1 +E6 +T -s $MODULE.f
   unset echo
endif
#
#   For IBM 32 or 64 bit RS/6000 workstations or SP parallel systems,
#   the options for XL FORTRAN 7.1.0 and AIX 4.3.3 are
#       -c means compile only
#       -O2 and -O3 choose optimization levels (there is no -O1)
#       -qarch=com,601,pwr,pwr2,pwr3 (see also -qtune)
#       -qflag requests I,L,W,E,S,Q (information, language, 
#              warning, error, severe, quiet) messages
#       -qflttrap=ov:zero:inv:inex:enable traps bad arithmetic
#       -qhalt stops compiler after certain error level
#       -qnosave forces non-static storage
#       -qinitauto=ff initializes non-static storage
#       -qextchk checks calling args and common lengths at link
#       -qsource requests a printable .lst file.
#       -qtune selects target chip
#
#   The following clause requires XL FORTRAN 3.2 or higher.
#   Use of 64 bit integers requires XL FORTRAN 6.1 or higher.
#   The command to find your FORTRAN version level depends on your
#   AIX version: v3 is "lslpp -h xlfcmp.obj", v4="lslpp -h xlfcmp".
#
#   To profile code during development stages,
#   1. "comp" desired modules with -pg added to the xlf flags
#   2. "lked" after adding -p to the xlf flags.
#   3. execute to completion, generating 'mon.out' file.
#   4. profile by 'prof gamess.00.x mon.out'.
#
if (($TARGET == ibm32) || ($TARGET == ibm64) || \
    ($TARGET == ibm32-sp) || ($TARGET == ibm64-sp)) then
   set FORT='xlf'
   if ($TARGET == ibm32-sp) set FORT='mpxlf'
   if ($TARGET == ibm64-sp) set FORT='mpxlf'
#         old 32 bit workstations, whether Power, Power2, or PowerPC
#         should all select plain vanilla common architecture.  Its
#         faster and runs on any combination of these chips you own.
#         under xlf 4.1.0 a few files won't fully optimize
   if ($TARGET == ibm32) then
      set  OPT='-O3'
      if ($MODULE == grd2b)  set OPT='-O3 -qspillsize=1500'
      if ($MODULE == gugdm)  set OPT='-O2'
      if ($MODULE == gugem)  set OPT='-O2'
      if ($MODULE == resc)   set OPT='-O2'
      if ($MODULE == trnstn) set OPT='-O2'
      set ARCH='com'
      set TUNE='604'   # PowerPC tuning makes even Power1 run faster
   endif
#         64 bit workstations or SP is sure to be Power3 or 4 chips.
#         everything optimizes under xlf 5.1.1 and 6.1.0 and 7.1.0
   if (($TARGET == ibm64) || ($TARGET == ibm64-sp)) then
      set  OPT='-O3 -q64 -qintsize=8 -qspillsize=1500'
      set ARCH='pwr3'
      set TUNE='pwr3'
   endif
#         your 32 SP might not be a new one, if not, mimic ibm32 clause.
#         Here ARCH/TUNE assume the SP is new enough to have Power3 nodes,
#         but that the software is old and so lacks a 64 bit MPI library.
   if ($TARGET == ibm32-sp) then
      set  OPT='-O3 -qspillsize=1500'
      set ARCH='pwr3'
      set TUNE='pwr3'
   endif
   set echo
   $FORT -c $OPT -qarch=$ARCH -qtune=$TUNE \
         -qflag=I:I -qhalt=W -qnosave $MODULE.f
   unset echo
endif
#
#   ------ RedHat Linux on "PC" (Pentium/Athlon chips)  -----
#   There are no less than four compilers for Linux included here!
#   They are g77 (default), f2c/gcc, Intel's ifc, and Portland's pgf77.
#   If you want to use one of the other three, just make sure that the
#   one you prefer is uncommented.  In addition, you will need to edit
#   the link-edit script 'lked' to reflect your compiler choice here.
#
#   Note that we have only used Redhat, so this is not necessarily
#   correct for Slackware, SUSE, FreeBSD, ..., although many people
#   have been able to get these others to work.
#
#   1. Using g77 compiler.     compiler version query: rpm -q gcc-g77 
#   If your RedHat is older than 6.0, see the f2c/gcc clause below.
#
#   There is an I/O library problem in g77, apparently in all RedHat
#   releases from 6.0 through 7.1's gcc-g77-2.96-81.  You can find a
#   a fix on our web page: www.msg.ameslab.gov/GAMESS/GAMESS.html,
#   then check the bottom of the "how to get a copy of GAMESS" page.
#   It is crucial to install this, as it works around a very serious
#   error in the g77 I/O library.
#   The 3.0 release of GNU compilers is scheduled to fix this bug.
#
#   The meaning of g77's flags are
#      -O0, -O1, -O2, -O3 are the optimization levels, -O means -O1
#      -Wno-globals -fno-globals together suppress argument type checking.
#      -fautomatic defeats use of static storage
#      -malign-double uses 64 bit data alignment
#   Tests on a representative GAMESS test suite (executing energy, gradient
#   hessian code, as well as various SCF, MCSCF, MP2 runs, show that each
#   of the more aggressive optimizations -ffast-math, -march=i586, and -O3
#   gave speedups of less than 1% each.  (-march=i686 slows a Celeron type
#   Pentium II fractionally).  None of these optimizations are used below.
#      
if ($TARGET == linux-pc) then
   set OPT = '-O2'
   set echo
   g77 -c $OPT -malign-double -march=i686 -fschedule-insns2 -Wno-globals -fno-globals $MODULE.f
   unset echo
endif
#
#   2. RedHat 4.x and 5.x's FORTRAN compiler was f2c/gcc, and early
#   releases of g77 that were present in some of these distributions
#   do not run GAMESS correctly.  If you have RedHat 6.0 or higher,
#   which do not contain the f2c interpreter, see just above.  If
#   your RedHat is older, use f2c/gcc instead of buggy old g77's.
#
#--if ($TARGET == linux-pc) then
#--   if (($MODULE == qfmm) || ($MODULE == solib) || ($MODULE == zheev)) then
#--       mv $MODULE.f $MODULE.junk
#--       sed -e s/DREAL/DBLE/g $MODULE.junk > $MODULE.f
#--       rm -f $MODULE.junk
#--   endif
#--   set OPT = '-O3 -malign-double'
#--   set echo
#--   f2c -w66 -a -Nn802 -Nx400 $MODULE.f
#--   gcc -c $OPT $MODULE.c
#--   rm -f $MODULE.c
#--   unset echo
#--endif
#
#    3. Intel Fortran Compiler 5.0.1 for Linux
#    This compiler can be downloaded for non-commercial purposes:
# http://developer.intel.com/software/products/compilers/f50/linux/noncom.htm
#    -O3  is the most aggressive optimization, the default is -O2.
#    -cm  turn off printing of informational messages (f90 syntax, etc)
#    -w   turn off all warnings
#    Remember to load compiler setup by running proper (for your shell)
#    setup script as described in Intel's docs:
#            . /opt/intel/compiler50/ia32/bin/ifcvars.sh
#
#--if ($TARGET == linux-pc) then
#--   set OPT = '-O3'
#--   if ($MODULE == zheev) set OPT='-O0'
#--   set echo
#--   ifc $OPT -cm -w -c $MODULE.f
#--   unset echo
#--endif
#
#    4. Portland Group compiler.  
#    This is a synthesis of recommendations from Fred Arnold and Brian
#    Salter-Duke.  -fast means -O2 -Munroll -Mnoframe, the latter option
#    seems to be the source of several incorrect results.  It is said that
#    "-Munroll" and "-tp p6" gives no significant improvement in run times.
#
#--if ($TARGET == linux-pc) then
#--   setenv PATH $PATH\:/usr/pgi/linux86/bin
#--   setenv PGI /usr/pgi
#--   if (($MODULE == qfmm) || ($MODULE == solib) || ($MODULE == zheev)) then
#--       mv $MODULE.f $MODULE.junk
#--       sed -e s/DREAL/DBLE/g $MODULE.junk > $MODULE.f
#--       rm -f $MODULE.junk
#--   endif
#--   set OPT = '-O2'
#--   set echo
#--   pgf77 -c $OPT $MODULE.f
#--   unset echo
#--endif
#
#  Mac OS X version 10.1 (aka Darwin)
#  You must first install the Apple Developer Toolkit to get a cc
#  compiler, this is available for download at Apple's web site.
#  Then you have two choices for obtaining a FORTRAN compiler:
#  1. f2c, which in our experience was 10% faster, and is therefore 
#  the default.  Download the f2c installation script from 
#    http://www.caos.aamu.edu/pub/MacOS_X/BSD/Development/Languages/f2c
#  2. g77, from the FINK project, http://fink.sourceforge.net/download,
#  first to get the binary installer, and then under languages, g77.
#  In the event you use g77, you also need to change the 'lked' script.
#
if ($TARGET == macosx) then
    if (($MODULE == qfmm) || ($MODULE == solib) || ($MODULE == zheev)) then
        mv $MODULE.f $MODULE.junk
        sed -e s/DREAL/DBLE/g $MODULE.junk > $MODULE.f
        rm -f $MODULE.junk
    endif
    set OPT = '-O3'
    set echo
    f2c -w66 -a -Nn802 -Nx400 $MODULE.f
    cc -c $OPT $MODULE.c
    rm -f $MODULE.c
    unset echo
#
#--    set OPT = '-O2'
#--    set echo
#--    g77 -c $OPT -fautomatic -Wno-globals -fno-globals $MODULE.f
#--    unset echo
endif
#
# Options for f90 under SUPER-UX on the NEC SX Series are
#      -float0 -w Cray compatible data
#      -b sets needed passing of scalar arguments
#      -pvtcl noassume vwork=stack sets vectorization temporaries parameters
#      -Nv  vectorization is turned off
#
if ($TARGET == necsx) then
   set OPT = '-Wf"-v -pvctl noassume vwork=stack"'
   if ($MODULE == mcqdpt) \
       set OPT='-Wf"-v -pvctl noassume vwork=stack noaltcode nodivloop -NO"'
   if ($MODULE == ddi) \
       set OPT='-Wf"-v -pvctl noassume vwork=stack -I /usr/include"'
#         compiler character problem
   if ($MODULE == inputc) set OPT='-Wf"-Nv"'
#         next ones are faster in scalar
   if ($MODULE == int2a)  set OPT='-Wf"-Nv"'
   if ($MODULE == int2b)  set OPT='-Wf"-Nv"'
   if ($MODULE == grd2a)  set OPT='-Wf"-Nv"'
   if ($MODULE == grd2b)  set OPT='-Wf"-Nv"'
   if ($MODULE == grd2c)  set OPT='-Wf"-Nv"'
   if ($MODULE == gugem)  set OPT='-Wf"-Nv"'
   if ($MODULE == gugdrt) set OPT='-Wf"-Nv"'
#
   set echo
   f90 -c -float0 -ebw $OPT $MODULE.f
   unset echo
endif
#
#  Options for SGI f77 7.3 under Irix 6.5.14
#  Type "versions -I ftn77_dev" to see the compiler revision.
#
#  Different versions of the SGI compiler may not work with the fairly
#  aggressive -O3 optimization used below.  If you experience any numerical
#  problems, please try using -O2 rather than -O3 below.  It is unsafe to
#  use very aggressive optimizations like "-OPT:IEEE_arith=3:roundoff=3".
#
#  ARCH might need to be changed from -r12000 to -r10000 or -r8000 or -r5000,
#  type "hinv -c processor" to see what your processor is.
#  Especially for R5000 systems, or low end O2, the options "-64 -mips4"
#  might need to be "-n32 -mips3", perhaps.  If you change these here, be
#  sure to make the corresponding change in compall and lked scripts too.
#
#  If you use f90, do not reset the units for RECL in the sed hacks above.
#
#  -show would add verbose printing of compiler phase information.
#  -automatic is the opposite of -static storage allocation.
#  -OPT:Olimit=0 removes subroutine size optimization limits.
#  -woff suppresses warnings (2290=type checking on calling args).
#  -i8 selects 8 byte integer and logical data types.
#
if (($TARGET == sgi32) || ($TARGET == sgi64)) then
   set OPT='-O3'
   if ($MODULE == grd2c) set OPT='-O2'
   set ARCH='-r12000'
                         set INTS=' '
   if ($TARGET == sgi64) set INTS='-i8'
   set FLAGS='-64 -mips4 -automatic -G0 -woff 2290 -OPT:Olimit=0'
   set echo
   f77 -c $OPT $ARCH $FLAGS $INTS $MODULE.f
   unset echo
endif
#
#  Options for UltraSPARC Sun f77 4.2 under Solaris 2.6.
#     -c compile only
#     -v print verbose information on compiler phases and options
#     -stackvar forces non-static storage
#     -fast     is a smorgasbord of all the below, if you wish to
#               override one you must *follow* the -fast with it.
#         -xtarget=native  (optimize for chip being compiled upon)
#         -O4        (see below)
#         -libmil    (inline some math routines)
#         -fsimple=1 (0,1,2 control floating point simplifications)
#         -dalign    (double word alignment and instructions)
#         -xlibmopt  (link to optimized math library)
#         -depend    (DO loop data dependency analysis)
#         -fns       (fast underflow handling)
#         -ftrap=%none  (floating point error handling)
#     -O5 = aggressive optimizations
#     -O4 = automatic inlining
#     -O3 = loop unrolling and additional global optimizations
#     -O2 = basic local, global optimizations (must turn off -depend)
#     -O1 = minimal optimization
#     -xtarget=generic doesn't tune for any particular chip
#     -xtarget=native selects a -xarch,-xchip,-xcache for compile host
#            you may want to set this a particular chip if you run
#            more times on a different system than you compile on.
#  On an UltraSPARC, the -xtarget=native in -fast implied -xarch=v8!
#  You get best results by picking -xarch value closest to your system.
#     -xarch=generic  no particular SPARC chip
#     -xarch=v7      SPARC V7   (i.e. SPARCstn 1)
#     -xarch=v8      SuperSPARC (i.e. SPARCstn 2 and SPARCstn 10)
#     -xarch=v8a     microSPARC
#     -xarch=v8plus  UltraSPARC II, using 32 bit instructions
#     -xarch=v8plusb UltraSPARC III
#     -xarch=v9      64 bit instruction set (not tested well)
#
#  MPI usage: in comp, add -I/opt/SUNWhpc/include/
#             in lked, add -L/opt/SUNWhpc/lib -lfmpi -lmpi
#  as well as making the obvious changes to move Sun clauses from the
#  TCP/IP sockets sections to the MPI sections in compall and lked.
#
#  If you have a pre-ultraSPARC system, try OPT='-O2' or '-O3' while
#  eliminating the -xarch flag, and you might also need -Nl30.
#
if ($TARGET == sun) then
   set OPT='-fast -O4 -xarch=v8plus'
   if ($MODULE == ddi)    set OPT='-fast -O4 -nodepend -xarch=v8plus'
   if ($MODULE == grd2c)  set OPT='-fast -O4 -nodepend -xarch=v8plus'
   set echo
   f77 -c $OPT -stackvar $MODULE.f
   unset echo
endif
#
#  Windows (win32) versions using Compaq Visual Fortran compiler
#  The scripts may be executed with UWIN's C-shell emulator.
#
if ($TARGET == win32) then
   set OPT = '4'
   if ($MODULE == inputa) set OPT = '2'
   set echo
   f77.exe /c /optimize:$OPT /object:$MODULE.o $MODULE.f
   unset echo
endif
#
#  Store the generated object code, clean up, and quit
#
#mv $MODULE.o $OBJDIR/$MODULE.o
#if ($TINKER == true) exit
#rm -f $MODULE.f
#if ($SRCDIR == .) rm -f $MODULE.src    # delete copy with no scalar code
unset echo
date
time