#!/bin/bash

. ./param_bash.ini

clear
if [ "$FTP_SERVER" == "" ] ; then
    INFORM "The parameter FTP_SERVER is not defined"
    INFORM "Do you want me to find the best ftp for you?"
    CONFIRM
    if [ "$?" = "0" ] ; then
        bash check_best_ftp
        . ./param_bash.ini
    else
       ERROR "Set the parameter FTP_SERVER in param_bash.ini and rerun this step"
    fi
    if [ "$FTP_SERVER" == "" ] ; then
        ERROR "No FTP_SERVER where found in param_bash.ini file please set the parameter and rerun this step" "$0"
    fi
fi

test $FTP_TEMPL_VER
if [ "$?" != "0" ] ; then
        ERROR "Unsupported OS" "$0"
fi

START "Download Rosetta and Oracle software and binaries" "$0"

echo -n "Please enter the target download directory name [$IKIT_FTP_DIR]: "
read ans
if [ "$ans" != "" ] ; then
	IKIT_FTP_DIR=$ans
fi
if [ ! -d $IKIT_FTP_DIR ] ; then
	ERROR "Directory $IKIT_FTP_DIR does not exist" "$0"
fi

Update_params  "IKIT_FTP_DIR" "${IKIT_FTP_DIR}"

echo -n "Please enter you email [$MAIL]: "
read ans

if [ "$ans" != "" ] ; then	
	MAIL=$ans
fi
Update_params "MAIL" "${MAIL}"

bash -f main_ftp_proc.bash "$FTP_TEMPL_VER" "ver" "$IKIT_FTP_DIR" "$MAIL"
bash -f main_ftp_proc.bash "$FTP_TEMPL_PRD" "prd" "$IKIT_FTP_DIR" "$MAIL"

INFORM "Downloading Third party products scripts..."
$WGET -N --append-output $gen_log  ${WGET_PASSIVE} ftp://$FTP_USER:$FTP_PASSWD@$FTP_SERVER/product/scripts/"*" > /dev/null 2>&1
if [ -f product_ftp_download.sh ] ; then
    bash product_ftp_download.sh $MAIN_PRODUCT
else
    ERROR "Could not download third party product scripts"
fi

INFORM "The ftp now runs, please wait."
INFORM "When the ftp will be finished, e-mail will be sent to preliminary mentioned "
INFORM "address and the empty file ROSETTA_SOFT_FTP_COMPLETED or ORA_SOFT_FTP_COMPLETED will be created in the current directory." 

DONE "Please check the FTP additional log files in the $IKIT_DIR/log directory" "$0"
