#!/bin/csh
#
# S.L. Freeland  12-Jan-1994
#		 15-Jan-1994 (SLF) add /ysmod action
#                15-Feb-1994 (SLF) add /get action
#		 23-Feb-1994 (SLF) rm->/bin/rm, 1993->1994
#		  6-Apr-1994 (SLF) extend to ydb files
#		 11-Apr-1994 (SLF) add keywords, look for .pro exts.
#				   
# ys_contrib
# auto-ftp contributions to (and from) Yohkoh SW system via anon ftp
# 
# Calling Sequence:
#   ys_contrib <files> [/ysmod] [/get]
#   
# Input Parameters:
#   files - list of one or more files to transfer (may be .tar or .tar.Z)     
#
# Switches:
#   /ysmod - notifies Yohkoh SW managers of update to  existing SW (priority)
#   /get   - retrieves most recent version of specified routines   
#   /gen,/bcs,/sxt,/ucon (exclusive) - request specific $ys output directory
#   /online - bypass normal checks, place online 
#
################### Initiailize #######################
set putfiles=()			# null list for starters
set mail=1			# default, send mail
set ysmod=0			# not Yohkoh SW update
set README=0			# no readme file
set get=0			# default is put
set ydb=0			# default is sofware/text
set ysdir="None Specified"	# no default output path target
set incftp=0			# default - dont include ftp log
set online="NO"			# default - not automatic online
#######################################################

########### Parse Input Parameters ####################
foreach argx ($argv)
   switch ($argx)
	case /nomail:
	   set mail=0 
 	   breaksw
	case /ysmod:
	   set ysmod=1
	   breaksw
	case README:
	   set README=1
	   breaksw
        case /get:
           set get=1
           breaksw
	case /ydb:
	   set ydb=1
	   set get=1
	   set mail=0
           breaksw
	case /gen:
           set online="YES"
           set ysdir='/gen'
           breaksw
        case /bcs:
           set online="YES"
	   set ysdir='/bcs'
           breaksw
        case /sxt:
           set online="YES"
	   set ysdir='/sxt'
           breaksw
        case /ucon:
           set online="YES"
	   set ysdir='/ucon'
           breaksw
        case /wbs:
           set online="YES"
	   set ysdir='/wbs'
           breaksw
        case /hxt:
           set online="YES"
	   set ysdir='/hxt'
           breaksw
        case /ftplog
	   set incftp=1
           breaksw
        case /online:
           set online="YES"
           breaksw
        default:
	    set putfiles=($putfiles $argx)
        breaksw
     endsw
end	   		
#######################################################

