#! /bin/csh
#
# This script will make a list of all files in the current directory.  It
# will then copy the files to a DADS data ingest area and then copy the 
# file containing the list of files to the dads request area.  
# It will use rsh to invoke a VMS DCL procedure to create the DADS archive
# request.
#

#
# Check if the user specified a path
#

set source_path = "."

set remote_node = 'archb.stdads.stsci.edu'
set remote_path = 'cdbs_ingest_data:'
set remote_user = 'cdbs@'
set exec_path   = 'dads_ops$tools:'

set display_usage = 0
set mandatory_param = 0

if ($#argv > 0 ) then
    while ($#argv)
        switch ($argv[1])
            case -s:
                shift
                if ( -d $argv[1] ) then
                    set source_path = $argv[1] 
                else
                    echo "$argv[1] is not a directory"
		    exit
		endif
                breaksw
            case -d:
                shift
                set remote_path = "$argv[1]" 
                breaksw
            case -a:
                shift
                set archive_class = "$argv[1]" 
		if ( "$archive_class" != "CTB" && \
                     "$archive_class" != "ctb" && \
                     "$archive_class" != "CDB" && \
                     "$archive_class" != "cdb" ) then 
                    echo "Invalid archive class.  Must be CTB, CDB"
                    set display_usage = 1
                endif
                set mandatory_param = 1
                breaksw
            case -n:
                shift
                set remote_node = $argv[1] 
                breaksw
            case -e:
                shift
                set exec_path = "$argv[1]" 
                breaksw
            case -u:
                shift
                set remote_user = "$argv[1]" 
                breaksw
            default:
		if ( "$argv[1]" != "-h" ) then
		    echo "invalid option $argv[1]"
		endif
                set display_usage = 1
        endsw
        shift
    end
else
	set display_usage = 1
endif


if ( ($display_usage) || (! $mandatory_param) ) then

	echo "Usage: $0" 
	echo "   -a  archive_class     Archive class of files to archive"
	echo "  [-s  source_dir]       Path of data files to archive"
	echo "  [-d 'remote_dir']      VMS path where data is copied"
	echo "  [-n  remote_node]      VMS host"
	echo "  [-u  remote_user]      VMS user account"
	echo "  [-e 'executable_path'] VMS path of DCL command file"
	exit 

endif


#
# Create the file list of the current directory. Copy the file over to the
# VMS side as the list is created.
#
set num_copied = 0
set file_list = $$_file_list.dat
cd $source_path
foreach i (*)
        #
	# check if the file is a plain file (not a directory)
	#
	if ( -f $i ) then

	    # Check if it is a ".fits" file

            set ext = `echo $i | awk -F\. '{print $2}'`
	    if ("$ext" == "fits") then

                # Strip off the .fits - end the new name with the extension
		# perl: (.*)      = everything up to the "_" put result in $1
                #       _(.{3})\. = 3 chars between _xxx.    put result in $2

                set filename = `echo $i | perl -ne '/(.*)_(.{3})\./; print "$1.$2";'` 

   	        # write the file to the file list file and copy it to VMS side

	    	echo "$remote_path$filename" >> $file_list
	    	echo "Copying $i to $remote_node':$remote_path'"
	    	set cmd = "rcp $i ""'$remote_user'""$remote_node':$remote_path'$filename"
            	echo "$cmd" | csh 
                set num_copied = 1
	    else
                echo "$i NOT COPIED - Does not end in .fits"
            endif

	else
            echo "$i NOT COPIED - Not a regular file"
        endif 
end

if ($num_copied == 0) then
    echo "No files copied - request submission cancelled"
    exit 2
endif

#
# copy the file list file to VMS and then delete local copy
#

echo ""
echo "Copying $file_list to $remote_node':$remote_path'"

set cmd = "rcp $file_list ""'$remote_user'""$remote_node':$remote_path'"
echo "$cmd" | csh 

/bin/rm -f $file_list


#
# execute the VMS dcl procedure to build the request
#

echo ""
echo "Executing DCL command to generate and submit request"
echo ""

# strip of the @ sign if there is one on the remote user_name

set remote_user = `echo $remote_user | awk -F\@ '{print $1}'`

set cmd = "rsh -l $remote_user $remote_node '@""$exec_path""cdbs_request_make.com $archive_class $remote_path""$file_list'"
echo "$cmd" | csh 


echo ""
echo "Completed request submission"
echo ""
