#! /bin/sh

#    Dec 03/08 - F.Majaess (Revised for sa/saiph, ib/dorval-ib, af/alef)
#    Oct 16/06 - F.Majaess (Revised for ma/maia, ns/naos)
#    Mar 01/06 - F.Majaess (Revised for rg/rigel)
#    Feb 15/05 - F.Majaess (Revised for AIX/Linux setup in Victoria)
#    Jun 20/03 - F.Majaess (Revised for az/azur)
#    Feb 25/02 - F.Majaess (Revised for ya/yata)
#    Sep 14/00 - F.Majaess (Revised for tu/tsunami)
#    Jan 05/00 - F.Majaess (Added kz/kaze)
#    Jan 21/99 - F.Majaess (Added yo/yonaka)
#    Jul 14/97 - F.Majaess (Added sx/hiru)
#    Mar 18/97 - F.Majaess (Revise for group restructuring)
#    Mar 14/96 - F.Majaess (Added sx4)
#    Jan 09/96 - FM (Revise to use access/delete)
#    Jun 21/95 - F.Majaess (Added orion, gandalf & aragorn)
#    Nov 08/94 - F.Majaess (Modify for new destination identifiers)
#    Dec 11/92 - E. Chan   
 
#id  chgnam  - submits a job to change the name of a file in the 
#id            user's run directory   
 
#    AUTHOR - E. Chan  
 
#hd  PURPOSE - "chgnam" submits a job to the destination machine and 
#hd            changes the name of the specified file in the user's 
#hd            run directory ('$RUNPATH'). The corresponding link on 
#hd            the official CCRD/N data directory ('$DATAPATH') is 
#hd            modified accordingly.
#hd 
#hd            By default, the file with the highest edition number will be
#hd            affected. The "ed" parameter may be used to specify a 
#hd            particular edition number.
 
#pr  PARAMETERS:
#pr  
#pr    POSITIONAL
#pr 
#pr      oldpfn = name of saved file 
#pr  
#pr      newpfn = new name to be used for the file
#pr   
#pr    PRIMARY
#pr   
#pr      Note: One of 'ha/sp/po/ca/mz'  in Dorval.
#pr            In Victoria, default destination: "$HOSTID"  for Linux.
#pr
#pr          ha = to target job to IBM (hadar).
#pr
#pr          sp = to target job to IBM (spica).
#pr
#pr          po = to target job to Linux (pollux).
#pr
#pr          ca = to target job to Linux (castor).
#pr
#pr          mz = to target job to Linux (mez).
#pr
#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 for the targeted officially saved 
#pr               file.
#pr               (=$DEFFGRP).
#pr
#pr          ed = edition number of saved file (default: the latest edition)
#pr 
#pr        time = time limit for job in seconds (=200)
#pr  
#pr         mem = memory limit for job in megabytes (=25)
#pr  
 
#ex  EXAMPLE: 
#ex  
#ex  chgnam dmm17sp emm17sp fil_grpname='ccrn_shr' ha
#ex 
#ex  The above example changes the name and possibly the group ownership
#ex  on Hadar of the latest edition of "dmm17sp" in the user's own run 
#ex  directory to the latest edition of "emm17sp". The corresponding link 
#ex  on the official data directory is changed accordingly.

#  * Obtain any specified option. 

for arg in $@
do
  case $arg in
        -x) set $arg                                       ;;
       *=*) eval $arg                                      ;;
      ed=*) ed=`expr $arg : 'ed=\([0-9]*\)'`               ;;
        ha) mdest=${mdest:='hadar'}                        ;;
        sp) mdest=${mdest:='spica'}                        ;;
        po) mdest=${mdest:='pollux'}                       ;;
        ca) mdest=${mdest:='castor'}                       ;;
        mz) mdest=${mdest:='mez'}                       ;;
    mamccc) mdest=${mdest:='mamccc'}            ;;
    odin) mdest=${mdest:='odin'}            ;;
    climate) mdest=${mdest:='climate'}            ;;
         *) tmp1=${oldpfn:+$arg} ; newpfn=${newpfn:=$tmp1} ; 
            oldpfn=${oldpfn:=$arg}
  esac
done

#  * Ensure that both filenames are specified.

if [ -z "$oldpfn" -o -z "$newpfn" ] ; then
  echo "Error in chgnam: must specify both old and new filenames"
  exit 1
fi

#  * Set the defaults.

time=${time:=200}
mem=${mem:=25}
nonqs=${nonqs:=$NONQS}
nonqs=${nonqs:='no'}

#  * Prompt for a destination if none was specified.

