#!/bin/csh
# 
# go_acopy2 - Copy Yohkoh Archive or Data base tapes
#
# Calling Sequence:
#   (assumen alias acopy $DIR_GEN_SCRIPT/go_acopy2
#   acopy hawaii		# make Hawaii copy from master
#   acopy hawaii 1		# make Hawaii copy from copy 
#   acopy hawaii /ydb		# make Hawaii copy of ydb master tape
#   acopy sdac /soon		# make nssdc copy of a SOON tape
#
#     7-Jan-93 (MDM)  - Modified not to send messages at the end of the
#                       batch jobs (because IDL_BATCH_NOTIFY is set)
#    21-Apr-93 (JRL)  - Added ISAS to the list.  Also, added in_gen option.
#    20-May-93 (MDM)  - Modified to do a grep of the IDL batch log to look
#                       for errors
#    25-May-93 (MDM)  - Removed 20-May-93 modification since the log file
#                       is not made yet (this script just submits a batch
#                       job)
#    25-Jan-93 (SLF)  - Added /ydb switch, allow strings for institutes
#			allow string input for institutes 
#     9-Jun-94 (SLF)  - fix type (breakse - > breaksw)
#     8-Aug-94 (SLF)  - add /soon switch 
#
set soon=""
set ydb=""
set arglist=()
foreach argx ($argv)
   switch ($argx)
      case soon:
      case /soon:
         set soon="1"
         breaksw
      case /ydb:
      case ydb:
         set ydb=",/ydb"
      breaksw
       case help:
       case HELP:
       case /help:
          echo Calling Sequence:
          echo    "   acopy INSTITUTE  "' [generation] [/ydb] [/soon]'
          echo " "
          echo "Calling Examples:  (current institute list below)"
          echo    '   acopy hawaii      #hawaii archive tape'
          echo    '   acopy su /ydb     #stanford ydb (data base) tape'
          echo    '   acopy sdac /soon  #SDAC SOON tape (wiil prompt for name)'
          echo " "
        echo "Institute  (synonyms)"
        echo " "
        echo "hawaii     uh     0" 
        echo "stanford   su     1"
        echo "berkeley   ucb    2"
        echo "harvey     kh     3"
        echo "lockheed   lparl  4"
        echo "nssdc             5"
        echo "naoj              6"
	echo "bcs               7"
	echo "acton      lwa    8"
        echo "other             9"
	echo "nolog      /nolog 10"
        echo "isas              11"
	echo "sdac              12"
        exit
        breaksw        
   default:
      set arglist=($arglist $argx)
      breaksw
   endsw
end


if ($#arglist == 0) then
   if (-e $DIR_GEN_SCRIPT/go_acopy2)  $DIR_GEN_SCRIPT/go_acopy2 /help
   exit
endif

set inst=$arglist[1]            # assume institute number passed

set defins=(0 1 2 3 4 5 6 7 8 9 10 11 12)
set chkins=`echo $defins | grep $inst`
if ("$chkins" == "") then 
switch ($arglist[1])
   case hawaii:
   case uh:
   case UH:
      set inst=0
      breaksw
   case stanford:
   case su:
   case SU:
      set inst=1
      breaksw
   case berkeley:
   case ucb:
   case UCB2:
      set inst=2
      breaksw
   case harvey:
   case kh:
   case KH:
      set inst=3
      breaksw
    case lockheed:
    case LPARL:
    case lparl:
       set inst=4
    breaksw
    case NSSDC:
    case nssdc:
       set inst=5
       breaksw
    case NAOJ:
    case naoj:
       set inst=6
       breaksw
    case bcs:
    case BCS:
       set inst=7
       breaksw
    case acton:
    case lwa:
       set inst=8
        breaksw
    case other:
       set inst=9
        breaksw
    case nolog:
    case /nolog:
       set inst=10
       breaksw
    case isas:
    case ISAS:
       set inst=11
       breaksw
    case sdac:
    case SDAC:
       set inst=12
       breaksw
    default:
       echo Unknown Institute: $arglist[1]
       go_acopy2 /help
       exit 
      breaksw
endsw

endif

if ($soon == 1) then
   echo -n "Enter SOON Tape (ex: STH940517B01) : "
   set tname = $<
   set chkst=`echo $tname | grep -i st`
   if ($chkst == "") set tname="ST"$tname
   set chkb0=`echo $tname | grep -i b01`
   if ($chkb0 == "") set tname="$tname"B01
   set ccount=`echo $tname | wc -c`
echo $ccount
   while ($ccount < 12)
      echo "SOON Tape Name should be 12 characters, try again"
      echo -n "Enter SOON Tape (ex: STH940517B01) : "
      set tname = $<
      set ccount=`echo $tname | wc -c`
echo $ccount
echo $tname
   end
   set soon=',soon="'$tname'"'
endif   

set gen=""
if ($#arglist == 2) then
   set gen=",in_gen=$arglist[2]"
endif

echo "acopy, $inst, /non_interactive $gen$ydb$soon" > ~/go_acopy.pro
echo "end" >> ~/go_acopy.pro

rm -f $DIR_SITE_LOGS/acopy_`hostname`
if ($?IDL_BATCH_NOTIFY == 1) then
    setenv SAVE_IDL_BATCH_NOTIFY `echo $IDL_BATCH_NOTIFY`
    unsetenv IDL_BATCH_NOTIFY
endif

nohup /ys/gen/script/idl_batch ~/go_acopy.pro $DIR_SITE_LOGS/acopy_`hostname` &

if ($?SAVE_IDL_BATCH_NOTIFY == 1) then
    setenv IDL_BATCH_NOTIFY `echo $SAVE_IDL_BATCH_NOTIFY`
    unsetenv SAVE_IDL_BATCH_NOTIFY
endif

exit
