#!/bin/sh
set -x
#=======================================================================
# delete a list of files                                --- del_list ---
# $Id: del_list_jobdef 665 2012-04-03 19:20:25Z acrnrls $
#=======================================================================
#
#     keyword :: del_list
# description :: delete a list of files from DATAPATH/RUNPATH

  set -a
  . betapath2

  # These variables are set when the job string is created
  run_start_year=NotSet  # memory99=1
  run_start_month=NotSet # memory99=1
  run_stop_year=NotSet   # memory99=1
  run_stop_month=NotSet  # memory99=1
  current_year=NotSet    # memory99=1
  current_month=NotSet   # memory99=1
  previous_year=NotSet   # memory99=1
  previous_month=NotSet  # memory99=1
  next_year=NotSet       # memory99=1
  next_month=NotSet      # memory99=1

  jobname=del_list;
  runid="job000";
  uxxx='uxxx'; del_list_uxxx=$uxxx;
  del_list_prefix="${del_list_uxxx}_${runid}";
  del_list_prefix_=${del_list_prefix}_
  prefix=$del_list_prefix_
  crawork="${runid}_job"; username="acrnxxx"; user="XXX";

  RUNID=`echo "${runid}"|tr '[a-z]' '[A-Z]'`;
  nqsprfx="${runid}_"; nqsext='';

  year=yyy; mon=mm

