#!/bin/ksh
#
# $Revision: 1.13 $
# $Date: 2005/10/24 01:49:56 $
#
LANG=C
export LANG

if [ "$PKG_INSTALL_ROOT" = "/" ]; then
	PKG_INSTALL_ROOT=""
fi

TAR_CMD=/usr/bin/tar
CUR_DIR=`pwd`


# create agent config dir.
if [ ! -d ${PKG_INSTALL_ROOT}/var/opt/SUNWsscs/ssagent ]; then
    mkdir -p ${PKG_INSTALL_ROOT}/var/opt/SUNWsscs/ssagent 2>&1 >/dev/null
fi


# resotre configuration.
if [ "$RESTORE_CFG" = "true" ]; then
    # resotre console config files
    if [ -f ${PKG_INSTALL_ROOT}/var/opt/SUNWsscs/sscscon.cfg.tar ]; then
        cd ${PKG_INSTALL_ROOT}/opt/SUNWsscs/sscsconsole
        tar xf ${PKG_INSTALL_ROOT}/var/opt/SUNWsscs/sscscon.cfg.tar >/dev/null 2>&1
        rm -f ${PKG_INSTALL_ROOT}/var/opt/SUNWsscs/sscscon.cfg.tar >/dev/null 2>&1
    fi

    # resotre agent cfg files
    if [ -f ${PKG_INSTALL_ROOT}/var/opt/SUNWsscs/sscsagt.cfg.tar ]; then
        cd ${PKG_INSTALL_ROOT}/var/opt/SUNWsscs/ssagent
        tar xf ${PKG_INSTALL_ROOT}/var/opt/SUNWsscs/sscsagt.cfg.tar >/dev/null 2>&1
        rm -f ${PKG_INSTALL_ROOT}/var/opt/SUNWsscs/sscsagt.cfg.tar >/dev/null 2>&1
    fi

    # resotre dr ui cfg files
    if [ -f ${PKG_INSTALL_ROOT}/var/opt/SUNWsscs/ssdgrptui.cfg.tar ]; then
        cd ${PKG_INSTALL_ROOT}/opt/SUNWsscs/ssdiagreporterui
        tar xf ${PKG_INSTALL_ROOT}/var/opt/SUNWsscs/ssdgrptui.cfg.tar >/dev/null 2>&1
        rm -f ${PKG_INSTALL_ROOT}/var/opt/SUNWsscs/ssdgrptui.cfg.tar >/dev/null 2>&1
    fi

    # resotre dr daemon cfg files
    if [ -f ${PKG_INSTALL_ROOT}/var/opt/SUNWsscs/ssdgrptd.cfg.tar ]; then
        cd ${PKG_INSTALL_ROOT}/opt/SUNWsscs/ssdiagreporterd
        tar xf ${PKG_INSTALL_ROOT}/var/opt/SUNWsscs/ssdgrptd.cfg.tar >/dev/null 2>&1
        rm -f ${PKG_INSTALL_ROOT}/var/opt/SUNWsscs/ssdgrptd.cfg.tar >/dev/null 2>&1
    fi

    cd ${CUR_DIR}
fi

# create sstrapd.conf
if [ ! -f ${PKG_INSTALL_ROOT}/var/opt/SUNWsscs/ssagent/sstrapd.conf ]; then
	CUR_DIR=`pwd`
	cd ${PKG_INSTALL_ROOT}/var/opt/SUNWsscs/ssagent
	cp ${PKG_INSTALL_ROOT}/$BASEDIR/SUNWsscs/ssagent/sstrapd.conf . >/dev/null 2>&1
	cd ${CUR_DIR}
fi

