#!/bin/csh 
#
#  modified by TY 1998-10-19 multi-schedule support
#  modified by TY 1998-11-12 anonymous FTP
#  modified by TY 1998-11-17 csh version
#  modified by TY 1999-01-14 for event data
#  modified by TY 1999-04-15 for dir_to
#  modified by MS 2013-01-23 for ftphost

set argv=`getopt rl:h:d:t: $*`

set ftppass = "norh_trans@`hostname`"
set ftphost = 'solar-pub.nao.ac.jp'
#set ftphost = 'solar.nro.nao.ac.jp'
#set ftphost = '133.40.84.74'
#set ftphost = 'nsro-archive.nro.nao.ac.jp'
set dir_from0 = '/pub/nsro/norh/raw'
set dir_to = '.'
set rename = 0

while ( $#argv )
  switch ($argv[1])
    case -r:
      set rename = 1
    breaksw
    case -l:
      shift
      set ftppass = $argv[1]
    breaksw
    case -h:
      shift 
      set ftphost = $argv[1]
    breaksw  
    case -d:
      shift
      set dir_from0 = $argv[1]
    breaksw
    case -t:
      shift
      set dir_to = $argv[1]
    breaksw
    case --:
      shift
      break
    breaksw
  endsw
  shift
end

if ( $#argv == 0 ) cat <<END
Usage: norh_trans [-l username] [-h host] [input1 input2 ...]
  -l[username]  your email address (recorded on the anonymous FTP host)
  -h[host]      anonymous FTP host
  -d[dir]       top directory of the NoRH archive
END

set ftpuser = 'ftp';
set com='get';
set ftpcom=/tmp/ftp$$;
if ( -f $ftpcom) \rm $ftpcom
##################################################################

cat <<END > $ftpcom
open $ftphost
user $ftpuser $ftppass
binary
END

foreach file ( $argv )

set awkcom=/tmp/awk$$;
#set awkcom=awkcom
cat >! $awkcom <<AWK
  {file=\$1}
  {mode=substr(file,2,1)}
 {if (mode == "s" ) {
   if (NF == 2) {sccar=\$2} else {sccar="-"}
 } 
 }
  {freq=substr(file,3,2)}
  {yearst=substr(file,5,2)}
  {month=substr(file,7,2)}
  {day=substr(file,9,2)}
  {seq=substr(file,11,3)}
  {if (yearst >= 90) {year="19"yearst} else {year="20"yearst}}

 {if ( mode == "s") {
  {if (sccar == "-") {sccar=""}}

  {dir_from="$dir_from0/steady/"year"/"month"/"day sccar}
  {filename="sz00" yearst month day "001"}
  {filename_to="sz00" yearst month day "001"}
  {if ("$rename" == "1") {filename_to="sz00" yearst month day sccar "001"}}
  {comline="$com " dir_from "/" filename " $dir_to/" filename_to}
  {print comline}
  {comline="$com " dir_from "/" filename ".Z" " $dir_to/" filename_to ".Z"}
  {print comline}
  {comline="! uncompress -f $dir_to/" filename_to ".Z"}
  {print comline}

# {filename="info"}
# {filename_to= "info" yearst month day}
# {if ("$rename" == "1") {filename_to=filename_to sccar}}
# {comline="$com " dir_from "/" filename " $dir_to/" filename_to}
# {print comline}
# {comline="$com " dir_from "/" filename ".Z" " $dir_to/" filename_to ".Z"}
# {print comline}
# {comline="! uncompress -f $dir_to/" filename_to ".Z"}
# {print comline}

  {filename=file}
  {filename_to= filename}
  {if ("$rename" == "1") {filename_to="ss" freq yearst month day sccar seq}}
  {comline="$com " dir_from "/" filename " $dir_to/" filename_to}
  {print comline}
  {comline="$com " dir_from "/" filename ".Z" " $dir_to/" filename_to ".Z"}
  {print comline}
  {comline="! uncompress -f $dir_to/" filename_to ".Z"}
  {print comline}

 } else {
  {sccar=substr(file,11,6)}

  {dir_from="$dir_from0/steady/"year"/"month"/"day }
  {filename="sz00" yearst month day "001"}
  {filename_to=filename}
  {if ("$rename" == "1") {filename_to="sz00" yearst month day sccar "001"}}
  {comline="$com " dir_from "/" filename " $dir_to/" filename_to}
  {print comline}
  {comline="$com " dir_from "/" filename ".Z" " $dir_to/" filename_to ".Z"}
  {print comline}
  {comline="! uncompress -f $dir_to/" filename_to ".Z"}
  {print comline}

  {dir_from="$dir_from0/event/" year"/"month"/"day }
# {filename="info"}
# {filename_to= "info" yearst month day}
# {comline="$com " dir_from "/" filename ".Z" "$dir_to/" filename_to ".Z"}
# {print comline}
# {comline="$com " dir_from "/" filename "$dir_to/" filename_to}
# {print comline}
# {comline="! uncompress -f $dir_to/" filename_to ".Z"}
# {print comline}

  {filename=file}
  {filename_to= filename}
  {comline="$com " dir_from "/" filename " $dir_to/" filename_to}
  {print comline}
  {comline="$com " dir_from "/" filename ".Z" " $dir_to/" filename_to ".Z"}
  {print comline}
  {comline="! uncompress -f $dir_to/" filename_to ".Z"}
  {print comline}

 }}

AWK

cat $file | awk -f $awkcom  | grep '^[^\!]' | sort | uniq >> $ftpcom
cat $file | awk -f $awkcom  | grep '^\!' | sort | uniq >> $ftpcom
\rm $awkcom

end  # foreach

echo "close" >> $ftpcom
echo "" >> $ftpcom
#cat $ftpcom
cat $ftpcom | ftp -n
\rm $ftpcom

