#!/bin/csh

# Set Lagranto 
set LAGRANTO = ${LAGRANTOBASE}.${MODEL}/

# Write usage information
if ( ${#argv} == 0) then
  echo 
  echo "--------------------------------------------------------------"
  echo "Usage: datelist.sh filename mode [parameters]"
  echo 
  echo "         filename : Name of the date file"
  echo "         mode     : Mode"
  echo 
  echo "             -create startdate enddate : create a date list"
  echo "             -interval hour            : interval in hours"
  echo "             -first                    : first date"
  echo "             -last                     : last date"
  echo "             -ndates                   : number if dates"
  echo "             -timerange                : time range in hours"
  echo "             -indir                    : all dates in a directory"
  echo "             -next date                : get the next date after {date}"
  echo "             -prev date                : get the previous date before {date}"
  echo "             -isin date                : check whether date is in list"
  echo "             -overlap file1 file2      : overlap of two date lists"
  echo "             -onlyin1 file1 file2      : dates which are only in file1"
  echo "             -onlyin2 file1 file2      : dates which are only in file2"
  echo 
  echo "--------------------------------------------------------------"
  exit 0
endif

# Handle fixed arguments
set filename = $1
set mode     = $2

# Redirect output to screen if requested
if ( "${filename}" == "stdout" ) set filename="/dev/stdout"
if ( "${filename}" == "screen" ) set filename="/dev/stdout"

# Handle optional arguments
set interval = "6"

while ( $#argv > 0 )

  switch ( $argv[1] )

   case -create
     set startdate = $argv[2] 
     set finaldate = $argv[3] 
     shift;
     shift;
   breaksw

   case -indir
     set dirname = $argv[2] 
     shift;
   breaksw

   case -next
     set date = $argv[2] 
     shift;
   breaksw

   case -prev
     set date = $argv[2] 
     shift;
   breaksw

   case -isin
     set date = $argv[2] 
     shift;
   breaksw

   case -interval
     set interval = $argv[2] 
     shift;
   breaksw

   case -overlap
     set file1 = $argv[2] 
     set file2 = $argv[3] 
     shift;
     shift;
   breaksw

   case -onlyin1
     set file1 = $argv[2] 
     set file2 = $argv[3] 
     shift;
     shift;
   breaksw

   case -onlyin2
     set file1 = $argv[2] 
     set file2 = $argv[3] 
     shift;
     shift;
   breaksw


  endsw

  shift;

end

# Mode: -create startdate finaldate
if ( "${mode}" == "-create" ) then
    
   \rm -f datelist.param
   echo \"${filename}\"    >  datelist.param
   echo \"${mode}\"        >> datelist.param
   echo \"${startdate}00\" >> datelist.param
   echo \"${finaldate}00\" >> datelist.param
   echo ${interval}        >> datelist.param
   
   ${LAGRANTO}/goodies/datelist

endif

# Mode: -first 
if ( "${mode}" == "-first" ) then
    
   head -1 ${filename} 

endif

# Mode: -last 
if ( "${mode}" == "-last" ) then
    
   tail -1 ${filename} 

endif

# Mode: -ndates 
if ( "${mode}" == "-ndates" ) then
    
   wc -l ${filename} | awk '{ print $1}'

endif

# Mode: -timerange 
if ( "${mode}" == "-timerange" ) then
  
   set firstdate = `head -1 ${filename}`
   set finaldate = `tail -1 ${filename}`  

   ${LAGRANTO}/goodies/gettidiff ${finaldate} ${firstdate}

endif

# Mode: -indir {directory name}
if ( "${mode}" == "-indir" ) then

    ls -1 ${dirname} | perl -ne 'print  if s/.*([0-9]{8}_[0-9]{2}).*/\1/' | sort | uniq >! ${filename}

endif

# Mode: -next
if ( "${mode}" == "-next" ) then

  set last = `tail -1 ${filename}` 
  if ( "${date}" != "${last}" ) then
     set next = `sed -n "/${date}/{n;p;}" ${filename}`
     echo ${next}
  else
     echo "nil"
  endif

endif

# Mode: -prev
if ( "${mode}" == "-prev" ) then

  set first = `head -1 ${filename}` 
  if ( "${date}" != "${first}" ) then
     set prev = `sed -n "/${date}/{g;p;};h" ${filename}`
     echo ${prev}
  else
     echo "nil"
  endif

endif 

# Mode: -isin
if ( "${mode}" == "-isin" ) then

  set flag = `sed -n "/${date}/p" ${filename}`
    
  if ( "${flag}" != "" ) then
    echo "1"
  else
    echo "0"
  endif

endif 

# Mode: -overlap
if ( "${mode}" == "-overlap" ) then
    if ( "${filename}" != "/dev/stdout" ) then 
       set outfile = ${filename}.$$
       \rm -f ${outfile}
       grep -f ${file1} ${file2} > ${outfile}
       \mv ${outfile} ${filename}
    else
       grep -f ${file1} ${file2}
    endif
endif

# Mode: -onlyin1
if ( "${mode}" == "-onlyin1" ) then
    set outfile1 = "tmp1.$$"
    set outfile2 = "tmp2.$$"
    \rm -f ${outfile1}
    \rm -f ${outfile2}
    grep -f  ${file1}    ${file2} >! ${outfile1}
    grep -vf ${outfile1} ${file1} >! ${outfile2}
    if ( "${filename}" != "/dev/stdout" ) then
      \mv ${outfile2} ${filename}
    else
      cat  ${outfile2}
    endif
    \rm -f ${outfile1}
    \rm -f ${outfile2}
endif

# Mode: -onlyin2
if ( "${mode}" == "-onlyin2" ) then
    set outfile1 = "tmp1.$$"
    set outfile2 = "tmp2.$$"
    \rm -f ${outfile1}
    \rm -f ${outfile2}
    grep -f  ${file2}    ${file1} >! ${outfile1}
    grep -vf ${outfile1} ${file2} >! ${outfile2}
    if ( "${filename}" != "/dev/stdout" ) then
      \mv ${outfile2} ${filename}
    else
      cat  ${outfile2}
    endif
    \rm -f ${outfile1}
    \rm -f ${outfile2}
endif


exit 0

