#!/bin/sh

#    Jan 25/2017 - F.Majaess (Removed/added "fdir"/"xdir" parameter support)
#    Dec 03/2008 - F.Majaess (Revised for sa/saiph, ib/dorval-ib, af/alef)
#    Jan 05/2007 - F.Majaess (Added "noxlfqinitauto" option support)
#    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 ya/yata)
#    Jan 29/2001 - F.Majaess (Revised to support SXCROSS_KIT/SXF90_VER)
#    Feb 23/2000 - F.Majaess (Revised for kz/kaze and "sxf90")
#    Jan 21/1999 - F.Majaess (Added "fdir" parameter support)
#    Jan 21/1999 - F.Majaess (Added yo/yonaka)
#    Jul 14/1997 - F.Majaess (Revise for sx/hiru)
#    May 06/1997 - F.Majaess (Add "multi" switch for SX4)
#    Mar 18/1997 - F.Majaess (Revise for group file ownership)
#    Feb 26/1997 - F.Majaess (Use "/bin/ar.use-temp" on sx3r
#                              until "ar" problem is fixed)
#    Jun 10/1996 - F.Majaess (Implement "all" switch)
#    Apr 04/1996 - F.Majaess (revise for SX-4)
#    Oct 26/1995 - F.Majaess (Modify for IBM RS6000)
#    Feb 03/1995 - F.Majaess (Modify for SGI)
#    Dec 13/1994 - F.Majaess (Add time/mem parameters)
#    Nov 08/1994 - F.Majaess (Modify for sx3r and new destination identifiers)
#    Jan 13/1994 - M. Lazare (Protect source code from shell by enclosing
#                             "woofsx" in quotes)
#    Feb 16/1993 - E. Chan (Translate file names to lower case after fsplit
#                           on the SX-3)
#    Feb 08/1993 - E. Chan (Allow Bourne shell options on command line)
#    Nov 23/1992 - E. Chan (List contents of '.L' files to printout)
#    Aug 17/1992 - E. Chan (Implement float1 option on MIPS and change
#                  FLOAT to DFLOAT, AIMAG to IMAG, and COMPLEX to COMPLEX*16
#                  when compiling with the '-r8' option on the MIPS)
#    Jul 21/1992 - E. Chan (Increase memory limits in call to qsub)
#    Feb 26/1992 - E. Chan  

#id  archsub - Modifies a CCRN archive file on a server from Fortran
#id            source code (subroutines) on the MIPS server.

#    AUTHOR  - M. Lazare

#hd  PURPOSE - "archsub" gets files(s) of source code (function
#hd            or subroutine) from the official CCRN LSSUB file
#hd            structure on the server, and in-lines it into a batch job
#hd            submitted to a server which compiles the routines and
#hd            updates the appropriate archive ("---.a") file there.
#hd            These files typically resides under $RTEXTBLS.
#hd            If the destination is one of the back-end nodes, it will 
#hd            also update the source code ("---.f") files in 
#hd            $CCRNSRC/source$OSbin/lssub.
#hd 

#pr  PARAMETERS:
#pr 
#pr    POSITIONAL
#pr
#pr      fil    = name of subroutine or function with which to update the
#pr               archive file (with .f extension).
#pr               If "*.f" is supplied, it will process all such files in
#pr               the current working directory.
#pr               "*.f" is the default if "all" switch is activated.
#pr
#pr    PRIMARY
#pr 
#pr
#pr      Note: One of 'ha/sp/po/ca/mz/hr/br/ba/dy/p1/p2/p3/p4' is to be specified.
#pr
#pr    hare/hr  = to target job to hare.
#pr
#pr    brooks/br  = to target job to brooks.
#pr
#pr     daley/dy  = to target job to daley.
#pr
#pr   banting/ba  = to target job to banting.
#pr
#pr    ppp1/p1  = to target job to ppp1.
#pr
#pr    ppp2/p2  = to target job to ppp2.
#pr
#pr    ppp3/p3  = to target job to ppp3.
#pr
#pr    ppp4/p4  = to target job to ppp4.
#pr
#pr    hadar/ha = to target job to IBM (hadar).
#pr
#pr    spica/sp = to target job to IBM (spica).
#pr
#pr    pollux/po= to target job to Pollux Linux front-end.
#pr
#pr    castor/ca= to target job to Castor Linux front-end.
#pr
#pr      mez/mz = to target job to Mez Linux front-end.
#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 archive library.
#pr               (=$DEFFGRP).
#pr
#pr      time   = time limit to use for submitting batch job in seconds 
#pr               (=900)
#pr  
#pr      mem    = memory limit to use for submitting batch job in megabytes 
#pr               (=250)
#pr 
#pr      afname = name of archive file, without "--.a" extension. 
#pr               Typically, will be one of the following:
#pr                       LOSUB_comm    - common and machine local routines.
#pr                       LOSUB_diag    - diagnostic routines.
#pr                       LOSUB_plots   - plotting routines.
#pr                       LOSUB_model   - model routines (not on servers).
#pr               Each of the above archive files correspond to one sub-
#pr               directory branch of $CCRNSRC/source$OSbin/lssub (/local 
#pr               combined with /comm, in order for search to work in all 
#pr               cases).
#pr               For servers, the above are prefaced inside this script with
#pr               "lib", since this string is required by f77 (see "afname"
#pr               re-definition).
#pr               Must be supplied with keyword.
#pr
#pr      xdir   = directory where the generated archive library to be placed.
#pr               (="\$RTEXTBLS")
#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 
#pr      float1 = switch to compile the code in 32-bits (real/integer)
#pr               mode.
#pr               It can be used in conjunction with "openmp=yes".
#pr
#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               "castor/ca","mez/mz" or other suitable targets.
#pr
#pr    SECONDARY
#pr 
#pr noxlfqinitauto = switch valid only on IBM running 'xlf' under AIX
#pr               where using this switch lead to the compilation without
#pr               '-qinitauto' xlf compilation option for initializing automatic
#pr               arrays. (valid only in combination with openmp mode).
#pr
#pr       noext = switch valid only on IBM running 'xlf' under AIX
#pr               where using this switch lead to the compilation without
#pr               '-qextname' xlf compilation option.
#pr
#pr        all  = switch to process all '.f' files in all the 
#pr               subdirectories underneath the current subdirectory 
#pr               as well as those in the current subdirectory.
#pr               Mainly used to rearchive all '*.f' codes for a given
#pr               library. "all=yes" is not valid with "f90mod=yes".
#pr               (='no'/'yes')
#pr
#pr     f90mod  = switch to be used with Fortran 90(+) modules where
#pr               the source code is expected to be supplied so that
#pr               the internal caching file is properly ordered with
#pr               modules defined before being referenced. Also, the 
#pr               submitted job make use of and relies upon "f90split" 
#pr               utility functionalities to properly accomplish the
#pr               expected task. Not appropriate to use with "all=yes".
#pr               (='no'/'yes')
#pr
#pr     updtmk  = switch valid only in combination with "f90mod=yes".
#pr               It handles updating "f90split" generated mapping
#pr               file into the path it's pointing to.
#pr
#pr        febg = switch to execute job in background mode
#pr               without going through batch mode (='no'/'yes')
#pr
#pr        wait = switch valid only in combination with "febg=yes"
#pr               to run the job as part of the current interactive
#pr               session and wait for it to complete (='no'/'yes')