################### Help if no files ##################
if ($#putfiles == 0) then
   echo "Calling Sequence:"
   echo "   ys_contrib file [,file2, file3...] [/ysmod] [/nomail] [/get]"
   exit
endif
#######################################################

######### Determine login and transfer info ###########
set user=`whoami`			# user name
set host=`hostname`			# hostname
set memail="$user"'@'"$host"		
set ftpput="~/ys_contrib.$user"		# ftp transfer file
# use full host description if possible (hostname may just give alias)
if (-e /etc/hosts) then
   set thost=`grep "$host\." /etc/hosts`
   if ($#thost > 2) then
      set host=$thost[2] 
   endif
endif
# Default to Yohkoh SW master
if (!($?yssw_master)) set yssw_master="isass0.solar.isas.ac.jp"
#######################################################
# clean for people with noclobber and rm aliases...
/bin/rm -f $ftpput
/bin/rm -f $ftpput.log

echo " " > $ftpput.log

echo Generating ftp transfer script
################## build ftp script ##################
echo open $yssw_master 			>  $ftpput
echo user ftp $user"@"$host		>> $ftpput
echo binary		 		>> $ftpput
echo cd pub/swmaint 			>> $ftpput
   ### request (get) logic ###
set fid=`$DIR_GEN_SCRIPT/date2fid`
if ($get) then
   set getlist=~/getlist.$user.$fid
   set type="Files Requested:"
   if ($ydb) then 
      ################# ydb support #####################
      set subj="Yohkoh Data Base Request"
      set getenv=`printenv "$putfiles[1]"`
      set outlogs=`printenv DIR_SITE_LOGS`
      if (!($?OS)) set OS="?"
      echo "OS="$OS 	 > $outlogs/"$putfiles[1]".$user
      ls -altr $getenv 	>> $outlogs/"$putfiles[1]".$user
      echo cd ydbget/remlists		>> $ftpput
      echo lcd $outlogs			>> $ftpput
      echo put 	"$putfiles[1]".$user	>> $ftpput
      set putfiles=$outlogs/"$putfiles[1]".$user
      #####################################################
   else
      ################# /get function #####################
      set subj="Yohkoh SW Request"
      echo "Files to be e-mailed TO: "$user"@"$host": ..."
      echo $user"@"$host  > $getlist 
      echo $putfiles     >> $getlist
      echo cd getlists			>> $ftpput
      echo lcd 				>> $ftpput
      echo put getlist.$user.$fid	>> $ftpput
      set putfiles="$getlist"
      echo "   "$putfiles
   endif
      #############################################################
else
   ############# software contribution (put) logic ################
   set type="Files Transfered:"
   set subj="Yohkoh SW Contribution"
   set found=()
   set notfound=()
   echo cd contrib				>> $ftpput
   echo mkdir $user		 		>> $ftpput
   echo cd $user				>> $ftpput
   set onefound=0
   foreach putfile ($putfiles)
      set pfile=$putfile
#     append  ".pro" if cannot find specified file
      if (!(-e $pfile)) then
         set prochk=`echo $pfile | grep .pro`
         if("$prochk" == "") then
            set pfile="$pfile"".pro"
         endif
      endif
      if (!(-e $pfile)) then
         echo Cannot find file: $putfile or $putfile".pro"
         set notfound=($notfound $putfile)
      else
         set onefound=1
         set found=($found $pfile)
         echo put $pfile $pfile:t		>> $ftpput
      endif
   end
   if (!($onefound)) then
      echo "None of your input files were found, aborting"
      exit		############# Unstructured Exit
   endif
   echo put $ftpput.log	"$ftpput:t"."$fid"	>> $ftpput	# include log
endif
echo bye		 			>> $ftpput
##################################################################

############### Build output log file #################
echo "---- "$subj" ""$ys/gen/script/ys_contrib ----" >> $ftpput.log
echo " " 				>> $ftpput.log
echo "Job Name: ""$ftpput:t"."$fid"     >> $ftpput.log
echo " " 				>> $ftpput.log
echo "Requested YS Directory: "$ysdir	>> $ftpput.log
echo " " 				>> $ftpput.log
if ($get) then 
   echo "$type"" " $putfiles 		>> $ftpput.log
   echo " " 				>> $ftpput.log
else
   echo "Online Request: "$online 	>> $ftpput.log
   echo " " 				>> $ftpput.log
   echo "$type"" " $found 		>> $ftpput.log
   echo " " 				>> $ftpput.log
   if ($#notfound > 0) then
     echo "Files not Found: "$notfound  >> $ftpput.log
     echo " " 				>> $ftpput.log
   endif
endif   
echo "-------------------------------------------" >> $ftpput.log
#######################################################

if ($README == "1") then
   echo "---------- README File ------------" >> $ftpput.log
   if (-e README) then
      cat README >> $ftpput.log   
   else
      echo could not find file README   >> $ftpput.log
      echo could not find file README   
   endif
   echo "-----------------------------------" >> $ftpput.log
endif
echo " " >> $ftpput.log

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

############### ftp transfer ########################## 
# start ftp
if (!($get)) echo Initiating ftp transfer to $yssw_master 
if ($incftp) then 
   echo "---------- ftp output log ------------" >> $ftpput.log
   ftp -ivn < $ftpput >> $ftpput.log
else 
   ftp -ivn < $ftpput > /dev/null 
endif
#######################################################

###### Send Mail to Software and Contributor ##########
if ($mail) then 
   if ($ysmod) set subj="$subj"" \!\!\! YS MOD \!\!\!"
   echo sending mail to software account and $memail
   set wmail=`which Mail`
   if ($#wmail > 1) set wmail=`which mail`
#   $wmail -s "$subj" software@isass0.solar.isas.ac.jp < $ftpput.log
   $wmail -s "$subj" software@isass0.solar.isas.ac.jp $memail  < $ftpput.log
endif
#######################################################

######## cleanup ############
if ($?getlist) then
   if (-e $getlist) /bin/rm -f $getlist
endif
#######################################################
exit