#  * ........................... Parmsub Parameters ....................

  lopgm="lopgm"; stime="1800"; memory1="1gb";

  # Allow the user to reset CCRNTMP and/or RUNPATH
  del_list_RUNPATH=''
  RUNPATH=${del_list_RUNPATH:=$RUNPATH}
  del_list_CCRNTMP=''
  CCRNTMP=${del_list_CCRNTMP:=$CCRNTMP}

  # Alternate path to a directory where .queue/.crawork will be found
  JHOME=''

  if [ -n "$JHOME" -a x"$JHOME" != x"$HOME" ]; then
    # Allow optional reset of DATAPATH/RUNPATH
    JHOME_DATA=''
    DATAPATH=${JHOME_DATA:=$DATAPATH}
    RUNPATH=${JHOME_DATA:=$RUNPATH}
    # Allow optional reset of CCRNTMP
    JHOME_RUN=''
    CCRNTMP=${JHOME_RUN:=$CCRNTMP}
  fi

  # ---Start_submit_ignore_code----

  stamp=`date "+%j%H%M%S"$$`

  # Use -e option if recognized by echo
  if [ "X`echo -e`" = "X-e" ]; then
    echo_e() { echo ${1+"$@"}; }
  else
    echo_e() { echo -e ${1+"$@"}; }
  fi

  # bail is a simple error exit routine
  # Note: we write the error directly to a file in ~/.queue so that this
  #       info is not lost if/when stdout is not returned
  this_host=`hostname|cut -d'.' -f1`
  error_out="${JHOME:-$HOME}/.queue/error_del_list_${runid}_${this_host}_$stamp"
  [ ! -z "$error_out" ] && rm -f $error_out
  bail(){
    echo_e `date`" $this_host $runid --- del_list: $*"
    echo_e `date`" $this_host $runid --- del_list: $*" >>$error_out
    exit 1
  }

  # If either reset_start_year or reset_stop_year are set then they must be
  # of the form old_year:new_year (ie a colon separated pair of integers)
  # where the first integer is the year that needs to be changed
  # and the second integer is the year that it will be changed to.
  # These may potentially change the value of start_year or stop_year that
  # are defined after the call to make_file_name_list below
  del_list_reset_start_year=''
  reset_start_year=${del_list_reset_start_year:=''}
  del_list_reset_stop_year=''
  reset_stop_year=${del_list_reset_stop_year:=''}
  # reset_end_year is defined here for backward compatibility
  # It is effectively equivalent to reset_stop_year.
  del_list_reset_end_year=''
  reset_end_year=${del_list_reset_end_year:=''}

  # This invocation of make_file_name_list will process the *_year and *_months
  # variables defined above and output a file containing definitions for
  # start_year, start_mon, stop_year, stop_mon, end_year, end_mon
  fopts=''
  del_list_mon_offset=''
  if [ -n "$del_list_mon_offset" ]; then
    # Set a user supplied month offset
    eval fopts=\"--mon_offset\=$del_list_mon_offset\"
  fi
  tmp_file_list="tmp_file_list_${runid}_${stamp}"
  make_file_name_list $fopts --dates_only $tmp_file_list || \
    bail "Problem in make_file_name_list"

  # Verify that the output list is not empty
  [ ! -s "$tmp_file_list" ] && bail "Unable to create file list"

  # A file list was created ...source it
  # This will define start_year, start_mon, stop_year, stop_mon,
  # end_year and end_mon in the current environment
  : ; . $tmp_file_list
  rm -f $tmp_file_list

  # These variables are defined for backward compatability
  del_list_start_year=$start_year
  del_list_start_mon=$start_mon
  del_list_end_year=$stop_year
  del_list_end_mon=$stop_mon

  del_list_leave_last_mon=0
  if [ $del_list_leave_last_mon -eq 1 ]; then
    # Leave the last month on disk by shifting these dates back 1 month
    if [ -z "$reset_start_year" ]; then
      del_list_vtmp=`echo $del_list_start_year $del_list_start_mon|\
                     awk '{m=$2-1;if(m==0){m=12;y=$1-1}else{y=$1};
                           printf "%2.2d %3.3d",m,y}' -`
      del_list_start_mon=`echo $del_list_vtmp|awk '{printf "%2.2d",$1}' -`
      del_list_start_year=`echo $del_list_vtmp|awk '{printf "%3.3d",$2}' -`
    fi
    if [ -z "$reset_end_year" ]; then
      del_list_vtmp=`echo $del_list_end_year $del_list_end_mon|\
                     awk '{m=$2-1;if(m==0){m=12;y=$1-1}else{y=$1};
                           printf "%2.2d %3.3d",m,y}' -`
      del_list_end_mon=`echo $del_list_vtmp|awk '{printf "%2.2d",$1}' -`
      del_list_end_year=`echo $del_list_vtmp|awk '{printf "%3.3d",$2}' -`
    fi
  fi

  start_year=$del_list_start_year
  start_mon=$del_list_start_mon
  stop_year=$del_list_end_year
  stop_mon=$del_list_end_mon
  end_year=$del_list_end_year
  end_mon=$del_list_end_mon

  # rtd parameters
  # PhysA = on/off     physical atmosphere variables
  # PhysO = on/off     physical ocean variables
  # CarbA = on/off     atmosphere carbon variables
  # CarbO = on/off     ocean carbon variables
  # CarbL = on/off     land carbon variables
  PhysA=on
  PhysO=on
  CarbA=on
  CarbO=on
  CarbL=on

  # RHS for certain ocean variables
  rhs_avg=off

  # <<INSERT_DEL_LIST>>

  # If delete_file_list is defined then add the file names defined therein
  # to the list of files to be deleted. This will be in addition to any
  # provided by the user when this string was created.
  # The file provided in delete_file_list must be available via "access"
  delete_file_list=''

  if [ -n "$delete_file_list" ]; then
    # access a file containing the list of file names
    eval dpalist_opt=dpalist\=off
    access LOCAL_FILE_LIST_$stamp $delete_file_list nocp=off $dpalist_opt

    # Determine how many, if any, fileN variables are defined
    # Any file names supplied by the user at the time this string
    # is created will already be defined by this point.
    # Note that the variables fileN must be exported so that the
    # following script will have access to them
    N=`perl -e'$n=1;
               $key="file1";
               while ($ENV{$key}) {$n++; $key="file$n"}
               print $n-1'`

    # Create a shell script that may be "sourced" to add the
    # file names provided in delete_file_list to the current env
    # If delete_file_list contains comment lines of the form "#stop"
    # then an extra fileN=:stop will be defined and used to terminate
    # sub list processing in the script below.
    awk -v N=$N -v fname=fileN_from_list_$stamp '
        BEGIN {print "# Files added from list" > fname}
        $0 ~ /^[ \t]*#/ {if ($0 ~ /^[ \t]*#[ \t]*[Ss][Tt][Oo][Pp][ \t]*$/) {exit};
                         next}
        $0 ~ /^[ \t]*$/ {next}
        {N+=1; printf "file%d=%s\n",N,$1 >> fname}' LOCAL_FILE_LIST_$stamp
    [ ! -s fileN_from_list_$stamp ] && bail "Error processing $delete_file_list"
    chmod u+x fileN_from_list_$stamp

    release LOCAL_FILE_LIST_$stamp

    # Source this file to bring the fileN variables into the current env
    : ; . fileN_from_list_$stamp
    rm -f fileN_from_list_$stamp

    # Re-evaluate the number of fileN variables defined in the current
    # environment and set join appropriately
    N=`perl -e'$n=1;
               $key="file1";
               while ($ENV{$key}) {$n++; $key="file$n"}
               print $n-1'`
    eval join\=$N
  fi

  if [ 2 -eq 1 ]; then
    rm -f ${JHOME:-$HOME}/tmp/t_del_list
    touch ${JHOME:-$HOME}/tmp/t_del_list
    n=0
    while [ $n -lt $join ]; do
      n=`expr $n + 1`
      eval echo "del_list: file$n = \$file$n" >> ${JHOME:-$HOME}/tmp/t_del_list
    done
    exit 1
  fi

  # ---Stop_submit_ignore_code----

#  * ............................ Condef Parameters ............................

  noprint=on       # do not return stdout to ~/.queue
  nextjob=on       # more jobs in the job string will follow

#  * ............................. Deck Definition .............................

  # Note: If delet is placed inside a conditional then submit3 will fail.
  # submit3 will ignore everthing after the delet line when it creates an
  # internal file named job.info. It then attempts to source job.info and dies
  # due to a syntax error (the final fi is missing).
  . delet.dk

#end_of_job
