#!/bin/csh -f

# ---------------------------
# For USAGE, TYPE: movestat -help
# ---------------------------

set ext = gif

# Check if BATCH job
# ------------------
if( $?PBS_NODEFILE ) then
    if(! -e ~/.WEBSERVER ) then
EXIT:
       echo " "
       echo "You must run -movestat- to create ~/.WEBID and ~/.WEBSERVER"
       echo " "
       exit
   else
       set   server  = `cat ~/.WEBSERVER`
       if( .$server != .train.gsfc.nasa.gov && \
           .$server != .polar.gsfc.nasa.gov ) goto EXIT
   endif
   if(! -e ~/.WEBID ) then
       goto EXIT
   else
       set webid = `cat ~/.WEBID`
   endif

else  # Interactive Job

# Initial setup of WEBID and WEBSERVER
# ------------------------------------
   if(! -e ~/.WEBSERVER ) then
SERVER:
     set server = NULL 
     echo " "
     echo "Enter WEB Server to use:  train"
     echo "                          polar"
     set dummy   = $<
     if($dummy  == train ) set server = train.gsfc.nasa.gov
     if($dummy  == polar ) set server = polar.gsfc.nasa.gov
     if($server == NULL  ) then
         goto SERVER
     else
         echo $server > ~/.WEBSERVER
     endif
   else
     set   server  = `cat ~/.WEBSERVER`
     if( .$server != .train.gsfc.nasa.gov && \
         .$server != .polar.gsfc.nasa.gov ) goto SERVER
   endif

   if(! -e ~/.WEBID ) then
WEBID:
     echo " "
     echo "Enter WEB ID to use on ${server}:"
     set webid = $<
     echo "Hit -ENTER- to confirm: $webid"
     set dummy = $<
     if($dummy != "" ) then
         goto WEBID
     else
         echo $webid > ~/.WEBID
     endif
   else
     set webid = `cat ~/.WEBID`
   endif

endif   # End BATCH JOB Test

if($server == train.gsfc.nasa.gov ) set www = /gmao/intranet/research/modeling/agcm/geos5
if($server == polar.gsfc.nasa.gov ) set www = /www/html/intranet/research/modeling/agcm/geos5

# ---------------------------------------------------------------
# ---------------------------------------------------------------

@ nmax = $#argv

set year     = 'NULL'
set season   = 'NULL'
set expid    = 'NULL'
set pltdir   = 'NULL'
set corcmprc = 'corcmp.rc'
set plots    = 'NULL'

@       n  = 1
while( $n <= $nmax )
       if( "$argv[$n]" == "-help"  ) goto USAGE
       if( "$argv[$n]" == "-expid" ) then
                    @ n = $n + 1
             set expid  = $argv[$n]
       endif
       if( "$argv[$n]" == "-year" ) then
                    @ n = $n + 1
             set year   = $argv[$n]
       endif
       if( "$argv[$n]" == "-season" ) then
                    @ n = $n + 1
             set season = $argv[$n]
       endif
       if( "$argv[$n]" =~ \-plot* ) then
                    @ n = $n + 1
             set plots  = $argv[$n]
       endif
       if( "$argv[$n]" == "-pltdir" ) then
                    @ n = $n + 1
             set pltdir = $argv[$n]
       endif
       if( "$argv[$n]" == "-rc" ) then
                    @ n = $n + 1
             set corcmprc = $argv[$n]
       endif
     @ n = $n + 1
end

# Seasonal Plots
# --------------
if( $year == 'NULL' ) then
      echo ' '
      echo 'You must supply a YEAR to move'
      goto USAGE
endif

# Extract EXPID from PLTDIR Directory (assuming 2nd-to-last node)
# ---------------------------------------------------------------
if( $pltdir == 'NULL' ) set pltdir = `pwd`
   @ end     = 99
   @ counter = 2
   while ( $counter != $end )
      set  dummy  = `echo $pltdir | cut -d"/" -f$counter`
      if( $dummy != '' ) then
         @ counter  = $counter + 1
      else
         @ counter  = $counter - 2
                             set  source = `echo $pltdir | cut -d"/" -f1-$counter`
      if( $expid == 'NULL' ) set   expid = `echo $pltdir | cut -d"/"   -f$counter`
         @ counter = 99
      endif
   end

   set webdir = $expid/$year

