#!/bin/bash

. ./param_bash.ini

export ORAC="N"
export FILE_INPUT="N"

GET_PARAMETERS(){
    PROMPT_FOR_MINIMAL_VALUES
    FILL_IF_ALL_IN_ONE
    if [ "$AIO" != "Y" ] ; then
        if [ "$FILE_INPUT" == "N" ] ; then
            PROMPT_FOR_VALUES
        else
            READ_VALUES_FROM_FILE Y
        fi
    else
        READ_VALUES_FROM_FILE N
    fi
    FILL_LB
    if [ "${ORAC}" = "Y" ] ; then
        if [ "${RAC_TNS}" = "" -o "${RAC_JDBC}" = "" ] ; then
            BUILD_RAC_STRINGS
        fi
    fi
    FILL_PDS_PROTOCOL
}
FILL_PDS_PROTOCOL()
{
    if [ "$FILE_INPUT" == "N" ] ; then
        echo -n "Enter pds protocol type (http/https) [$PDS_PRO]:"
        read PROTOCOL
        if [ "$PROTOCOL" == "" ] ; then
            PROTOCOL=$PDS_PRO
        fi
    else
        PROTOCOL=`cat def.dat | grep PDS_PROTOCOL | cut -d'"' -f2`
    fi
    Update_params "PDS_PRO" "$PROTOCOL"
}
FILL_LB()
{
    export LB="N"
    if [ "$FILE_INPUT" == "N" ] ; then
        echo -n "Enter Y To Configure Load Balancer [N]Y:"
        read LB
    else
        LB=`cat def.dat | grep LOAD_BALANCER | cut -d'"' -f2`
    fi
    if [ "${LB}" = "y" -o "${LB}" = "Y" ] ; then
        export LB="Y"
        PROMPT_FOR_LB_VALUES
        FILL_SERVERS_LIST
    fi
}
FILL_IF_ALL_IN_ONE()
{
    if [ "$FILE_INPUT" == "N" ] ; then
        echo -n "Is it an ALL-IN-ONE installation [Y,n]: "
        read AIO
    else
        AIO=`cat def.dat | grep ALL_IN_ONE | cut -d'"' -f2`
    fi
    if [ "${AIO}" = "y" -o "${AIO}" = "Y" -o "${AIO}" = "" ] ; then
        AIO="Y"
    else
        if [ "${AIO}" = "n" -o "${AIO}" = "N" ] ; then
            AIO="N"
        else
            AIO=""
        fi
    fi
    if [ "$AIO" == "" ] ; then
        if [ "$FILE_INPUT" == "N" ] ; then
            FILL_IF_ALL_IN_ONE
        else
            ERROR "Problem with def.dat ALL_IN_ONE parameter"
        fi
    fi
    export AIO=$AIO
}
FILL_ORA_INST_TYPE()
{
    if [ "$FILE_INPUT" == "N" ] ; then
        echo -n "Choose your Oracle Installation Type [ Local, Remote, RAC ] : "
        read ORA_INST_TYPE
    else
        ORA_INST_TYPE=`cat def.dat | grep ORA_INST_TYPE | cut -d'"' -f2`
    fi
    if [ "${ORA_INST_TYPE}" != "Local" -a "${ORA_INST_TYPE}" != "Remote" -a "${ORA_INST_TYPE}" != "RAC" ] ; then
        if [ "$FILE_INPUT" == "Y" ] ; then
            ERROR "Problem with def.dat ORA_INST_TYPE parameter"
        fi
        FILL_ORA_INST_TYPE
    else
        Update_params "ORA_INST_TYPE" "${ORA_INST_TYPE}"
        export ORA_INST_TYPE=$ORA_INST_TYPE
    fi
}
PROMPT_FOR_MINIMAL_VALUES()
{
        FILL_ORA_INST_TYPE
        if [ "$ORA_INST_TYPE" = "RAC" ] ; then
            ORACLE_SERVER="RAC"
            export ORAC="Y"
            Update_params "ORACLE_SERVER" "${ORACLE_SERVER}"
        else
            if [ "$FILE_INPUT" == "N" ] ; then
                echo -n "Enter the oracle server [$ORACLE_SERVER] : "
                read ans
            else
                ans=`cat def.dat | grep ORACLE_SERVER | cut -d'"' -f2`
            fi
            if [ "${ans}" != "" ] ; then
                ORACLE_SERVER=${ans}
                Update_params "ORACLE_SERVER" "${ORACLE_SERVER}"
            fi
        fi
        if [ "$FILE_INPUT" == "N" ] ; then
            echo -n "Enter the Installation ID (A Unique 3 digit number shared across all Roles):"
            read idv
            echo -n "Enter the Oracle User Prefix [V2XX_] (Replace the two XX with two letters or digits or a combination):"
            read pref
            echo -n "Enter the Delivery Shared disk [$DELIVERY_SHARED]: "
            read dels
            [[ $dels == "" ]] && dels=$DELIVERY_SHARED
            echo -n "Enter the Deposit Repository disk [$DEPOSIT_REPOSITORY]:"
            read opes
            [[ $opes == "" ]] && opes=$DEPOSIT_REPOSITORY
            echo -n "Enter the Operational Shared disk [$OPERATIONAL_SHARED]:"
            read oper
            [[ $oper == "" ]] && oper=$OPERATIONAL_SHARED
            echo -n "Enter the Operational Repository disk [$OPERATIONAL_REPOSITORY]:"
            read depr
            [[ $depr == "" ]] && depr=$OPERATIONAL_REPOSITORY
            echo -n "Enter the Permanent Repository disk [$PERMANENT_REPOSITORY]:"
            read perr
            [[ $perr == "" ]] && perr=$PERMANENT_REPOSITORY        
 	else
            idv=`cat def.dat | grep Installation_ID | cut -d'"' -f2`
            pref=`cat def.dat | grep User_Prefix | cut -d'"' -f2`

            dels=`cat def.dat | grep DELIVERY_SHARED | cut -d'"' -f2`
            opes=`cat def.dat | grep DEPOSIT_REPOSITORY | cut -d'"' -f2`
            oper=`cat def.dat | grep OPERATIONAL_SHARED | cut -d'"' -f2`
            depr=`cat def.dat | grep OPERATIONAL_REPOSITORY | cut -d'"' -f2`
            perr=`cat def.dat | grep PERMANENT_REPOSITORY | cut -d'"' -f2`
        fi

        Update_params "INST_ID" "${idv}"
        ORA_UPREFIX="V2${pref}"
        Update_params "ORA_UPREFIX" "${ORA_UPREFIX}"

        Update_params "DELIVERY_SHARED" "${dels}"
        Update_params "DEPOSIT_REPOSITORY" "${opes}"
        Update_params "OPERATIONAL_SHARED" "${oper}"
        Update_params "OPERATIONAL_REPOSITORY" "${depr}"
        Update_params "PERMANENT_REPOSITORY" "${perr}"
}
FILL_LB_VALUES()
{
    ROLE=$1
    if [ "${ROLE}" = "DEPOSIT" ] ; then
        param_name=DEP_LB
        port=DEP_LB_PORT
    elif [ "${ROLE}" = "DELIVERY" ] ; then
        param_name=DEL_LB
        port=DEL_LB_PORT
    elif [ "${ROLE}" = "REPOSIT" ] ; then
        param_name=REP_LB
        port=REP_LB_PORT
    elif [ "${ROLE}" = "PERMANENT" ] ; then
        param_name=PER_LB
        port=PER_LB_PORT
    else
        param_name=PDS_LB
        port=PDS_LB_PORT
    fi
    if [ "$FILE_INPUT" == "N" ] ; then
        echo -n "Enter the $ROLE Server Load Balancer url : "
        read ans
        if [ "${ans}" != "" ] ; then
            param_val=${ans}
            export ${param_name}=${param_val}
            Update_params "${param_name}" "${param_val}"
        fi
        echo -n "Enter the $ROLE Server Load Balancer port : "
        read ans
        if [ "${ans}" != "" ] ; then
            param_val=${ans}
            export ${port}=${param_val}
            Update_params "${port}" "${param_val}"
        fi
    else
        param_val=`cat def.dat | grep $param_name | grep -v PORT | cut -d'"' -f2`
        export ${param_name}=${param_val}
        Update_params "${param_name}" "${param_val}"
        param_val=`cat def.dat | grep $port | cut -d'"' -f2`
        export ${port}=${param_val}
        Update_params "${port}" "${param_val}"
    fi
}
PROMPT_FOR_LB_VALUES()
{
    for f in DEPOSIT REPOSIT DELIVERY PERMANENT PDS ; do
        FILL_LB_VALUES $f
    done
}
# $1 = the name of the role to fill his list
# [ DEPOSIT/REPOSIT/PERMANENT ]
# $2 = the array to fill [DEP_LIST/REP_LIST/PER_LIST]
BUILD_LIST()
{
        ROLE=$1
        LIST=""
        if [ "${ROLE}" = "DEPOSIT" ] ; then
            name=DEP_LIST
            grp=DEP_GRP
        elif [ "${ROLE}" = "DELIVERY" ] ; then
            name=DEL_LIST
            grp=DEL_GRP
        elif [ "${ROLE}" = "REPOSIT" ] ; then
            name=REP_LIST
            grp=REP_GRP
        else
            name=PER_LIST
            grp=PER_GRP
        fi
        if [ "$FILE_INPUT" == "N" ] ; then
            while [ "${ans}" != "q" ]
            do
                echo -n "Please fill in the list of the $ROLE Servers, one at a time, 'q' to quit:"
                read ans
                if [ "${ans}" != "q" ] ; then
                    LIST="$LIST ${ans}"
                else
                    ans=q
                fi
            done
        else
            LIST=`cat def.dat | grep $name | cut -d'"' -f2`
        fi
        if [ "${LIST}" != "" ] ; then
            LIST=`echo $LIST | sed 's/ /,/g'`
            GRP=`echo $LIST | sed "s/,/[780$SLOT_NUMBER],/g" | sed "s/$/[780$SLOT_NUMBER]/"`
            Update_params "${name}" "\"${LIST}\""
            Update_params "${grp}" "\"${GRP}\""
        else
            echo "You must insert at least one Server name!!!"
            ans=""
            BUILD_LIST $1
        fi
        ans=""
        LIST=""
}
FILL_SERVERS_LIST(){
        echo ""
        echo "Fill in Deposit Servers list"
        echo ""
        BUILD_LIST "DEPOSIT"
        echo ""
        echo "Fill in Delivery Servers list"
        echo ""
        BUILD_LIST "DELIVERY"
        echo ""
        echo "Fill in Reposit Servers list"
        echo ""
        BUILD_LIST "REPOSIT"
        echo ""
        echo "Fill in Permanent Servers list"
        echo ""
        BUILD_LIST "PERMANENT"
}
BUILD_RAC_STRINGS()
{
        echo ""
        echo "RAC Configuration"
        echo ""
        RAC_TNS_PRE="RAC.$ORACLE_SID="
        RAC_JDBC_PRE="jdbc:oracle:thin:@"
        INFORM "Enter RAC DB Connection String"
        INFORM "Make sure to insert a correct String, it will used be to connect to the DB"
        INFORM "Make Sure there are No Spaces in the String"
        INFORM "This is an Example of the RAC Connection String:"
        INFORM "(description=(address_list=(load_balance=on)(failover=on)(address=(protocol=tcp)(host=il-dtldb01-vip.corp.exlibrisgroup.com)(port=1521))(address=(protocol=tcp)(host=il-dtldb02-vip.corp.exlibrisgroup.com)(port=1521)))(connect_data=(server=dedicated)(service_name=dps)))"
        INFORM "If you don't know/have the requested String, please stop and contact your DBA"
        CONFIRM
        if [ "$?" != "0" ] ; then
                CANCEL "Aborting Rosetta Configuration" "$0"
        else
            echo -n "Please Enter The String:"
            read -n 999 ans
            RAC_TNS="${RAC_TNS_PRE}${ans}"
            RAC_JDBC="${RAC_JDBC_PRE}${ans}"
            Update_params "RAC_TNS" "\"${RAC_TNS}\""
            Update_params "RAC_JDBC" "\"${RAC_JDBC}\""
        fi
}
PROMPT_FOR_VALUES(){
        echo -n "Enter the Delivery Server NAME [$DELIVERY_SERVER] : "
        read ans
        if [ "${ans}" != "" ] ; then
                DELIVERY_SERVER=${ans}
                Update_params "DELIVERY_SERVER" "${DELIVERY_SERVER}"
        fi
        echo -n "Enter the Deposit Server NAME [$DEPOSIT_SERVER] : "
        read ans
        if [ "${ans}" != "" ] ; then
                DEPOSIT_SERVER=${ans}
                Update_params "DEPOSIT_SERVER" "${DEPOSIT_SERVER}"
        fi
        echo -n "Enter the Repository Server NAME [$REPOSIT_SERVER] : "
        read ans
        if [ "${ans}" != "" ] ; then
                REPOSIT_SERVER=${ans}
                Update_params "REPOSIT_SERVER" "${REPOSIT_SERVER}"
        fi
        echo -n "Enter the Permanent Server NAME [$PERMANENT_SERVER] : "
        read ans
        if [ "${ans}" != "" ] ; then
                PERMANENT_SERVER=${ans}
                Update_params "PERMANENT_SERVER" "${PERMANENT_SERVER}"
        fi
}
READ_VALUES_FROM_FILE(){
    for param in DELIVERY_SERVER DEPOSIT_SERVER REPOSIT_SERVER PERMANENT_SERVER ; do
        if [ "$1" == "Y" ] ; then
            value=`cat def.dat | grep $param | cut -d'"' -f2`
        else
            value="$SERVER_NAME"
        fi
        Update_params "$param" "$value"
    done
}

