# check for root
user=`/bin/id | /usr/bin/grep root | /usr/bin/wc -l`
if [ $user -eq 0 ]; then
	echo "You must be root to execute this script."
	exit 1
fi

# helper function
start_db () {
    /usr/bin/su - $SSTR_DBOWNER -c "$SSTR_DBSTART"
    this_try=1
    until /usr/bin/su $SSTR_DBOWNER  -c ". $SSTR_DBBIN/postgres.env; LD_LIBRARY_PATH=$SSTR_DBHOME/lib:$SSTR_DBHOME/nsm1/lib; export LD_LIBRARY_PATH; $SSTR_PSQL -l" > /dev/null 2>&1
    do
        if [ $this_try -ge 12 ]; then
            echo "Database still not up - giving up"
            exit 1
        fi
        echo "Wait and try connecting to database again..."
        this_try=`/usr/bin/expr $this_try + 1`
        sleep 10
    done
}

# if TR installed
/usr/bin/pkginfo -q SUNWnsmbj
if [ $? -eq 0 ]; then

    # if database is installed need to update some SQL
    PKG=SUNWnsmdb
    /usr/bin/pkginfo -q $PKG
    if [ $? -eq 0 ]; then
        BASEDIR=`/usr/bin/pkginfo -r $PKG 2>&1`
        if [ $? -ne 0 ]; then
            BASEDIR=/opt
        fi
	SSTR_HOME=$BASEDIR/SUNWnsm
	SSTR_DBHOME=$SSTR_HOME/util/pgsql
	SSTR_DBBIN=$SSTR_DBHOME/nsm1/bin
	SSTR_DBSTART=$SSTR_DBBIN/dbstart.sh
	SSTR_DBSTOP=$SSTR_DBBIN/dbstop.sh
	SSTR_DBOWNER=sstr001
	SSTR_PSQL=$SSTR_DBHOME/bin/psql
   	start_db 

        # make current (-02) changes
        echo "Making -02 DB changes..."
        /usr/bin/su $SSTR_DBOWNER  -c ". $SSTR_DBBIN/postgres.env; LD_LIBRARY_PATH=$SSTR_DBHOME/lib:$SSTR_DBHOME/nsm1/lib; export LD_LIBRARY_PATH; $SSTR_PSQL -f $SSTR_DBHOME/nsm1/ddl/patchadd-02.sql"
        # shut db back down
	/usr/bin/su - $SSTR_DBOWNER -c "$SSTR_DBSTOP"
    fi

    # if agent station has already been configured, need to update httpd.conf 
    # config file
    PKG=SUNWnsmat
    /usr/bin/pkginfo -q $PKG
    if [ $? -eq 0 ]; then
        BASEDIR=`/usr/bin/pkginfo -r $PKG 2>&1`
        if [ $? -ne 0 ]; then
            BASEDIR=/opt
        fi

        # if sstr.properties exists then it's been configured already
        PRODUCT_DIR=$BASEDIR/SUNWnsm
        NSMCONF_FILE=$PRODUCT_DIR/etc/sstr.properties
        if [ -f $NSMCONF_FILE ]; then
            echo "Reconfiguring the topology reporter agents..."
            sed_file () {
                src="$1"
                dst="$2"
                regexp="$3"
    
                ftmp=/tmp/nsmsed$$
                /bin/sed "$regexp" "$src" > $ftmp
                if [ $? -ne 0 ]; then
                    return 1
                fi
                mv "$ftmp" "$dst"
                if [ $? -ne 0 ]; then
                    return 1
                fi
                return 0;
            }

            setup_agent_apache () {
                src="$PRODUCT_DIR/util/apache/conf/httpd.conf.default"
                dst="$PRODUCT_DIR/util/apache/conf/httpd.conf"
                regexp="s/NSM_APACHE_PORT/$NSM_APACHE_PORT/g"
                sed_file "$src" "$dst" "$regexp"
                if [ $? -ne 0 ]; then
                    return 1
                fi
    
                regexp="s/NSM_APACHE_SERVERNAME/$NSM_APACHE_SERVERNAME/g"
                sed_file "$dst" "$dst" "$regexp"
                if [ $? -ne 0 ]; then
                    return 1
                fi
    
                regexp="s/NSM_APACHE_SERVERADMIN/$NSM_APACHE_SERVERADMIN/g"
                sed_file "$dst" "$dst" "$regexp"
                if [ $? -ne 0 ]; then
                    return 1
                fi
    
                chown root:other "$dst"
                chmod 644 "$dst"
    
                return 0;
            }

            NSM_REGISTRY_HOST=`/sbin/uname -n`
            . $NSMCONF_FILE
            setup_agent_apache
            if [ $? -ne 0 ]; then
                echo "Reconfigure of the topology reporter agent failed."
                echo "Please reconfigure the agent manually with "
                echo "$BASEDIR/SUNWstm/bin/esmconfig"
            fi
        fi
    fi

    echo "Topology Reporter needs to be restarted with the command"
    echo "\t/etc/init.d/sstrd start\n"
fi

# if CS installed
/usr/bin/pkginfo -q SUNWdm
if [ $? -eq 0 ]; then
    echo "Configuration Service needs to be restarted with the commands:"
    os=`/sbin/uname -r`
    if [ "$os" = "5.8" ]; then
	echo "\t/etc/init.d/prngd start"
    fi
    echo "\t/etc/init.d/sshd start"
    echo "\t/etc/init.d/sscs.wbem start"
    echo "\t/etc/init.d/sscs start\n"
fi

# if DE installed
PKG=SUNWsade
/usr/bin/pkginfo -q $PKG
if [ $? -eq 0 ]; then
    echo "Diagnostic Expert needs to be restarted with the command"
    echo "\t/etc/init.d/init.ssde start\n"
fi