# Construct Experiment Directories from corcmp.rc
# -----------------------------------------------
     set  DSCS  = ''
     set  DESCS = `cat $corcmprc | grep DSC | grep -v \# | grep -v \* | grep :`
     set NDESCS = $#DESCS
     @   NDESCS = $NDESCS / 2
     @        N = 1
     while ( $N <= $NDESCS )
     @        M = $N - 1
         set DESC = `cat $corcmprc | grep -v \# | grep -v \* | grep DSC$M | cut -d: -f2`
         set DSCS = `echo $DSCS $DESC`
     @        N = $N + 1
     end

echo " "
echo " EXPID: $expid"
echo "  YEAR: $year"
echo "SOURCE: $source"
echo "PLTDIR: $pltdir"
echo "WEBDIR: $webdir"
echo "  DSCS: $DSCS"
echo " "

# Copy Plots to Web Directory
# ---------------------------

# corcmp files
# ------------
     if( -e $pltdir/corcmp & ( $plots == NULL | $plots == corcmp ) ) then
         cd $pltdir/corcmp
         if( $season == 'NULL' ) then
           set files = `/bin/ls -1 *stats*gif`
           set fname = `echo $files[1] | cut -d. -f1`
           set fsize = `echo $fname | awk '{print length($0)}'`
             @ fsize = $fsize - 2
          set season = `echo $fname | cut -b${fsize}-`
         endif
         ssh $webid@${server} mkdir -p ${www}/${webdir}/${season}/corcmp
         echo Syncing ${season} Files from $pltdir/corcmp to $webid@${server}:${www}/${webdir}/${season}/corcmp
         rsync -avx *gif $webid@${server}:${www}/${webdir}/${season}/corcmp
     endif

# pltsys files
# ------------
     foreach DSC ($DSCS)
         if( -e $pltdir/$DSC & ( $plots == NULL | $plots == sys ) ) then
             cd $pltdir/$DSC
             if( $season == 'NULL' ) then
               set files = `/bin/ls -1 *stats*gif`
               set fname = `echo $files[1] | cut -d. -f1`
               set fsize = `echo $fname | awk '{print length($0)}'`
                 @ fsize = $fsize - 2
              set season = `echo $fname | cut -b${fsize}-`
             endif
             ssh $webid@${server} mkdir -p ${www}/${webdir}/${season}/$DSC
             echo Syncing ${season} Files from $pltdir/$DSC to $webid@${server}:${www}/${webdir}/${season}/$DSC
             rsync -avx *gif $webid@${server}:${www}/${webdir}/${season}/$DSC
             ssh $webid@${server} "cd ${www}/${webdir}/${season} ; ../../move_stats $DSC $season"
         endif
     end

# syscmp files
# ------------
     foreach  DSC ($DSCS)
         if( $DSC != $DSCS[1] ) then
         if( -e $pltdir/$DSCS[1].$DSC & ( $plots == NULL | $plots == syscmp ) ) then
             cd $pltdir/$DSCS[1].$DSC
             if( $season == 'NULL' ) then
               set files = `/bin/ls -1 *stats*gif`
               set fname = `echo $files[1] | cut -d. -f1`
               set fsize = `echo $fname | awk '{print length($0)}'`
                 @ fsize = $fsize - 2
              set season = `echo $fname | cut -b${fsize}-`
             endif
             ssh $webid@${server} mkdir -p ${www}/${webdir}/${season}/$DSCS[1].$DSC

             echo Syncing ${season} Files from $pltdir/$DSCS[1].$DSC to $webid@${server}:${www}/${webdir}/${season}/$DSCS[1].$DSC
             rsync -avx *gif $webid@${server}:${www}/${webdir}/${season}/$DSCS[1].$DSC
             ssh $webid@${server} "cd ${www}/${webdir}/${season} ; ../../move_stats $DSCS[1].$DSC $season"
         endif
         endif
     end