# add users for agent.
if [ "${PKG_INSTALL_ROOT:-/}" = "/" ]; then
	addssuser() {
    	#
    	# addssuser have two function
    	# First it can judge the user exist or not
    	# Second it can set the UID to some value which may be useful for add 
    	# a user into NIS server. if you did not care the user is in NIS or not
    	# then you can not specife the UID value.
    	#
        uname=$1; shift

        getent passwd $uname >/dev/null && {
            echo "The username $uname already exists."
            return 1
        }
        
        i=60000
        while [ $i -gt 0 ] && getent passwd $i >/dev/null; do
          i=`expr $i - 1`
        done
        
        test $i -lt 1 && {
            echo >&2 "There is no free user ID available for username $uname."
            return 2
        }
        echo "Using uid $i for username $uname."

        useradd -d / -u $i  -g nobody -s /bin/false  "$@" $uname
        #passwd -d $uname
        return 0
	}
	
	ADDUSERS=""
	addssuser ssmon -c 'Sun StorEdge(tm) Configuration Service Monitor'
	if [ "$?" = "0" ]; then 
	    ADDUSERS="ssmon"
	fi
	addssuser ssadmin -c 'Sun StorEdge(tm) Configuration Service Admin' 
	if [ "$?" = "0" ]; then 
	    if [ "$ADDUSERS" != "" ]; then
            ADDUSERS="${ADDUSERS},"
	    fi
        ADDUSERS="${ADDUSERS} ssadmin"
    fi
	addssuser ssconfig -c 'Sun StorEdge(tm) Configuration Service Config' 
	if [ "$?" = "0" ]; then 
	    if [ "$ADDUSERS" != "" ]; then
            ADDUSERS="${ADDUSERS},"
	    fi
        ADDUSERS="${ADDUSERS} ssconfig"
    fi
	
	#if [ "$ADDUSERS" != "" ]; then
        echo ""
        #echo "Be sure to set a password for the SSCS users(${ADDUSERS}). "
        #echo "Not doing so will allow other SSCS consoles to log in and change the "
        #echo "configuration."
    #fi
    echo "Be sure to set the passwords for all SSCS users(ssmon,ssadmin,ssconfig)."
	echo ""
fi


NEED_SN=false


## for console
# create needed files for console
CONSOLE_INSTALL_DIR=${PKG_INSTALL_ROOT}/opt/SUNWsscs/sscsconsole
if [ ! -f ${CONSOLE_INSTALL_DIR}/browser.default ]; then
	echo "/usr/dt/bin/netscape" > ${CONSOLE_INSTALL_DIR}/browser.default
	chmod 0664 ${CONSOLE_INSTALL_DIR}/browser.default
	chgrp bin ${CONSOLE_INSTALL_DIR}/browser.default
fi

if [ "$NEED_SN" = "true" ]; then
    if [ ! -f ${CONSOLE_INSTALL_DIR}/serial.txt ]; then
    	touch  ${CONSOLE_INSTALL_DIR}/serial.txt
    	chmod 0664 ${CONSOLE_INSTALL_DIR}/serial.txt
    	chgrp bin ${CONSOLE_INSTALL_DIR}/serial.txt
    fi
fi

#Create files which are used by raidscape with proper permissions
touch ${CONSOLE_INSTALL_DIR}/errlog.txt
chgrp bin ${CONSOLE_INSTALL_DIR}/errlog.txt
chmod 0666 ${CONSOLE_INSTALL_DIR}/errlog.txt

touch ${CONSOLE_INSTALL_DIR}/eventlog.txt
chgrp bin ${CONSOLE_INSTALL_DIR}/eventlog.txt
chmod 0664 ${CONSOLE_INSTALL_DIR}/eventlog.txt

touch ${CONSOLE_INSTALL_DIR}/svrlist.dat
chgrp bin ${CONSOLE_INSTALL_DIR}/svrlist.dat
chmod 0664 ${CONSOLE_INSTALL_DIR}/svrlist.dat


echo ""

echo "Before starting the Console, please run "
if [ "$NEED_SN" = "true" ]; then
    echo "${CONSOLE_INSTALL_DIR}/config_sscon."
    echo "to input the Product Serial Number, the authentication code and "
    echo "the pathname of the default Web Browser to access HTML help files"
else
    echo "${CONSOLE_INSTALL_DIR}/config_sscon to specify the web browser"
    echo "to use for accessing online help."
fi
echo ""

echo "The Sun StorEdge Configuration Service agent and Sun StorEdge Diagnostic"
echo "Reporter daemon have been installed, but are not configured to start at boot"
echo "time by default."
echo ""
echo "To enable the Sun StorEdge Configuration Service agent to start automatically"
echo "when the system boots, and to start it now, type:"
echo ""
echo "/etc/init.d/ssagent enable start"
echo ""
echo "To enable the Sun StorEdge Diagnostic Reporter to start automatically when the"
echo "system boots, and to start it now, type:"
echo ""
echo "/etc/init.d/ssdgrptd enable start"
echo ""
echo "Refer to the Sun StorEdge 3000 Family Software Installation Guide for more"
echo "information."
echo ""

cd ${CUR_DIR}


#
# we do not auot start the agent and dr daemon
#
#if [ "${PKG_INSTALL_ROOT:-/}" = "/" ]; then
#    /etc/init.d/ssdgrptd start
#    /etc/init.d/ssagent start
#fi


exit 0