clear
START "Rosetta Software Configuration" "$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

INFORM "Do you want me to read the installation parameters from def.dat file?"
CONFIRM
if [ "$?" == "0" ] ; then
        export FILE_INPUT="Y"
fi

GET_PARAMETERS

GRID_JGRP=`echo ${REP_GRP} | sed 's/780/980/g'`
Update_params "GRID_JGRP" "${GRID_JGRP}"
. ./param_bash.ini
clear
echo "The Rosetta configuration will be done with the following values:"
echo "THIS MACHINE        =$SERVER_NAME"
echo "ORACLE_SERVER       =$ORACLE_SERVER"
echo "DELIVERY_SERVER     =$DELIVERY_SERVER"
if [ "$LB" != "Y" ] ; then
    echo "DEPOSIT_SERVER      =$DEPOSIT_SERVER"
    echo "REPOSIT_SERVER      =$REPOSIT_SERVER"
    echo "PERMANENT_SERVER    =$PERMANENT_SERVER"
    echo ""
else
    echo "DELIVERY SERVER LIST  =$DEL_LIST"
    echo "DEPOSIT SERVER        =$DEPOSIT_SERVER"
    echo "DEPOSIT SERVER LIST   =$DEP_LIST"
    echo "REPOSIT SERVER        =$REPOSIT_SERVER"
    echo "REPOSIT SERVER LIST   =$REP_LIST"
    echo "PERMANENT SERVER      =$PERMANENT_SERVER"
    echo "PERMANENT SERVER LIST =$PER_LIST"
    echo "Load Balancer"
    echo "DELIVERY LB           =$DEL_LB"
    echo "DELIVERY LB PORT      =$DEL_LB_PORT"
    echo "REPOSIT LB            =$REP_LB"
    echo "REPOSIT LB PORT       =$REP_LB_PORT"
    echo "DEPOSIT LB            =$DEP_LB"
    echo "DEPOSIT LB PORT       =$DEP_LB_PORT"
    echo "PERMANENT LB          =$PER_LB"
    echo "PERMANENT LB PORT     =$PER_LB_PORT"
    echo "PDS LB                =$PDS_LB"
    echo "PDS LB PORT           =$PDS_LB_PORT"
    echo "JBOSS JGROUPS CACHE"
    echo "DEPOSIT GROUP         =$DEP_GRP"
    echo "REPOSIT GROUP         =$REP_GRP"
    echo "GRIDGAIN GROUP        =$GRID_JGRP"
    echo "PERMANENT GROUP       =$PER_GRP"
    echo ""
