#!/bin/bash

. ./param_bash.ini

clear

echo "Please make sure to start Rosseta for the first time before running the SP"
CONFIRM
if [ "$?" != "0" ] ; then
    exit 1
fi

START "Service Pack installation" "$0"
if [ ! -w /etc/passwd ] && [ "$USER" != "$MAIN_USER" ] ; then
	ERROR "Run the script as $MAIN_USER user or root user" "$0"
fi

MainUserExist
if [ "$?" = "1" ] ; then 
	ERROR "$MAIN_USER user does not exist" "$0"
fi


if [ ! -d ${APP_HOME}/${SOFT_DIR}${SLOT_NUMBER} ] ; then
	ERROR "The directory ${APP_HOME}/${SOFT_DIR}$SLOT_NUMBER does not exist on the server" "$0"
fi
echo -n "Enter the location of the SP distribution package ${SERVICE_PACK} [$IKIT_FTP_DIR] : "
read ans
if [ "$ans" != "" ] ; then
	IKIT_FTP_DIR=$ans
fi

ls ${IKIT_FTP_DIR}/*${SERVICE_PACK}*.tar.gz_aa > /dev/null 2>&1
if [ "$?" != "0" ] ; then
	ERROR "The SP package ${APP_HOME}/${SOFT_DIR}$SLOT_NUMBER/*${SERVICE_PACK}*.tar.gz* does not exist on the server" "$0"
fi
cd ${IKIT_FTP_DIR}
cat ${IKIT_FTP_DIR}/*${SERVICE_PACK}*.tar.gz_* | $TAR -xzvf - >> $gen_log
if [ $? != "0" ] ; then
	ERROR "Cannot extract the package ${IKIT_FTP_DIR}/${SERVICE_PACK} into ${IKIT_FTP_DIR}" "$0"
fi 
chown -R $MAIN_USER:$GROUP ${IKIT_FTP_DIR}/*
cd ${IKIT_FTP_DIR}/RoziKit
################################################################################
if [ -w /etc/passwd ] ; then
	su $param_to_su ${MAIN_USER} -c "csh -f install_service_pack.csh '${APP_HOME}/${SOFT_DIR}${SLOT_NUMBER}/profile' '$IKIT_FTP_DIR'" >> $gen_log 2>&1

else
	csh -f install_service_pack.csh  "${APP_HOME}/${SOFT_DIR}${SLOT_NUMBER}/profile" "$IKIT_FTP_DIR" >> $gen_log 2>&1
fi 

###############################################################################
if [ "$?" = "0" ] ; then 
	DONE "The Service Pack was installed correctly" "$0"
else
	ERROR "Failed to install the Service Pack" "$0"	
fi