#ex  EXAMPLE:
#ex 
#ex   1) archsub GAUSSG.f afname=mlsub po 
#ex 
#ex      The above example gets the source code for the subroutine "GAUSSG" on
#ex      the server and submits a batch job to pollux to compile it and update
#ex      the archive file libmlsub.a under $RTEXTBLS.

#ex   2) archsub *.f afname=LOSUB_model fil_grpname='ccrn_shr' ha
#ex 
#ex      The above example gets the source code from all "*.f" files in the
#ex      current working directory on the server and submits a batch job to 
#ex      IBM "HADAR" to compile it and update the model routines archive file
#ex      LOSUB_model.a under $RTEXTBLS with "ccrn_shr" group ownership.

#ex   3) archsub all openmp mem=100 time=1800 afname=LOSUB_comm_openmp sp
#ex 
#ex      Assuming the above command invoked from ...src/source$OSbin/lssub/comm
#ex      subdirectory. This effectively will result in rearchiving all
#ex      the '.f' routines into LOSUB_comm_openmp.a archive library on SPICA
#ex      and saving the '.f' files into .../src/source$OSbin/lssub_openmp 
#ex      subdirectory there.

#ex   4) archsub *_mod.*dk time=1801 f90mod=yes  \
#ex          updtmk=$CCRNSRC/source/lsmod/agcm/gcmxx/MODULES/DEPENDENCIES \
#ex          afname=LOSUB_model_gcmxx_float1_openmp_noxlfqinitauto \
#ex          float1 openmp noxlfqinitauto ha
#ex 
#ex      Assuming the above command invoked from within MODULES subdirectory
#ex      under ...src/source$OSbin/lsmod/agcm/gcmxx. It will result in 
#ex      compiling all matching "*_mod.*dk" modules source code, updating
#ex      corresponding ".o" files in 
#ex         libLOSUB_model_gcmxx_float1_openmp_noxlfqinitauto_mod_dir
#ex      subdirectory and archiving them as well into 
#ex         libLOSUB_model_gcmxx_float1_openmp_noxlfqinitauto.a
#ex      archive library under $RTEXTBLS on Hadar. Also, updating 
#ex      "f90split" generated dependencies mapping files under
#ex         $CCRNSRC/source/lsmod/agcm/gcmxx/MODULES/DEPENDENCIES
#ex      subdirectory.

#  * 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
fil_list=''
# set -x

#  * Obtain the file names and any specified option.

arg_list=$@
for arg in $arg_list
do
  case $arg in
            -*) set $arg                     ;;
           *=*) eval "$arg"                  ;;
       hare|hr) mdest=${mdest:='hare'}     ;;
       brooks|br) mdest=${mdest:='brooks'}     ;;
        daley|dy) mdest=${mdest:='daley'}     ;;
      banting|ba) mdest=${mdest:='banting'}     ;;
       ppp1|p1) mdest=${mdest:='ppp1'}     ;;
       ppp2|p2) mdest=${mdest:='ppp2'}     ;;
       ppp3|p3) mdest=${mdest:='ppp3'}     ;;
       ppp4|p4) mdest=${mdest:='ppp4'}     ;;
       hadar|ha) mdest=${mdest:='hadar'}     ;;
       spica|sp) mdest=${mdest:='spica'}     ;;
     pollux|po) mdest=${mdest:='pollux'}     ;;
     castor|ca) mdest=${mdest:='castor'}     ;;
        mez|mz) mdest=${mdest:='mez'}        ;;
         lxsrv) mdest=${mdest:='lxsrv'}      ;;
        lxsrv2) mdest=${mdest:='lxsrv2'}     ;;
        lxwrk1) mdest=${mdest:='lxwrk1'}     ;;
        lxwrk2) mdest=${mdest:='lxwrk2'}     ;;
          febg) febg='yes'                   ;;
          wait) wait='yes'                   ;;
           all)  all='yes'                   ;;
        f90mod) f90mod='yes'                 ;;
         noext) noext='_noext'               ;;
 noxlfqinitauto) noxlfqinitauto='_noxlfqinitauto' ;;
      afname=*) eval "$arg"                  ;;
        float1) if [ "$float" = '_openmp' ] ; then
                float='_float1_openmp'       
                else
                 float='_float1'
                fi                           ;; 
        float2) float='_float2'              ;; 
        openmp) if [ "$float" = '_float1' ] ; then
                float='_float1_openmp'              
                else
                 float='_openmp'              
                fi                           ;;
             *) fil_list="$fil_list $arg"
  esac
done 

# Ensure all '.f' are processed if "all" switch is invoked...