fi
echo "INSTALLATION ID     =$INST_ID"
echo "ORACLE USER PREFIX  =$ORA_UPREFIX"
echo "PDS_PROTOCOL        =$PDS_PRO"
echo ""
echo "DELIVERY_SHARED     =$DELIVERY_SHARED"
echo "DEPOSIT_REP         =$DEPOSIT_REPOSITORY"
echo "OPERATIONAL_SHARED  =$OPERATIONAL_SHARED"
echo "OPERATIONAL_REP     =$OPERATIONAL_REPOSITORY"
echo "PERMANENT_REP       =$PERMANENT_REPOSITORY"
echo ""

CONFIRM
if [ "$?" = "0" ] ; then
        if [ ~ = "$APP_HOME" ] ; then
                bash config_dps.bash ${LB} ${ORAC} >> $gen_log 2>&1
        else
                su $param_to_su $MAIN_USER -c "bash config_dps.bash ${LB} ${ORAC}" >> $gen_log 2>&1
        fi
else
        CANCEL "Rosetta configuration is Canceled" "$0"
fi
if [ "$?" != "0" ] ; then
        ERROR "The configuration of the ${APP_HOME}/${SOFT_DIR}$SLOT_NUMBER has failed" "$0"
else
        INFORM "The configuration of DPS is done, moving on to change_post"