while [ -z "$mdest" ]
do
 if [ "$SITE_ID" = 'Victoria' ] ; then
    mdest="$HOSTID"
 elif [ "$SITE_ID" = 'Dorval' ] ; then
  echo "please enter a destination; ha/sp/po/ca/mz: > \\c"
  read tmdest
  case $tmdest in
      ha) mdest='hadar'                 ;;
      sp) mdest='spica'                 ;;
      po) mdest='pollux'                ;;
      ca) mdest='castor'                ;;
      mz) mdest='mez'                   ;;
       *) echo "illegal destination ! " ;;
  esac
 elif [ "$SITE_ID" = 'Downsview' ] ; then
    mdest="$HOSTID"
 else
  echo "please enter a destination: > \\c"
  read mdest
 fi
done

#  * If edition number is specified remove leading zeros in the edition 
#  * number and ensure that three digits are used. 

if [ -n "$ed" ] ; then
  ed=`expr $ed + 0`
  if [ "$ed" -lt 100 ] ; then
    ed=0$ed
  fi
  if [ "$ed" -lt 10 ] ; then
    ed=0$ed
  fi
  Ted="ed=$ed"
fi

#  * Construct and submit the job.


if [ "$mdest" = 'hadar' ] ; then
  outdest=$OUTPUTQ/chgnam_${oldpfn}.ha.$$
elif [ "$mdest" = 'spica' ] ; then
  outdest=$OUTPUTQ/chgnam_${oldpfn}.sp.$$
elif [ "$mdest" = 'pollux' ] ; then
  outdest=$OUTPUTQ/chgnam_${oldpfn}.po.$$
elif [ "$mdest" = 'castor' ] ; then
  outdest=$OUTPUTQ/chgnam_${oldpfn}.ca.$$
elif [ "$mdest" = 'mez' ] ; then
  outdest=$OUTPUTQ/chgnam_${oldpfn}.mz.$$
else
  outdest=$OUTPUTQ/chgnam_${oldpfn}.$$
fi
# fil_grpname=${fil_grpname:='ccrn_shr'}
if [ "$SITE_ID" = 'Dorval' ] ; then
 DEFFGRP=${DEFFGRP:='ccrn_shr'}
 fil_grpname=${fil_grpname:=$DEFFGRP}
fi
if [ -n "$fil_grpname" ] ; then
 Tfil_grpname="fil_grpname=$fil_grpname && export fil_grpname "
else
 Tfil_grpname=" : "
fi


# qsub -q $mdest -lT $time -lM ${mem}mb -r chgnam.$oldpfn \
# qsub -q $mdest -lT $time -lM ${mem}mb -r chgnam \
#                -eo -o $outdest <<endjob

if [ "$nonqs" = 'yes' ] ; then
 if [ "$mdest" = "$HOSTID" ] ; then
  if [ "$OS" = 'Linux' ] ; then
   Batch_job="/tmp/tmp_chgnam_job$$"
   RM_Batch_job="rm -f $Batch_job"
   line="cat > $Batch_job ; chmod u+x $Batch_job ; nohup $Batch_job 1>>$outdest 2>>$outdest & " 
  else
   RM_Batch_job=':'
   line="nohup sh -s - 1>>$outdest 2>>$outdest & "
  fi
 else
  echo "CHGNAM: Sorry, nohup is limited to the same platform"
  echo "        mdest=$mdest; HOSTID=$HOSTID !"
  exit 1
 fi
else
 line="qsub -q $mdest -lT $time -lM ${mem}mb -r chgnam -eo -o $outdest"
fi
RM_Batch_job=${RM_Batch_job:-':'}
cd $HOME/tmp
(cat <<endjob) | eval "$line"
#!/bin/sh
# set -x
#
$Tfil_grpname
Stamp="\${HOSTID}_"\`date +%Y%j%H%M%S\`
mkdir -m 755 \$CCRNTMP/tmp_chgnam_\$\$_\$Stamp
cd \$CCRNTMP/tmp_chgnam_\$\$_\$Stamp
#
#  * Access the file and change it's name.
#  * The latest edition is used if the edition number
#  * is not specified.
#
access chgnam_file $oldpfn $Ted &&
ln -s chgnam_file n_chgnam_file &&
save n_chgnam_file $newpfn &&
delete chgnam_file &&
release n_chgnam_file &&
echo "$oldpfn has been changed to $newpfn" &&
cd \$CCRNTMP &&
\rm -r -f \$CCRNTMP/tmp_chgnam_\$\$_\$Stamp &&
exit 0 ||
echo "Error in chgnam: file $oldpfn does not exist" && 
cd \$CCRNTMP &&
\rm -r -f \$CCRNTMP/tmp_chgnam_\$\$_\$Stamp &&
( rmllfls || : ) && $RM_Batch_job &&
exit 1 || ( rmllfls || : ) && $RM_Batch_job && exit 2
endjob

exit
