#!/bin/csh -f
# download files from the FTP server
set basedir=`dirname $0`
cd $basedir
# determine which Ex Libris application
source util_sp_set_app

if (`uname` == Linux) then
	setenv aleph_mail "mail"
else
	setenv aleph_mail "mailx"
endif


if (! -f $sp_conf) then
        echo "Initial configuration not saved"
        echo "Operation aborted"
        exit 1
    endif
endif
source $util_proc/util_sp_get_conf
if (! -d $sp_dir) then
    mkdir $sp_dir
endif
cd $sp_dir
if (-e ftp.pid) then
    ps -p `cat ftp.pid` >& /dev/null
    if ($status) then
        echo "Removing unused pid file"
    else
        echo "FTP still running - please check ftp.log"
        exit 1
    endif
endif

# set ftp_pass
# if (-e $sp_passfile) then
# 	set ftp_pass=`cat $app_conf/.sp_pass`
# endif
# if ("$ftp_pass" == "") then
# 	echo "Empty FTP password - exiting"
# 	exit 1
# endif
ls -1 | grep -v sp.log | xargs rm -f  >& /dev/null



set platform="`uname`"
set cobtype=new
if ($?COBDIR) then
	if (-e $COBDIR/cobver) then
	    set cobtype=old
	endif
endif
if (-e ftp.list) then
    rm ftp.list
endif
echo $$ > $sp_dir/ftp.pid

cat > sftp_cli_file  << EOF
ls
EOF

# Defining ssh key to be used 
switch ( "$ftp_user" ) 
   case "*4_sp*":
      set ssh_private_key = "~crn/.ssh/arc_4_sp_mft"
      breaksw

   case "*45_sp*": 
      set ssh_private_key = "~crn/.ssh/arc_45_sp_mft" 
      breaksw

   case "*3_sp*": 
      set ssh_private_key = "~crn/.ssh/arc_3_sp_mft" 
      breaksw

   default:
      echo "SSH wasnt found for this FTP user"
      breaksw
endsw

#sftp -o IdentityFile=$ssh_private_key -o Port=10022  -o StrictHostKeyChecking=no $ftp_user@$ftp_server << EOF >&! ftp1.log
#ls * ftp.list
#exit
#EOF
sftp -b sftp_cli_file -o IdentityFile=$ssh_private_key -o Port=10022 -o StrictHostKeyChecking=no $ftp_user@$ftp_server > ftp.list

#sftp -b sftp_cli_file -o IdentityFile=$ssh_private_key -o Port=10022 -o StrictHostKeyChecking=no $ftp_user@$ftp_server > ftp.list

# We want NOT to confirm the remote host identity key, 
#   and NOT to add it to the ~aleph/.ssh/known_hosts file (add it to empty file instead). 
#sftp -b sftp_cli_file -o IdentityFile=$ssh_private_key -o Port=10022 $ftp_user@$ftp_server > ftp.list

#ftp -vnid $ftp_server << EOF >&! ftp1.log
#user $ftp_user $ftp_pass
#$ftp_opts
#mls *  ftp.list
#bye
#EOF

if (! -e ftp.list) then
    rm ftp.pid
    set sp_check="FTP connection or login failed to $ftp_server"
    set ftp_status=failed
    echo $sp_check
    goto fail
endif

echo "FTP download started"
cat > ftp.in  << EOF
get cksum_files_list
EOF
# create SP download list
set OS=`uname`-`uname -p`
if ($platform == SunOS) then
    set platform="$platform-$cobtype"
else
    set cobtype=""
endif

grep $app ftp.list |  grep gz_ | grep -v Bin | tee sp.split.list | sed 's/^/get /' >>  ftp.in
foreach platform ($OS-$cobtype `uname`-$cobtype $OS `uname`)
	grep $app ftp.list |  grep gz_ | grep  Bin | grep "$platform" | tee sp.bin.list  | sed 's/^/get /' >>  ftp.in
	if (! -z sp.bin.list ) then
		break
	endif
end
if (-z sp.bin.list ) then
	echo "Could not find suitable binary SP"
	echo "Download aborted"
endif

sftp -b ftp.in -o IdentityFile=$ssh_private_key -o Port=10022 $ftp_user@$ftp_server

#ftp -vnid $ftp_server < ftp.in >&! ftp.log

rm $sp_dir/ftp.pid
set sp_ver
set sp_check
csh -f $util_proc/util_sp_check > sp_check.stat
if ($status) then
    set ftp_status="failed"
    set sp_check=`cat sp_check.stat`
else
    set ftp_status="success"
    set sp_ver=`./$sp_util -h`
endif
fail:
set cust_ip=`perl -e "use Socket;use Sys::Hostname; print inet_ntoa(scalar gethostbyname(hostname() || 'localhost'))"`



if ("$cust_mail" != "") then
    set subject="SP Download status - $ftp_status" 
    if ("$ftp_status" == "failed") then
        $aleph_mail -s "$subject" $cust_mail << EOF
Dear Customer,
The $app Service Pack download failed
`hostname` $app $app_version copy $app_copy
Please see ftp logs in $sp_dir
Error messgae is:
$sp_check

EOF
    else
        $aleph_mail -s "$subject" $cust_mail << EOF
Dear Customer,
The $app Service Pack download has been successfully completed.
$sp_ver
`hostname` $app $app_version copy $app_copy

you may continue with the SP installation steps

EOF
	    if ( ${mail_exl_flag} != "N" ) then
		$aleph_mail -s "$app $sp_ver has been downloaded" $exl_mail << EOF
    To $app support team,

    $app Service  Pack $sp_ver  has been downloaded on `date` from the Ex Libris FTP server ($ftp_server) by customer:
    $cust_code
    $cust_ip
    `hostname`
    `uname -a`

    Please take note of this information.



EOF
	    endif
    endif
endif