all=${all:='no'}
wait=${wait:='no'}
if [ "$all" = 'yes' ] ; then
 #if [ "$f90mod" = 'yes' ] ; then
 # echo "Archsub: Sorry, all=yes is not valid with f90mod=yes !"
 # exit 1
 #fi
 #fil_list='*.f'
  fil_list=${fil_list:='*.f'}
fi

#  * Exit if no value set for fil_list.

if [ -z "$fil_list" ] ; then
  echo "No value specified for fil=$fil_list"
  exit 1
fi

#  * Exit if no value set for afname.

if [ -z "$afname" ] ; then
  echo "No value specified for afname=$afname"
  exit 2
fi

# 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 [ "$SITE_ID" = 'Dorval' -a "$mdest" = 'erg' ] ; then
 if [ "$HOSTIDf" = 'erg' ] ; then
  mdest="$HOSTID"
  febg='yes'
 else
  echo "Sorry, archsub must be invoked from one of ERG nodes if mdest=erg !"
  exit 3
 fi
elif [ "$SITE_ID" = 'Victoria' -o "$SITE_ID" = 'Downsview' -o "$SITE_ID" = 'York' -o "$SITE_ID" = 'Utor1' -o "$SITE_ID" = 'UOFT' ] ; then
 mdest=${mdest:=$HOSTID}
fi

#  * Prompt for a destination if none was specified.

while [ -z "$mdest" ]
do
  echo "please enter a destination; ha/sp/po/ca/mz/hr/br/ba/dy/p1/p2/p3/p4: > \\c"
  read tmdest
  case $tmdest in
       hare|hr) mdest='hare'                  ;;
     brooks|br) mdest='brooks'                  ;;
      daley|dy) mdest='daley'                  ;;
    banting|ba) mdest='banting'                  ;;
       ppp1|p1) mdest='ppp1'                  ;;
       ppp2|p2) mdest='ppp2'                  ;;
       ppp3|p3) mdest='ppp3'                  ;;
       ppp4|p4) mdest='ppp4'                  ;; 
      hadar|ha) mdest='hadar'                  ;;
      spica|sp) mdest='spica'                  ;;
     pollux|po) mdest='pollux'                 ;;
     castor|ca) mdest='castor'                 ;;
        mez|mz) mdest='mez'                    ;;
             *) echo "illegal destination ! "  ;;
  esac
done

#  * Exit if parameter for mdest not suitable.

if [ "$SITE_ID" = 'Dorval' -a "$mdest" != 'pollux' -a "$mdest" != 'mez' -a "$mdest" != 'castor' -a "$mdest" != 'hadar' -a "$mdest" != 'spica' -a "$HOSTIDf" != 'jou' ] ; then
  echo "Invalid argument for machine destination $mdest"
  exit 3
fi
if [ "$SITE_ID" = 'DrvlSC' -a "$mdest" != 'hare' -a "$mdest" != 'brooks' -a "$mdest" != 'daley' -a "$mdest" != 'banting' -a "$mdest" != 'ppp1' -a "$mdest" != 'ppp2' -a "$mdest" != 'ppp3' -a "$mdest" != 'ppp4' -a "$mdest" != 'gpsc' ] ; then
  echo "Invalid argument for machine destination $mdest"
  exit 3
fi

if [ -n "$noext" -a "$noext" != '_noext' -a \( "$OS" != 'AIX' -o \( "$SITE_ID" = 'Dorval' -a "$mdest" != 'hadar' -a "$mdest" != 'spica' \) \) ] ; then
 echo "" ; echo "  archsub: Sorry, noext=$noext is invalid or illegal to use!"
 exit 4
fi

if [ -n "$noxlfqinitauto" -a "$noxlfqinitauto" != '_noxlfqinitauto' -a \( "$OS" != 'AIX' -o \( "$SITE_ID" = 'Dorval' -a "$mdest" != 'hadar' -a "$mdest" != 'spica'  \) \) ] ; then
 echo "" ; echo "  archsub: Sorry, noxlfqinitauto=$noxlfqinitauto is invalid or illegal to use!"
 exit 4
fi

if [ -n "$noxlfqinitauto" -a "$noxlfqinitauto" = '_noxlfqinitauto' -a "$float" != '_float1_openmp' -a "$float" != '_openmp' -a \( "$OS" != 'AIX' -o \( "$SITE_ID" = 'Dorval' -a "$mdest" != 'hadar' -a "$mdest" != 'spica' \) \) ] ; then
 echo "" ; echo "  archsub: Sorry, noxlfqinitauto=$noxlfqinitauto is invalid or illegal to use without openmp mode!"
 exit 4
fi

#  * Check for the validity of "float" switch on "mdest" (if applicable)

if [ "$float" = '_float2' ] ; then
 if [ "$SITE_ID" = 'Dorval' -a "$mdest" != 'mez' -a "$mdest" != 'castor' -a "$mdest" != 'hadar' -a "$mdest" != 'spica' -a "$mdest" != 'pollux' -a "$HOSTIDf" != 'jou' ] ; then
   echo "" ; echo "  archsub: Sorry, float2 switch can only be used on mez,castor,hadar,spica and pollux !"
   exit 7
 fi
 if [ "$SITE_ID" = 'DrvlSC' -a "$mdest" != 'hare' -a "$mdest" != 'brooks' -a "$mdest" != 'daley' -a "$mdest" != 'banting' -a "$mdest" != 'ppp1' -a "$mdest" != 'ppp2' -a "$mdest" != 'ppp3' -a "$mdest" != 'ppp4' -a "$mdest" != 'gpsc' ] ; then
   echo "" ; echo "  archsub: Sorry, float2 switch can only be used on hare,brooks,daley,banting,ppp1,ppp2,ppp3,ppp4 and gpsc !"
   exit 7
 fi
 if [ \( "$SITE_ID" = 'Downsview' -a "$OS" != 'Linux' \) -o \( "$SITE_ID" = 'Victoria' -a "$OS" != 'Linux' \) ] ; then
  echo "" ; echo "  archsub: Sorry, float2 switch is not a suitable option!"
  exit 8
 fi 
fi 