# Update year button
# ------------------
         if(    -e LOCKFILE ) /bin/echo "Waiting for LOCKFILE ... "
         if(    -e LOCKFILE ) set LOCK = `stat -c %Z LOCKFILE`
         while( -e LOCKFILE )
                   set    EPOCH = `date +'%s'`
                   @    LOCKAGE = $EPOCH - $LOCK
                   if( $LOCKAGE > 120 ) /bin/rm -f LOCKFILE
         end
                                       lockfile -3 LOCKFILE
         set  RC = $status
         if( $RC == 0 ) then
              set datetime = `date +%Y%m%d_%H%M%S`
             /bin/rm -f                                              awk_command.$datetime
              touch                                                  awk_command.$datetime
              echo "#! /bin/csh -f"                               >> awk_command.$datetime
              echo lockfile -3 LOCKFILE                           >> awk_command.$datetime
              echo "if( -e add_years.csh ) ./add_years.csh $year" >> awk_command.$datetime
              echo /bin/rm -f  LOCKFILE                           >> awk_command.$datetime
              chmod +x awk_command.$datetime
              scp awk_command.$datetime $webid@${server}:${www}/${expid}/buttons
              ssh $webid@${server} "cd ${www}/${expid}/buttons ; ./awk_command.$datetime ; /bin/rm -f awk_command.$datetime"
             /bin/rm -f awk_command.$datetime
         endif
        /bin/rm -f LOCKFILE

exit

# Update set_exps button
# ----------------------
if( $plotrc != NULL ) then
    set   CMPDIRS  = `grep PLOT_CMPEXP: $plotrc | cut -d'#' -f1 | cut -d':' -f2 `
    if( $#CMPDIRS != 0 ) then
        foreach cmpdir ($CMPDIRS)
            set cmpexp = `grep EXPID: $cmpdir/HISTORY.rc | cut -d'#' -f1 | cut -d: -f 2`
            set cmpexp = `echo $cmpexp`  # to remove preceding blanks

            if(    -e LOCKFILE ) /bin/echo "Waiting for LOCKFILE ... "
            if(    -e LOCKFILE ) set LOCK = `stat -c %Z LOCKFILE`
            while( -e LOCKFILE )
                      set    EPOCH = `date +'%s'`
                      @    LOCKAGE = $EPOCH - $LOCK
                      if( $LOCKAGE > 120 ) /bin/rm -f LOCKFILE
            end
                                          lockfile -3 LOCKFILE
            set  RC = $status
            if( $RC == 0 ) then
                 set datetime = `date +%Y%m%d_%H%M%S`
                /bin/rm -f                                              awk_command.$datetime
                 touch                                                  awk_command.$datetime
                 echo "#! /bin/csh -f"                               >> awk_command.$datetime
                 echo lockfile -3 LOCKFILE                           >> awk_command.$datetime
                 echo "if( -e add_exps.csh ) ./add_exps.csh $cmpexp" >> awk_command.$datetime
                 echo /bin/rm -f  LOCKFILE                           >> awk_command.$datetime
                 chmod +x awk_command.$datetime
                 scp awk_command.$datetime $webid@${server}:${www}/${expid}/buttons
                 ssh $webid@${server} "cd ${www}/${expid}/buttons ; ./awk_command.$datetime ; /bin/rm -f awk_command.$datetime"
                /bin/rm -f awk_command.$datetime
            endif
           /bin/rm -f LOCKFILE
        end
    endif
endif

exit

USAGE:
echo ' '
echo "Current     WEBID: $webid"
echo "Current WEBSERVER: $server"
echo ' '
echo ' ------------------------------------------------------------------'
echo ' '
echo '    Usage:  movestat -year    YEAR         '
echo ' '
echo '                    [-expid   EXPID   (EXPID on Web Page)]'
echo '                    [-rc      RCFILE  (RC File containing Experiments to compare)]'
echo '                    [-plots   PLOTS   (Type of PLOT to move)]'
echo '                    [-pltdir  PLTDIR  (Directory containing plot_directories to move)]'
echo '                    [-season  SEASON  (Season to move)]'
echo ' '
echo '                      Default  EXPID  (assumed to be 2nd-to-last node from pwd)'
echo '                      Default RCFILE  (assumed to be corcmp.rc)'
echo '                      Default PLOTS   (corcmp, sys, or syscmp.  Default: all)'
echo '                      Default PLTDIR  (assumed to be pwd)'
echo ' '
echo ' ------------------------------------------------------------------'
exit