fi

if [ ~ = "$APP_HOME" ] ; then
        csh -f run_change_post.csh '${APP_HOME}/${SOFT_DIR}${SLOT_NUMBER}/profile' '$gen_log' >> $gen_log 2>&1
else
        su $param_to_su $MAIN_USER -c "csh -f run_change_post.csh '${APP_HOME}/${SOFT_DIR}${SLOT_NUMBER}/profile' '$gen_log'" >> $gen_log 2>&1
fi
if [ "$?" != "0" ] ; then
        ERROR "Failed during change post" "$0"
else
        INFORM "Done with change_post, moving on to pds configuration"
fi

if [ ~ = "$APP_HOME" ] ; then
        csh -f run_pds_config.csh '${APP_HOME}/${SOFT_DIR}${SLOT_NUMBER}/profile' '$gen_log' >> $gen_log 2>&1
else
        su $param_to_su $MAIN_USER -c "csh -f run_pds_config.csh '${APP_HOME}/${SOFT_DIR}${SLOT_NUMBER}/profile' '$gen_log'">> $gen_log 2>&1
fi
if [ "$?" != "0" ] ; then
        ERROR "Failed during pds configuration" "$0"
else
        INFORM "Done with pds configuration, Creating dps Unix login menu"
fi

if [ ~ = "$APP_HOME" ] ; then
        bash create_dps_login_menu.bash
