#!/bin/bash

. ./param_bash.ini


MainUserExist
if [ "$?" = "1" ] ; then 
    ERROR "$MAIN_USER user doesn't exist" "$0"
fi

if [ "$MODE" = "0" ] ; then
    echo -n "Please enter a slot number $SOFT_DIR[$SLOT_NUMBER] : "
    read ans
    if [ "$ans" != "" ] ; then	
        SLOT_NUMBER=$ans	
    fi	
fi
if [ ! -d $APP_HOME/$SOFT_DIR$SLOT_NUMBER ] ; then
    ERROR "the directory ${APP_HOME}/${SOFT_DIR}$SLOT_NUMBER does not exist in the server" "$0"
fi
grep "${SOFT_DIR}${SLOT_NUMBER}" ${APP_HOME}/def_dps.dat > /dev/null 2>&1
if [ $? = "0" ] ; then 
    INFORM "${SOFT_DIR}${SLOT_NUMBER} already exist in the def_dps.dat file"
    INFORM "duplicate lines will not be inserted!!"
    INFORM "delete the lines manually or skip this step"
    echo "Press enter to continue:"
    read
    exit 1
fi

sav=$DD

INFORM "Backup the current def_dps.dat and def_version files"
if [ ! -f $APP_HOME/.cshrc ] ; then
    cp -p $IKIT_DIR/cshrc.dps $APP_HOME/.cshrc > /dev/null
    chown $MAIN_USER:$GROUP $APP_HOME/.cshrc > /dev/null 2>&1
else
    mv $APP_HOME/.cshrc $APP_HOME/.cshrc.$sav
    cp -p $IKIT_DIR/cshrc.dps $APP_HOME/.cshrc > /dev/null
    chown $MAIN_USER:$GROUP $APP_HOME/.cshrc > /dev/null 2>&1
fi
if [ -f $APP_HOME/def_dps.dat ] ; then  
    cp -p $APP_HOME/def_dps.dat $APP_HOME/def_dps.dat.$sav
    cp -p $APP_HOME/def_version $APP_HOME/def_version.$sav
else
    cp -p $IKIT_DIR/def_version.dps $APP_HOME/def_version
    cp -p $IKIT_DIR/def_dps.dat $APP_HOME/def_dps.dat
    chown $MAIN_USER:$GROUP $APP_HOME/def_dps.dat
    chown $MAIN_USER:$GROUP $APP_HOME/def_version
    cat $APP_HOME/def_dps.dat | \
	sed "s:d4_1:${SOFT_DIR}${SLOT_NUMBER}:g" | \
	sed "s:8991:${PORT_PRE}${SLOT_NUMBER}:" > $APP_HOME/def_dps.dat.new
    chown $MAIN_USER:$GROUP $APP_HOME/def_dps.dat.new
    cp -fp $APP_HOME/def_dps.dat.new $APP_HOME/def_dps.dat
    INFORM "The Rosetta login menu was created"
    exit 0
fi

INFORM "Adding the version $APP_HOME/$SOFT_DIR$SLOT_NUMBER to the Rosetta login menu"

if [ "$USER" = "$MAIN_USER" ] ; then
    csh -f add_dir_to_menu.csh $SLOT_NUMBER $APP_HOME $SOFT_DIR >> $gen_log
else
    su $param_to_su $MAIN_USER -c "csh -f add_dir_to_menu.csh $SLOT_NUMBER $APP_HOME $SOFT_DIR >> $gen_log"
fi

if [ "$?" = "0" ] ; then         	
    INFORM "The Rosetta login menu was created"
    exit 0
else
    INFORM "the Rosetta login menu was not created"
    exit 1
fi