if [ \( "$float" = '_openmp' -o "$float" = '_float1_openmp' \) -a \(  \
     \( "$SITE_ID" = 'Dorval' -a "$mdest" != 'mez' -a "$mdest" != 'castor' -a "$mdest" != 'hadar' -a "$mdest" != 'spica' -a "$mdest" != 'pollux' -a "$HOSTIDf" != 'jou' \) -o \
     \( "$SITE_ID" = 'DrvlSC' -a "$mdest" != 'hare' -a "$mdest" != 'brooks' -a "$mdest" != 'daley' -a "$mdest" != 'banting' -a "$mdest" != 'ppp1' -a "$mdest" != 'ppp2' -a "$mdest" != 'ppp3' -a "$mdest" != 'ppp4' -a "$mdest" != 'gpsc' \) -o \
     \( "$SITE_ID" = 'Downsview' -a "$OS" != 'Linux' \) -o       \
     \( "$SITE_ID" = 'Victoria' -a "$OS" != 'Linux'     \)  \) ] ; then

  echo "" ; echo "  archsub: Sorry, openmp is not a valid option with what was specified !"
  exit 9
fi

# if [ "$float" = '_openmp' ] ; then
#   echo "" ; echo "  archsub: Sorry, openmp switch is disabled ; please rerun without it!"
#   (echo "" ; echo "  archsub: Sorry, openmp switch is disabled ; please rerun without it!") >> haltit
#   exit 9
# 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`
 xlfver=${xlfver:="$XLFVER"}
 if [ "$HOSTIDf" = 'c8f' ] ; then
  xlfver=${xlfver:='xlf13108'}
 elif [ "$HOSTIDf" = 'c1f' -o "$HOSTIDf" = 'c1h' -o "$HOSTIDf" = 'c1r' -o "$HOSTIDf" = 'c1s' ] ; then
  xlfver=${xlfver:='xlf13108'}
 elif [ "$HOSTIDf" = 'c2f' -o "$HOSTIDf" = 'c2h' -o "$HOSTIDf" = 'c2r' -o "$HOSTIDf" = 'c2s' ] ; then
  xlfver=${xlfver:='xlf13108'}
 else
  xlfver=${xlfver:='xlf12104'}
 fi
 if [ "$xlfver" = 'xlf12100' ] ; then
  echo " ARCHSUB: Altered XLF target to version 12.1.0.4"
  xlfver='xlf12104'
 fi
# else
# xlfver=""
fi

# setup for proper batch queue
 
if [ "$mdest" = 'hare' -o "$mdest" = 'brooks' -o "$mdest" = 'daley' -o "$mdest" = 'banting' -o "$mdest" = 'ppp1' -o "$mdest" = 'ppp2' -o "$mdest" = 'ppp3' -o "$mdest" = 'ppp4' -o "$mdest" = 'gpsc' ] ; then
 queue=${queue:="$mdest"}
 Azminmem_mb=2000
 mem=${mem:="$Azminmem_mb"}
 if [ "$mem" -lt "$Azminmem_mb" ] ; then
   mem="$Azminmem_mb"
 fi
elif [ "$mdest" = 'hadar' ] ; then
 queue=${queue:='hadar'}
 Azminmem_mb=2000
 mem=${mem:="$Azminmem_mb"}
 if [ "$mem" -lt "$Azminmem_mb" ] ; then
   mem="$Azminmem_mb"
 fi
elif [ "$mdest" = 'spica' ] ; then
 queue=${queue:='spica'}
 Azminmem_mb=2000
 mem=${mem:="$Azminmem_mb"}
 if [ "$mem" -lt "$Azminmem_mb" ] ; then
   mem="$Azminmem_mb"
 fi
elif [ "$mdest" = 'mez' ] ; then
 queue=${queue:='mez'}
 Azminmem_mb=500
 mem=${mem:="$Azminmem_mb"}
 if [ "$mem" -lt "$Azminmem_mb" ] ; then
   mem="$Azminmem_mb"
 fi
elif [ "$mdest" = 'castor' ] ; then
 queue=${queue:='castor'}
 Azminmem_mb=500
 mem=${mem:="$Azminmem_mb"}
 if [ "$mem" -lt "$Azminmem_mb" ] ; then
   mem="$Azminmem_mb"
 fi
elif [ "$mdest" = 'pollux' ] ; then
 queue=${queue:='pollux'}
 Azminmem_mb=500
 mem=${mem:="$Azminmem_mb"}
 if [ "$mem" -lt "$Azminmem_mb" ] ; then
   mem="$Azminmem_mb"
 fi
else
 queue=$mdest
fi

#  * Set the default time and memory limits for the batch submission job. 

# time=${time:=900}
time=${time:=1800}
mem=${mem:=250}

#  * Re-define value for afname,
#  * and ensure proper setting of "febg" switch ...

afname=lib$afname
# if [ "$mdest" = "$HOSTID" -o \( "$HOSTIDf" = 'c2h' -a "$mdest" = 'hadar' \) ] ; then
  febg=${febg:="$NONQS"}
  febg=${febg:='no'}
# else
#   febg=no
# fi

if [ "$float" = '_openmp' ] ; then
 extnsn='_openmp'
elif [ "$float" = '_float1_openmp' ] ; then
 extnsn='_float1_openmp'
else
 unset extnsn
fi 

# If running at Dorval site, allow group ownership option for
# the archive library...

# if [ "$SITE_ID" = 'Dorval' -a "$mdest" = 'pollux' ] ; then
#   DEFFGRP=${DEFFGRP:='ccrn_shr'}
#   fil_grpname=${fil_grpname:=$DEFFGRP}
#   if [ -n "$fil_grpname" ] ; then
#     Tfil_grpname=$fil_grpname
#     case $Tfil_grpname in
#     ccrn_shr|ccrn_stf|ccrn_net|ccrn_rcm|ccrn_mam|ccrn_gst) fil_grpname=$Tfil_grpname ;;
#     *) fil_grpname='ccrn_shr' ;
#        echo "" ; echo "" ; echo "  archsub : Sorry, $fil_grpname file group ownership will be used" ;
#        echo "          instead of the specified $Tfil_grpname !" ; echo ""
#     esac
#     unset Tfil_grpname
#   fi
# fi

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

