###################################################################
echo "------------------------------------------------"
echo "J100 - " 
echo "------------------------------------------------"
echo "History: OCT 1996 - First implementation of this new script."
echo "History: FEB 1998 - Dropped /com/ncepdate (2-digit year)"
echo "                    from utility zdate.sh"
echo "History: MAR 1999 - Adapted for running on CLASS-VIII IBM"
echo "History: MAY 2006 - Modified for the new GEFS"
#####################################################################

cd $DATA

#####################################################################
#
# START FLOW OF CONTROL
#
# 1) Create nmcdate.
# 2) Manage /com directories.
# 3) Manage /com/output directories.
# 4) Manage /nwges directories.
# 5) Backup spalog file.
#####################################################################

########################################
set -x
msg="JOB $job HAS BEGUN"
postmsg "$jlogfile" "$msg"
##########################################

set +x
echo " "
echo "############################################################"
echo "       1.    Create the ncepdate."
echo "############################################################"
echo " "
set -x

export pgm="zdate"
msg="$pgm has begun."
postmsg "$jlogfile" "$msg"

export NEW_DIR=/com/date
$utilities/zdate.sh
export err=$?;err_chk

# Run setpdy and initialize PDY variables
sh $utilscript/setpdy.sh
. PDY

# LIST PDYs
echo PDY="$PDY"
echo PDYm1="$PDYm1"
echo PDYm2="$PDYm2"
echo PDYm3="$PDYm3"
echo PDYm4="$PDYm4"
echo PDYm5="$PDYm5"
echo PDYm6="$PDYm6"
echo PDYm7="$PDYm7"

# Make PDYp1 for tomorrows date
export PDYp1=`sh /nwprod/util/ush/finddate.sh $PDY d+1`

#########################################################################
#
# 2.  Manage /com directories
#
#########################################################################

for NET in gfs mrf nam ndas gdas wave gefs cmce ecmwf sst ukmet arkv \
        hourly vaftad hysplit aer o14 shf sni sno stf hur \
        cdas fog sice uv dump nam_pcpn_anal \
	sref wsr aqm hiresw loadview
do
  RUN=$NET
  keep=10
  case $NET in
    nam_pcpn_anal) RUN=hourly;;
    radar) RUN=hourly;;
    nesdis) RUN=hourly;;
    gefs)  RUN=gens;;
    cmce)  RUN=gens;;
    jma)  RUN=mrf;;
    ecmwf) RUN=mrf;;
    wsr) RUN=wsr;;
    gdas) RUN=gfs;;
    gfs) RUN=gfs;;
    ndas) RUN=nam;;
    nam)  RUN=nam;;
    sst) RUN=gfs;;
    ukmet) RUN=mrf;;
    aer) RUN=ingest;;
    o14) RUN=ingest;;
    shf) RUN=ingest;;
    sni) RUN=ingest;;
    sno) RUN=ingest;;
    stf) RUN=ingest;;
    fog) RUN=omb;;
    sice) RUN=omb;;
    uv) RUN=hourly;;
    aqm) RUN=aqm;;
    hiresw) RUN=hiresw;;
    loadview) RUN=logs;;
  esac
  com=/com/${RUN}/${envir}
  
  if test $cyc -eq 12
  then
    if [ $NET = "hur" ]
    then
      for hurcyc in 00 06 12 18
      do
        mkdir -p $com/${NET}.${PDYp1}${hurcyc}
        chmod 775 $com/${NET}.${PDYp1}${hurcyc}
      done
    else
      mkdir -p $com/${NET}.$PDYp1
      chmod 775 $com/${NET}.$PDYp1
    fi
  fi
done  

#
# Manage NAWIPS data directories
#
if [ $cyc -eq 12 ] ; then
  RUN=nawips
  for NET in nam gfs mrf gdas gefs cmce ghm wave ecmwf ukmet ice vaftad mdl sref
  do
    com=/com/${RUN}/${envir}
    mkdir -p $com/${NET}.$PDYp1
    chmod 775 $com/${NET}.$PDYp1
  done
fi

#####################################################################
#  3.  /com/output
#  Set up /com/output directory if it is a new day.
#  Delete /prod/ directories older than 5 days
#  and /test/ directories older than yesterday.
#####################################################################

if test $cycle = "t00z"
then
   mkdir -p /com/output/prod/$PDY
   chmod 775 /com/output/prod/$PDY
   mkdir -p /com/output/dev/$PDY
   chmod 775 /com/output/dev/$PDY
   mkdir -p /com/output/para/$PDY
   chmod 775 /com/output/para/$PDY
   mkdir -p /com/output/test/$PDY
   chmod 775 /com/output/test/$PDY

   ln -sf /com/output/prod/$PDY /com/output/prod/today
   ln -sf /com/output/dev/$PDY /com/output/dev/today
   ln -sf /com/output/para/$PDY /com/output/para/today
   ln -sf /com/output/test/$PDY /com/output/test/today

   mkdir -p /com/output/transfer/$PDY
   chmod 775 /com/output/transfer/$PDY

   ln -sf /com/output/transfer/$PDY /com/output/transfer/today
fi

##########################################
# 4.  Manage nwges directories.
##########################################
for NET in gfs nam gdas mrf ndas sref gefs
do
  mkdir -p /nwges/prod/${NET}.${PDY}
  mkdir -p /nwges/prod/${NET}.${PDYp1}
done

##########################################
# 5.  Copy spalog to a hold file
##########################################

cp /com/logs/spalog /com/logs/spalog.hold


#####################################################################

# GOOD RUN
set +x
echo "**************JOB PROD_SETUP COMPLETED NORMALLY ON THE IBM"
echo "**************JOB PROD_SETUP COMPLETED NORMALLY ON THE IBM"
echo "**************JOB PROD_SETUP COMPLETED NORMALLY ON THE IBM"
set -x

msg="JOB $job HAS COMPLETED NORMALLY."
echo $msg
postmsg "$jlogfile" "$msg"

############## END OF SCRIPT #######################
