#!/bin/csh -f
# Download and run third party installation scripts
clear
set basedir=`dirname $0`
cd $basedir
source util_sp_set_app

set conf_file=~/.util_sp_conf

if ( -f $conf_file ) then
    source $conf_file
else
    touch $conf_file
endif
if (! $?IKIT_FTP_DIR) then
    setenv IKIT_FTP_DIR /exlibris/ftp_from_exlibris
endif
if (! $?FTPUSER) then
    setenv FTPUSER "tp_all_down@customers.gl"
endif
set ftp_user = ${FTPUSER}

if (! $?FTP_SERVER) then
   # setenv PFTPSERVER "downloads-eu.hosted.exlibrisgroup.com"
   setenv PFTPSERVER "downloads-na.hosted.exlibrisgroup.com"
else
    setenv PFTPSERVER "$FTP_SERVER"
endif
set ftp_server = ${PFTPSERVER}

setenv FTP_SERVER "$ftp_server"

if (! $?FTPSCRIPTSDIR) then
    setenv FTPSCRIPTSDIR global.new/scripts
endif
set scriptsdir = ${FTPSCRIPTSDIR}

echo "The third party download utility will use sftp to download the required packages"
echo "Download directory will be $IKIT_FTP_DIR"
echo "If you want to change it please enter the new path now or <ENTER> to keep"
set ans="$<"
if ("$ans" != "") then
    setenv IKIT_FTP_DIR $ans
    (grep -v IKIT_FTP_DIR $conf_file ; echo "setenv IKIT_FTP_DIR $IKIT_FTP_DIR") > $conf_file.new
    mv $conf_file $conf_file.bak
    mv $conf_file.new $conf_file
endif
if (! -d  $IKIT_FTP_DIR) then
    echo "$IKIT_FTP_DIR   directory does not exist"
    echo "Operation aborted"
    goto exit
endif
if (! -w $IKIT_FTP_DIR) then
    echo "$IKIT_FTP_DIR not writable"
    echo "Operation aborted"
    goto exit
endif
echo ""
echo "*********************************************"
echo "Make sure you have a working sftp in your path"
echo "*********************************************"
echo ""
echo "Downloading latest version of third party scripts to download dir"

cd $IKIT_FTP_DIR

cat > sftp_cli_file_3rd_party  << EOF
cd $scriptsdir
get -P *
EOF

# Defining ssh key to be used
set priv_key = "~crn/.ssh/arc_3rd_party_products_mft"

if (! -f $priv_key) then
    echo "SSH key wasnt found (needed for sftp). "
    echo "Operation aborted"
    goto exit
endif

# The command:
sftp  -b sftp_cli_file_3rd_party -o IdentityFile=$priv_key  -o Port=10022 -o StrictHostKeyChecking=no $ftp_user@$ftp_server > ftp.list 

if ( ! -f product_ftp_download.sh) then
    echo "Could not find the download script"
    echo "Operation aborted"
    goto exit
endif
bash product_ftp_download.sh $exl_app
exit:
echo "Press any key to continue"
set ans="$<"