#  * Process all '.f' files if "all" switch is invoked.
 
if [ "$all" = 'yes' ] ; then
  Cwd=`pwd`
  Stamp="${HOSTID}_"`date +%Y%j%H%M%S`
  if [ "$SITE_ID" != 'Dorval' -a "$SITE_ID" != 'DrvlSC' -a "$OS" = 'Linux' ] ; then
   Tmpdir="/tmp/tmp_archsub_$$_$Stamp" 
  else
   Tmpdir="$CCRNTMP/tmp_archsub_$$_$Stamp" 
  fi
  mkdir -m 755 $Tmpdir &&
  cd $Tmpdir &&
  (\rm -f Files_Differ || : ) &&
  find -L ${Cwd}/. -name '*.f' -exec obtnfil nocp {} \; || exit 10
  if [ -f Files_Differ ] ; then
   echo "Abort in archsub: conflict in the contents of files sharing the same name!"
   cd $Cwd
   \rm -rf $Tmpdir
   exit 11
  fi
  fil_list='*.f'
fi

#  * Get the requested files into fil_src by looping over files in $fil_list
#  * and appending.

for arg in $fil_list
do
  #cat $arg >> fil_src
   cat $arg | sed -e '/^[%*].*[DdEeCcKk] /d' >> fil_src
done

if [ ! -f fil_src ] ; then
  echo "Abort in archsub: file fil_src not generated"
  exit 12
fi

#  * Make and move into a temporary working directory.

mkdir -m 755 tmp_archsub_$$
cd tmp_archsub_$$
mv ../fil_src ./fil_src

#  * Copy the following batch script as first part of batch submission
#  * job (to be followed by in-lined source code).