else
        su $param_to_su $MAIN_USER -c "bash create_dps_login_menu.bash"
fi
if [ "$?" != "0" ] ; then
        WARN "An Error occured during creation of dps Unix login menu"
fi

if [ ~ = "$APP_HOME" ] ; then
        bash install_clamv.bash >> $gen_log 2>&1
else
        su $param_to_su $MAIN_USER -c "bash install_clamv.bash" >> $gen_log 2>&1
fi
if [ "$?" != "0" ] ; then
        WARN "An Error occured during installing clamv anti-virus"
fi

rm ${APP_HOME}/${SOFT_DIR}${SLOT_NUMBER}/system.dir/thirdparty/gridgain-2.1.1/libs/ext/* 
cd ${APP_HOME}/${SOFT_DIR}${SLOT_NUMBER}/system.dir/thirdparty/gridgain-2.1.1/libs/ext/ 
ln -s ${APP_HOME}/${SOFT_DIR}${SLOT_NUMBER}/system.dir/thirdparty/jboss/server/default/deploy/repository.ear/*.jar . 
ln -s ${APP_HOME}/${SOFT_DIR}${SLOT_NUMBER}/system.dir/thirdparty/jboss/server/default/deploy/repository.ear/lib/jdom.jar . 
ln -s ${APP_HOME}/${SOFT_DIR}${SLOT_NUMBER}/system.dir/thirdparty/jboss/server/default/deploy/repository.ear/lib/*droid*.jar . 
ln -s ${APP_HOME}/${SOFT_DIR}${SLOT_NUMBER}/system.dir/thirdparty/jboss/server/default/deploy/repository.ear/lib/commons-io.jar . 
ln -s ${APP_HOME}/${SOFT_DIR}${SLOT_NUMBER}/system.dir/thirdparty/jboss/server/default/deploy/repository.ear/lib/commons-collections.jar .

DONE "Configuration of Rosetta software completed successfully" "$0"
