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

    # if agent station has already been configured, need to update agents.xml 
    # and httpd.conf config files
    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
        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"

            src="$PRODUCT_DIR/util/apache/fcgi-bin/agents.xml.default"
            dst="$PRODUCT_DIR/util/apache/fcgi-bin/agents.xml"
            regexp="s/NSM_APACHE_PORT/$NSM_APACHE_PORT/g"
            sed_file "$src" "$dst" "$regexp"
            if [ $? -ne 0 ]; then
                return 1
            fi

            regexp="s/NSM_REGISTRY_HOST/$NSM_REGISTRY_HOST/g"
            sed_file "$dst" "$dst" "$regexp"
            if [ $? -ne 0 ]; then
                return 1
            fi

            chown root:other "$dst"
            chmod 644 "$dst"

            return 0;
        }

        echo "Reconfiguring the topology reporter agents..."
	PRODUCT_DIR=$BASEDIR/SUNWnsm
	NSMCONF_FILE=$PRODUCT_DIR/etc/sstr.properties
	if [ -f $NSMCONF_FILE ]; then
	    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
/usr/bin/pkginfo -q SUNWsade
if [ $? -eq 0 ]; then
    echo "Diagnostic Expert needs to be restarted with the command"
    echo "\t/etc/init.d/init.ssde start\n"
fi