if [ "$SITE_ID" != 'Dorval' -a "$SITE_ID" != 'DrvlSC' -a "$OS" = 'Linux' ] ; then
cat << woof1 > joba
#!/bin/sh
echo ; echo "                  Beginning of \${HOSTID} job:" \`date\` ; echo
xdir="$xdir"
xdir=\${xdir:-"\$RTEXTBLS"}
Jstamp="\${HOSTID}_"\`date +%Y%j%H%M%S\`
mkdir -m 755 /tmp/tmp_archsub_\$\$_\$Jstamp
cd /tmp/tmp_archsub_\$\$_\$Jstamp
pwd
if [ "\$OS" = 'Linux' -a "\$SITE_ID" = 'Dorval' ] ; then
#pgf90_722 -V || :
 \$PGF90 -V || :
fi
cat <<'woofsx' > fil.src
woof1
else
cat << woof1 > joba
#!/bin/sh
echo ; echo "                  Beginning of \${HOSTID} job:" \`date\` ; echo
xdir="$xdir"
xdir=\${xdir:-"\$RTEXTBLS"}
Jstamp="\${HOSTID}_"\`date +%Y%j%H%M%S\`
mkdir -m 755 \$CCRNTMP/tmp_archsub_\$\$_\$Jstamp
cd \$CCRNTMP/tmp_archsub_\$\$_\$Jstamp
pwd
if [ "\$OS" = 'Linux' -a "\$SITE_ID" = 'Dorval' ] ; then
ulimit -a || :
#pgf90_722 -V || :
 \$PGF90 -V || :
fi
cat <<'woofsx' > fil.src
woof1
fi

#  * Add the following to complete the batch script.
#  * "woofsx" is the trailer for the batch copy of the in-line code,
#  * which is inserted before this section.
#  * The script "ccsplit" splits out the "c" routines out of
#  * a file and leaves the fortran routines in a new file
#  * which can be operated on by fsplit. 

cat << woof2 > jobb
woofsx
xlfver="$xlfver"
float="$float" ; export float
if [ "$f90mod" = 'yes' ] ; then
 fix77 fil.src
 f90split < fil.src > .list
 (set -x; cat dpd*.mk || : )
#(set -x; pwd ; \ls -al || : )
  ( set -x ; \\rm -f dupl[0-9][0-9][0-9][0-9].f90 || : )
else
 ccsplit fil.src f=fortran.src
 fix77 fortran.src
 fsplit fortran.src
 ( set -x ; \\rm -f dupl[0-9][0-9][0-9][0-9].f || : )
fi
( \\rm -f fil.src || : ) 
set -x
 # if [ "\$OS" = 'AIX' -a \( "$float" = '_float1' -o "$float" = '_float1_openmp' \) ] ; then
 if [ "\$OS" = 'AIX' -a "$float" = '_float1' ] ; then
  OBJECT_MODE='32' ; export OBJECT_MODE
 fi
 if [ "\$OS" = 'AIX' -a "$noext" = '_noext' ] ; then
  F77${float}=\`echo \$F77${float} | sed -e 's/-qextname//g'\`
 fi
 if [ "\$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\`
  if [ "$noxlfqinitauto" = '_noxlfqinitauto' ] ; then
   F77${float}=\`echo \$F77${float} | sed -e 's/-qinitauto=....FFFF//g' -e 's/-qfloat=nans:/-qfloat=/g' -e 's/-qflttrap=nanq:/-qflttrap=/g'\`
  fi
  if [ "\$SITE_ID" = 'Dorval' ] ; then 
   xlfver=\${xlfver:="\$XLFVER"}
   if [ "\$HOSTIDf" = 'c8f' ] ; then
    xlfver=\${xlfver:='xlf13108'}
   elif [ "\$HOSTIDf" = 'c1f' -o "\$HOSTIDf" = 'c1h' -o "\$HOSTIDf" = 'c1r' -o "\$HOSTIDf" = 'c1s' ] ; then
    xlfver=\${xlfver:='xlf13108'}
   elif [ "\$HOSTIDf" = 'c2f' -o "\$HOSTIDf" = 'c2h' -o "\$HOSTIDf" = 'c2r' -o "\$HOSTIDf" = 'c2s' ] ; then
    xlfver=\${xlfver:='xlf13108'}
   else
    xlfver=\${xlfver:='xlf12104'}
   fi
   if [ "\$xlfver" = 'xlf12100' ] ; then
    echo " ARCHSUB: Altered XLF target to version 12.1.0.4"
    xlfver='xlf12104'
   fi
   if [ "\$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
     # F77${float}=\`echo \$F77${float} | sed -e 's/-qfloat=rrm:/-qfloat=nans:rrm:/g'\`
     F77${float}=\`echo \$F77${float} | sed -e 's/-qspillsize=[0-9]* /-qspillsize=32648 /' -e 's/-qfloat=rrm:/-qfloat=nans:rrm:/g' -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 
     #F77${float}=\`echo \$F77${float} | sed -e 's/-qfloat=rrm:/-qfloat=nans:rrm:/g' -e 's/-qflttrap=ov:/-qflttrap=nanq:ov:/g'\`
     F77${float}=\`echo \$F77${float} | sed -e 's/-qfloat=rrm:/-qfloat=nans:rrm:/g'\`
    fi
   elif [ "\$xlfver" = 'xlf13108' ] ; then 
   #F77${float}=\`echo \$F77${float} | sed -e 's/-qfloat=rrm:/-qfloat=nans:rrm:/g' -e 's/-qflttrap=ov:/-qflttrap=nanq:ov:/g'\`
    F77${float}=\`echo \$F77${float} | sed -e 's/-qfloat=rrm:/-qfloat=nans:rrm:/g'\`
   elif [ "\$xlfver" = 'xlf14103' ] ; then 
   #F77${float}=\`echo \$F77${float} | sed -e 's/-qfloat=rrm:/-qfloat=nans:rrm:/g' -e 's/-qflttrap=ov:/-qflttrap=nanq:ov:/g'\`
   #F77${float}=\`echo \$F77${float} | sed -e 's/-qfloat=rrm:/-qfloat=nans:rrm:/g'\`
   #F77${float}=\`echo \$F77${float} | sed -e 's/-qfloat=rrm:/-qfloat=nans:rrm:/g' -e 's/ -qsimd=auto//'\`
    F77${float}=\`echo \$F77${float} | sed -e 's/-qfloat=rrm:/-qfloat=nans:rrm:/g' -e 's/ -qsimd=auto//'\` ; F77${float}=\`echo \$F77${float} | sed -e 's/\$/ -qsimd=auto /'\` 
   elif [ "\$xlfver" = 'xlf10103' ] ; then 
    F77${float}=\`echo \$F77${float} | sed -e 's/-qfloat=nans:/-qfloat=/g' -e 's/-qflttrap=nanq:/-qflttrap=/g'\`
   fi
  fi
 fi
 set +x
 if [ "\$SITE_ID" = 'Dorval' -a "\$OS" = 'AIX' ] ; then
  . suppress_compilers_setup
  if [ "\$xlfver" = 'xlf12104' ] ; then
    . xlf12104_setup
    F77${float}=\`echo \$F77${float} | sed -e 's/ -qxflag=ngenstub / /' -e 's/ -qfullpath / /' -e 's/ -qreport / /' | sed -e 's/ -qnoescape / -qnoescape -qxflag=ngenstub /' -e 's/ -qdbg / -qdbg -qfullpath /' -e 's/:nofold /:nofold -qreport /'\`
  elif [ "\$xlfver" = 'xlf13108' ] ; then
    . xlf13108_setup
    F77${float}=\`echo \$F77${float} | sed -e 's/ -qxflag=ngenstub / /' -e 's/ -qfullpath / /' -e 's/ -qreport / /' | sed -e 's/ -qnoescape / -qnoescape -qxflag=ngenstub /' -e 's/ -qdbg / -qdbg -qfullpath /' -e 's/:nofold /:nofold -qreport /'\`
  elif [ "\$xlfver" = 'xlf14103' ] ; then
    . xlf14103_setup
    F77${float}=\`echo \$F77${float} | sed -e 's/ -qxflag=ngenstub / /' -e 's/ -qfullpath / /' -e 's/ -qreport / /' | sed -e 's/ -qnoescape / -qnoescape -qxflag=ngenstub /' -e 's/ -qdbg / -qdbg -qfullpath /' -e 's/:nofold /:nofold -qreport /'\`
  elif [ "\$xlfver" = 'xlf10103' ] ; then
    . xlf10103_setup
  else
   echo "" ; echo "  archsub: Sorry, a valid XLF version must be specified via 'xlfver' parameter!"
   touch haltit
  (echo "" ; echo "  archsub: Sorry, a valid XLF version must be specified via 'xlfver' parameter!") >> haltit
   exit 6 
  fi
  xlf90 -qversion
 fi
 set -x
 if [ ! -d "\$xdir/${afname}_mod_dir/."  ] ; then
   mkdir -m 1755 \$xdir/${afname}_mod_dir
 else
   (set -x ; \cp -p \$xdir/${afname}_mod_dir\/*.mod . 2>>/dev/null  && chmod u+w *.mod && \ls -ld *.mod || : )
 fi
# \$F77${float} -c *.[fc]
 if [ "$f90mod" = 'yes' ] ; then
  set -x
  # file_list=\`cat .list | sed -e 's/^ *//g' | tr '\\012' '\\040' \`
  file_list=\`f90split_reorder clean list\`
  set +x
  echo ' ' ; echo ' ====== ./f90split_reordered_subprogram_referenced_by_list :' ; echo ' '
  ( cat ./f90split_reordered_subprogram_referenced_by_list && echo ' ===========' || : )
  echo ' ' ; echo ' ====== ./f90split_reordered_subprogram_reference_to_list :' ; echo ' '
  ( cat ./f90split_reordered_subprogram_reference_to_list && echo ' ===========' || : )
  # ( \\rm -f dpds0001.mk ./f90split_reordered_subprogram_reference*_list || : )
  ( \\rm -f f90split_reordered_list .list || : )
  if [ -s "./f90split_reordered_modules_cleanup_sh" ] ; then
   echo ' ' ; echo ' ====== Deleting the following .mod files due to inlined modules:' ; echo ' '
   ./f90split_reordered_modules_cleanup_sh
   echo ' ==========='
   ( \\rm -f ./f90split_reordered_modules_cleanup_sh || : )
   set -x
  fi
  ls -ld *.mod 2>> /dev/null
 else
  file_list=*.[fc]
 fi
 if [ "$f90mod" = 'yes' -a "\$OS" = 'AIX' -a "\$SITE_ID" = 'Dorval' ] ; then
 #XtRa="-I\$xdir/${afname}_mod_dir -qmoddir \$xdir/${afname}_mod_dir"
 #XtRa="-I\$xdir/${afname}_mod_dir "
  XtRa=''
 elif [ "$f90mod" = 'yes' -a "\$OS" = 'Linux' -a \( "\$SITE_ID" = 'Dorval' -o "\$SITE_ID" = 'Downsview' \) ] ; then
 #XtRa=" -Mfixed -module \$xdir/${afname}_mod_dir"
  XtRa=' -Mfixed '
 elif [ "$f90mod" = 'yes' -a "\$OS" = 'Linux' -a "\$SITE_ID" = 'DrvlSC' ] ; then
 #XtRa=" -Mfixed -module \$xdir/${afname}_mod_dir"
  XtRa=' -fixed '
 else
  XtRa=''
 fi

 set -x
 if [ "\$OS" = 'AIX' ] ; then TMPDIR=\`pwd\` ; export TMPDIR ; fi
 for FILE in \$file_list
 do
  if [ -s "\$FILE" ] ; then
   \$F77${float} \${XtRa} -c \$FILE
   if [ "\$?" -ne 0 ] ; then
    ( set +x ; echo "Non-zero exit code from --> \$FILE <-- compile step" || : )
   fi 
  fi
 done
 if [ "\$OS" = 'AIX' ] ; then unset TMPDIR ; fi
 # (set -x ; pwd ; ls -al  || : )
 Dmy=\`echo [A-Ja-j]*.o\`
 if [ "\$Dmy" != '[A-Ja-j]*.o' ] ; then
  set +x
  if [ "$f90mod" = 'yes' ] ; then
   Fmodls=\`ls -1 [A-Ja-j]*.f90 | wc -l | tail | sed -e 's/ *//g'\`
  else
   Fmodls=\`ls -1 [A-Ja-j]*.f | wc -l | tail | sed -e 's/ *//g'\`
  fi
  Omodls=\`ls -1 [A-Ja-j]*.o | wc -l | tail | sed -e 's/ *//g'\`
  set -x
  if [ "\$Fmodls" -ne "\$Omodls" ] ; then
   echo "Archsub-note: mismatch detected; \$Fmodls source modules and \$Omodls object code modules!"
  else
   echo "Archsub: Processing \$Omodls object code modules!"
  fi
  set +x
  for FILE in \$Dmy
   do
    FILElc=\` echo \$FILE | tr '[A-Z]' '[a-z]' \`
    if [ "\$FILElc" != "\$FILE" ] ; then
     mv \$FILE \$FILElc
    fi
   done
  set -x
  chmod a+r [A-Ja-j]*.o
  ar r \$xdir/$afname.a [A-Ja-j]*.o
 fi
 unset Dmy
 Dmy=\`echo [K-Zk-z]*.o\`
 if [ "\$Dmy" != '[K-Zk-z]*.o' ] ; then
  set +x
  if [ "$f90mod" = 'yes' ] ; then
   Fmodls=\`ls -1 [K-Zk-z]*.f90 | wc -l | tail | sed -e 's/ *//g'\`
  else
   Fmodls=\`ls -1 [K-Zk-z]*.f | wc -l | tail | sed -e 's/ *//g'\`
  fi
  Omodls=\`ls -1 [K-Zk-z]*.o | wc -l | tail | sed -e 's/ *//g'\` 
  set -x
  if [ "\$Fmodls" -ne "\$Omodls" ] ; then
   echo "Archsub-note: mismatch detected; \$Fmodls source modules and \$Omodls object code modules!"
  else
   echo "Archsub: Processing \$Omodls object code modules!"
  fi
  set +x
  for FILE in \$Dmy
   do
    FILElc=\` echo \$FILE | tr '[A-Z]' '[a-z]' \`
    if [ "\$FILElc" != "\$FILE" ] ; then
     mv \$FILE \$FILElc
    fi
   done
  set -x
  chmod a+r [K-Zk-z]*.o
  ar r \$xdir/$afname.a [K-Zk-z]*.o
 fi
# ar r \$xdir/$afname.a *.o
 if [ "$SITE_ID" = 'Dorval' ] ; then
  chgrp $fil_grpname \$xdir/$afname.a
 fi
 chmod a+r \$xdir/$afname.a

## MODULE(s)...

 set +x
 echo ""
 set -x
 Dmy=\`echo *.mod\`
 set +x
 Moduls_copied='no'
 if [ "\$Dmy" != '*.mod' -a -d "\$xdir/${afname}_mod_dir/." ] ; then
  (\chmod u+w *.mod && \chmod a+r *.mod || : )
  set +x
 #set -x
  for FILE in \$Dmy
   do
    FILElclf90=\` echo \$FILE | sed -e 's/\.mod/\.f90/' \`
    if [ -s "\$xdir/${afname}_mod_dir/\$FILE" -a ! -s "\$FILElclf90" ] ; then
     Md5lcl=\`md5sum ./\$FILE | \$AWK '{ print \$1 ; }'\`
     Md5lib=\`md5sum \$xdir/${afname}_mod_dir/\$FILE | \$AWK '{ print \$1 ; }'\`
     if [ "\$Md5lcl" != "\$Md5lib"  ] ; then
      (set -x ; \cp -p \$FILE \$xdir/${afname}_mod_dir/\$FILE || : )
      Moduls_copied='yes'
     fi
    else
      (set -x ; \cp -p \$FILE \$xdir/${afname}_mod_dir/\$FILE || : )
      Moduls_copied='yes'
    fi
   done
   #set -x
    if [ "\$Moduls_copied" = 'yes' ] ; then (set -x ; \ls -alt \$xdir/${afname}_mod_dir/. || : ) ; fi
   #(set -x ; \ls -al \$xdir/${afname}_mod_dir/. || : )
#elif [ -d "\$xdir/${afname}_mod_dir/." ] ; then
# if [ "\$Moduls_copied" = 'yes' ] ; then (set -x ; \ls -alt \$xdir/${afname}_mod_dir/. || : ) ; fi
 fi
 ( rmdir \$xdir/${afname}_mod_dir 2>>/dev/null || : )

## If requested, update f90split dependencies files under "$updtmk" 

 if [ "$f90mod" = 'yes' -a -n "$updtmk" -a -d "$updtmk/." ] ; then
  set -x
  ( ls -ld $updtmk/*.mk $updtmk/f90split_reordered_* || : )
  if [ -s "$updtmk/dpds0001.mk" ] ; then
   (\\rm -f f90split_reordered_subprogram_referenced_* Tmpdpds0001.mk || : )
   cat $updtmk/*.mk dpds0001.mk | sort | uniq > Tmpdpds0001.mk
   mv Tmpdpds0001.mk dpds0001.mk
   Ordrd_Listx=\`f90split_reorder list\`
  fi
  (\\rm -f f90split_reordered_list || : )
  chmod a+r dpds0001.mk f90split_reordered_*
  # cp -p dpds0001.mk f90split_reordered_* $updtmk/. 
  # Updt_f90split_Listx=\` echo dpds0001.mk f90split_reordered_* \`
  Updt_f90split_Listx='dpds0001.mk f90split_reordered_subprogram_reference_to_list f90split_reordered_subprogram_referenced_by_list'
  for Fspltfil in \${Updt_f90split_Listx}
   do
    if [ -s "./\${Fspltfil}" ] ; then
     if [ -s $updtmk/\${Fspltfil} ] ; then
      cmp -s \${Fspltfil} $updtmk/\${Fspltfil} && skip='yes' || skip='no'
      if [ "\${skip}" = 'no' ] ; then
       cp -p \${Fspltfil} $updtmk/.new_\${Fspltfil} ; [ -s $updtmk/\${Fspltfil} ] && mv $updtmk/\${Fspltfil} $updtmk/.previous_\${Fspltfil} ; mv $updtmk/.new_\${Fspltfil} $updtmk/\${Fspltfil}
      fi
     else
      cp -p \${Fspltfil} $updtmk/\${Fspltfil}
     fi
    fi
   done
 fi
 
## fi
set +x

cd ..
\rm -rf tmp_archsub_\$\$_\$Jstamp
( rmllfls || : )
woof2

#  * Build the batch job.

cat joba fil_src jobb > Job

if [ "$febg" = 'no' ] ; then
 #  * Submit the batch job.

 # nqs_jobname=`echo ${afname}-ar | cut -c1-14`
 nqs_jobname=`echo ${afname}-ar`
 Pwd=`pwd`
 mv Job $HOME/tmp/BATCH_job_archsub_$Stamp_$$
#qsub -q $queue Job -lT $time -lM ${mem}mb -eo -o "$OUTPUTQ/${afname}-ar.$$" \
 cd $HOME/tmp
 
 $CCCQSUB -q $queue BATCH_job_archsub_$Stamp_$$ -lT $time -lM ${mem}mb -eo -o "$OUTPUTQ/${afname}-ar.$$" \
      -r $nqs_jobname
#     -r "${afname}-ar"
 cd $Pwd
 \rm $HOME/tmp/BATCH_job_archsub_$Stamp_$$

else
 
## if [ "$HOSTID" = "$mdest" -o "$xf90" = 'yes' ] ; then
#if [ "$HOSTID" = "$mdest" ] ; then
#if [ "$mdest" = "$HOSTID" -o \( "$HOSTIDf" = 'c2h' -a "$mdest" = 'hadar' \) ] ; then

  chmod u+x Job
  if [ "$SITE_ID" != 'Dorval' -a "$OS" = 'Linux' ] ; then
   mv Job  /tmp/tmp_archsub_job$$
   echo "rm -f /tmp/tmp_archsub_job$$" >> /tmp/tmp_archsub_job$$
   # echo "OUTPUTQ="$OUTPUTQ
   if [ "$wait" = 'yes' ] ; then
    /tmp/tmp_archsub_job$$ > $OUTPUTQ/${afname}-ar.$$ 2>$OUTPUTQ/${afname}-ar.$$
   else
    nohup /tmp/tmp_archsub_job$$ > $OUTPUTQ/${afname}-ar.$$ 2>$OUTPUTQ/${afname}-ar.$$ &
   fi
  else
   mv Job  $CCRNTMP/tmp_archsub_job$$
   echo "rm -f $CCRNTMP/tmp_archsub_job$$" >> $CCRNTMP/tmp_archsub_job$$
   if [ "$wait" = 'yes' ] ; then
    $CCRNTMP/tmp_archsub_job$$ > $OUTPUTQ/${afname}-ar.$$ 2>$OUTPUTQ/${afname}-ar.$$
   else
    nohup $CCRNTMP/tmp_archsub_job$$ > $OUTPUTQ/${afname}-ar.$$ 2>$OUTPUTQ/${afname}-ar.$$ &
   fi
  fi

#else
#
# echo "archsub: you have to run archsub on $mdest for background mode!"
#
#fi

fi

# mv Job ../

#  * Cleanup temporary files and remove working directory.

cd ..
# ( set -x ; pwd ; ls -ld tmp_archsub_$$ )
\rm -rf tmp_archsub_$$

# * Cleanup if "all" switch is invoked...

if [ "$all" = 'yes' ] ; then
 cd $Cwd
 # ( set -x ; pwd ; ls -ld $Tmpdir )
 \rm -rf $Tmpdir
fi

exit
