#!/usr/bin/ksh
#
# Copyright 2002-2003 by Sun Microsystems, Inc.
# All rights reserved.
#
# @(#)sstrsetup	1.23 03/03/24
#
# Push NSM sstr.properties file values out sstr components.
#
# All functions in this file should be treated as private except for
# the two public entry points:
# 		setup_mgmt_station
# 		setup_agent_station
# 		setup_both_station
###############################################################################

#
# PGSQL group, gid, user, uid, password and home are fixed.
PGSQL_GROUP=sstr001
PGSQL_USER=sstr001
PGSQL_USER_HOME=/opt/SUNWnsm/util/pgsql
PGSQL_USER_PASSWORD=sstr
PGSQL_COMMENT=Sun_StorEdge_Topology_Reporter
PGSQL_LOG=/var/opt/SUNWnsm/pgsql

#
# Function Primitives

function setup_mgmt_agent_slp {
	station="$1"

	if [ ! -f /etc/inet/slp.conf ]; then
		if [ ! -f /etc/inet/slp.conf.default ]; then
		cp $PRODUCT_DIR/etc/slp.conf.default /etc/inet/slp.conf
		fi
		if [ $? -ne 0 ]; then
			logit "ERROR: Unable to create SLP $station file /etc/inet/slp.conf."
			return 1
		fi
	fi

	chown root:sys /etc/inet/slp.conf
	chmod 644 /etc/inet/slp.conf

	# Check to make sure net.slp.isDA=true
	cp /etc/inet/slp.conf /etc/inet/slp.conf.bak
        sed -e "/^[#, ]*net.slp.isDA=/d" /etc/inet/slp.conf.bak \
        >/etc/inet/slp.conf;
	echo "net.slp.isDA=true" >> /etc/inet/slp.conf
        rm /etc/inet/slp.conf.bak

	# Prompt user for append or replace of slp scope
        SCOPE=`cat /etc/inet/slp.conf | egrep '^[ ]*net.slp.useScopes='`
        SCOPECOUNT=`cat /etc/inet/slp.conf | egrep -c '^[ ]*net.slp.useScopes='`
        ISMYSCOPESET=`echo $SCOPE | egrep '(=|,)[ ]*'$NSM_SCOPE'($|[ ]*,)'`

	if [ "$SCOPECOUNT" -gt 1 ]; then
		echo "ERROR: net.slp.useScopes is set more than once in /etc/inet/slp.conf."
		echo "       Please copy /opt/SUNWnsm/etc/slp.conf.default to"
		echo "       /etc/inet/slp.conf and rerun configure."
		bailout
	fi

        # Remove device scope string from the SCOPE string.
        CURRENT_SCOPE=`echo $SCOPE | sed -e "s/$NSM_DEVICE_SCOPE,//"`
        # Remove net.slp.useScopes= from the SCOPE string
        CURRENT_NSM_SCOPE="`echo $CURRENT_SCOPE | sed -e 's/net.slp.useScopes=//'`"


        APPENDSCOPE="Append $NSM_SCOPE to $CURRENT_NSM_SCOPE"
        REPLACESCOPE="Replace $CURRENT_NSM_SCOPE with $NSM_SCOPE"
        if [ "$SCOPE" = "" ];then
                append_file "/etc/inet/slp.conf" "net.slp.useScopes=$NSM_SCOPE"
        elif [ "$ISMYSCOPESET" = "" ];then
                echo "Current scope in /etc/inet/slp.conf is set to: $CURRENT_NSM_SCOPE"
                echo
                PS3="Pick one of the above options: "
                select choice in "$APPENDSCOPE" "$REPLACESCOPE"
                do case $choice in
                        $APPENDSCOPE)
                                update_nsm_scope;
                                update_slp; break; ;;
                        $REPLACESCOPE)
                                update_slp; break; ;;
                        esac
                done
        fi

	return 0
}

function setup_mgmt_slp {

	setup_mgmt_agent_slp "mgmt"
	if [ $? -ne 0 ]; then
		return 1
	fi

	/etc/init.d/slpd start

	return 0
}

function setup_agent_slp {

	setup_mgmt_agent_slp "agent"
	if [ $? -ne 0 ]; then
		return 1
	fi
	
	/etc/init.d/slpd start

	src="$PRODUCT_DIR/util/apache/fcgi-bin/_agentConfig.default"
	dst="$PRODUCT_DIR/util/apache/fcgi-bin/_agentConfig"
	regexp="s/NSM_SCOPE/$NSM_SCOPE/g"
	sed_file "$src" "$dst" "$regexp"
	if [ $? -ne 0 ]; then
		logit "ERROR: $src scope modification for SLP agent failed."
		return 1
	fi

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

	return 0
}

function setup_both_slp {

        setup_mgmt_agent_slp "both"
        if [ $? -ne 0 ]; then
                return 1
        fi

	/etc/init.d/slpd start

        src="$PRODUCT_DIR/util/apache/fcgi-bin/_agentConfig.default"
        dst="$PRODUCT_DIR/util/apache/fcgi-bin/_agentConfig"
        regexp="s/NSM_SCOPE/$NSM_SCOPE/g"
        sed_file "$src" "$dst" "$regexp"
        if [ $? -ne 0 ]; then
                logit "ERROR: $src scope modification for SLP agent failed."
                return 1
        fi

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

        return 0

}

function 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
		logit "ERROR: $src port modification for Apache failed."
		return 1
	fi

	src="$PRODUCT_DIR/util/apache/conf/httpd.conf"
	dst="$PRODUCT_DIR/util/apache/conf/httpd.conf"
	regexp="s/NSM_APACHE_SERVERNAME/$NSM_APACHE_SERVERNAME/g"
	sed_file "$src" "$dst" "$regexp"
	if [ $? -ne 0 ]; then
		logit "ERROR: $src server name modification for Apache failed."
		return 1
	fi

	src="$PRODUCT_DIR/util/apache/conf/httpd.conf"
	dst="$PRODUCT_DIR/util/apache/conf/httpd.conf"
	regexp="s/NSM_APACHE_SERVERADMIN/$NSM_APACHE_SERVERADMIN/g"
	sed_file "$src" "$dst" "$regexp"
	if [ $? -ne 0 ]; then
		logit "ERROR: $src server admin modification for Apache failed."
		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
		logit "ERROR: $src port modification for Apache failed."
		return 1
	fi

	src="$PRODUCT_DIR/util/apache/fcgi-bin/agents.xml"
	dst="$PRODUCT_DIR/util/apache/fcgi-bin/agents.xml"
	regexp="s/NSM_REGISTRY_HOST/$NSM_REGISTRY_HOST/g"
	sed_file "$src" "$dst" "$regexp"
	if [ $? -ne 0 ]; then
		logit "ERROR: $src server name modification for Apache failed."
		return 1
	fi

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

	return 0
}

function setup_mgmt_tomcat {
	src="$PRODUCT_DIR/util/tomcat/conf/server.xml.default"
	dst="$PRODUCT_DIR/util/tomcat/conf/server.xml"
    removef SUNWnsmut $dst > /dev/null 2>&1
    removef -f SUNWnsmut
	regexp="s/NSM_TOMCAT_NONSSL_PORT/$NSM_TOMCAT_NONSSL_PORT/g"
	sed_file "$src" "$dst" "$regexp"
	if [ $? -ne 0 ]; then
		logit "ERROR: $src Tomcat non-ssl port modification failed."
		return 1
	fi

	src="$PRODUCT_DIR/util/tomcat/conf/server.xml"
	dst="$PRODUCT_DIR/util/tomcat/conf/server.xml"
	regexp="s/NSM_TOMCAT_SSL_PORT/$NSM_TOMCAT_SSL_PORT/g"
	sed_file "$src" "$dst" "$regexp"
	if [ $? -ne 0 ]; then
		logit "ERROR: $src Tomcat ssl port modification failed."
		return 1
	fi

	chown root:other "$dst"
	chmod 644 "$dst"
    installf SUNWnsmut $dst f 0644 root other
    installf -f SUNWnsmut

	return 0
}

function setup_mgmt_pgsql {

   # Create user group in /etc/group
   # groupname:password:gid:user-list
   GROUP_OPTS="$PGSQL_GROUP"
   echo "groupadd $GROUP_OPTS" >> $LOG
   groupadd $GROUP_OPTS >> $LOG 2>&1
   rval=$?
   if [ $rval -eq 0 ]; then :
   elif [ $rval -eq 9 ]; then
      echo "groupmod $GROUP_OPTS" >> $LOG
      groupmod $GROUP_OPTS >> $LOG 2>&1
      rval=$?
      if [ $rval -ne 0 ]; then
         logit "ERROR: Unable to modify PGSQL group $PGSQL_GROUP."
         return 1
      fi
   else
      logit "ERROR: Unable to add PGSQL group $PGSQL_GROUP."
      return 1
   fi

   # Create user home dir
   if [ ! -d $PGSQL_USER_HOME ]; then
      mkdir -p $PGSQL_USER_HOME >> $LOG 2>&1
      rval=$?
      if [ $rval -ne 0 ]; then
         logit "ERROR: Unabled to create PGSQL user home $PGSQL_USER_HOME."
         return 1
      fi
   fi

   # Add user to /etc/passwd
   # username:password:uid:gid:gcos-field:home-dir:login-shell
   USER_OPTS="-c $PGSQL_COMMENT -g $PGSQL_GROUP -d $PGSQL_USER_HOME $PGSQL_USER"
   echo "useradd $USER_OPTS" >> $LOG
   useradd $USER_OPTS >> $LOG 2>&1
   rval=$?
   if [ $rval -eq 0 ]; then :
   elif [ $rval -eq 9 ]; then
      echo "usermod $USER_OPTS" >> $LOG
      usermod $USER_OPTS >> $LOG 2>&1
      rval=$?
      if [ $rval -ne 0 ]; then
         logit "ERROR: Unable to modify PGSQL user $PGSQL_USER."
         return 1
      fi
   else
      logit "ERROR: Unable to add PGSQL user $PGSQL_USER."
      return 1
   fi

   # Set user password
   expect_rval=1
   if [ -n "$EXPECT" ]; then
      ftmp=/tmp/change$$
      echo "spawn passwd $PGSQL_USER" >> $ftmp
      echo "expect \"ssword:\"" >> $ftmp
      echo "send \"$PGSQL_USER_PASSWORD\\r\"" >> $ftmp
      echo "expect \"ssword:\"" >> $ftmp
      echo "send \"$PGSQL_USER_PASSWORD\\r\"" >> $ftmp
      echo "expect eof" >> $ftmp

      $EXPECT $ftmp >> $LOG 2>&1
      expect_rval=$?
      rm -f $ftmp > /dev/null 2>&1
   fi

	# In silent mode expect is required to change password
	if [ $MODE -eq $SILENT ] && [ $expect_rval -ne 0 ]; then
		logit "ERROR: Expect failed to set PGSQL user password."
		return 1
	fi

   if [ $expect_rval -ne 0 ]; then
		echo
      echo "Please change user PGSQL $PGSQL_USER password:"
      passwd $PGSQL_USER
      rval=$?
      if [ $rval -ne 0 ]; then
         logit "ERROR: Unabled to set PGSQL password for $PGSQL_USER."
         return 1
      fi
   fi

   # Create sql server directory
   if [ ! -d $PGSQL_LOG ]; then
      mkdir -p $PGSQL_LOG >> $LOG 2>&1
      if [ $? -ne 0 ]; then
         logit "ERROR: Unabled to create PGSQL log directory $PGSQL_LOG."
         return 1
      fi
   fi

   # Change permissions on sql server directory
   chown -fR $PGSQL_USER:$PGSQL_GROUP $PGSQL_USER_HOME >> $LOG 2>&1
   rval=$?
   if [ $rval -ne 0 ]; then
      logit "ERROR: Unable to change permission on PGSQL directory $PGSQL_USER_HOME."
      return 1
   fi

   # Change permissions on sql server log directory
   chown -fR $PGSQL_USER:$PGSQL_GROUP $PGSQL_LOG >> $LOG 2>&1
   rval=$?
   if [ $rval -ne 0 ]; then
      logit "ERROR: Unable to change permission on PGSQL directory $PGSQL_LOG."
      return 1
   fi
}


function setup_mgmt_rmi {
	src="$PRODUCT_DIR/util/cre/population/transient.xml.default"
	dst="$PRODUCT_DIR/util/cre/population/transient.xml"
	removef SUNWnsmu $dst > /dev/null 2>&1
	removef -f SUNWnsmu
	regexp="s/NSM_RMI_REGISTRY_PORT/$NSM_RMI_REGISTRY_PORT/g"
	sed_file "$src" "$dst" "$regexp"
	if [ $? -ne 0 ]; then
		logit "ERROR: $src RMI registry port modification failed."
		return 1
	fi

	chown root:other "$dst"
	chmod 644 "$dst"
	installf SUNWnsmu $dst f 0644 root other
	installf -f SUNWnsmu

	return 0
}


function setup_mgmt_temp {

	# Add temp development setup code for all sstr mgmt components here

	echo "Update CRE Startup: SLP $PRODUCT_DIR/sbin/sstr.cre" >> $LOG
	src="$PRODUCT_DIR/sbin/sstr.cre.default"
	dst="$PRODUCT_DIR/sbin/sstr.cre"
	regexp="s/NSM_SCOPE/$NSM_SCOPE/g;s/NSM_DEVICE_SCOPE/$NSM_DEVICE_SCOPE/g"
	sed_file "$src" "$dst" "$regexp"
	if [ $? -ne 0 ]; then
		logit "ERROR: Update CRE Startup: SLP $src failed."
		return 1
	fi

	chown root:sys "$dst"
	chmod 500 "$dst"

	echo "Update TOMCAT Startup: SLP $PRODUCT_DIR/sbin/sstr.tomcat" >> $LOG
	src="$PRODUCT_DIR/sbin/sstr.tomcat.default"
	dst="$PRODUCT_DIR/sbin/sstr.tomcat"
	regexp="s/NSM_SCOPE/$NSM_SCOPE/g;s/NSM_DEVICE_SCOPE/$NSM_DEVICE_SCOPE/g"
	sed_file "$src" "$dst" "$regexp"
	if [ $? -ne 0 ]; then
		logit "ERROR: Update TOMCAT Startup: SLP $src failed."
		return 1
	fi

	chown root:sys "$dst"
	chmod 500 "$dst"

	return 0
}

function setup_agent_temp {

	# Add temp development setup code for all sstr agent components here
	return 0
}

function setup_mgmt_station {

        setup_mgmt_slp
        if [ $? -ne 0 ]; then
                echo "ERROR: SLP mgmt setup failed!" >> $LOG
                return 1
        fi

	## IR3: the following is moved to SUNWnsmdb
	#setup_mgmt_pgsql
	#if [ $? -ne 0 ]; then
	#	echo "ERROR: PGSQL mgmt setup failed!" >> $LOG
	#	return 1
	#fi

	setup_mgmt_tomcat
	if [ $? -ne 0 ]; then
		echo "ERROR: Tomcat mgmt setup failed!" >> $LOG
		return 1
	fi

	setup_mgmt_rmi
	if [ $? -ne 0 ]; then
		echo "ERROR: RMI Registry mgmt setup failed!" >> $LOG
		return 1
	fi

	$BASEDIR/SUNWnsm/sbin/imqconf >> $LOG 2>&1
	if [ $? -ne 0 ]; then
	    echo "ERROR: IMQ mgmt setup failed!" >> $LOG
	    return 1
	fi

	setup_mgmt_temp
	if [ $? -ne 0 ]; then
		echo "ERROR: Temp mgmt work around fixes failed!" >> $LOG
		return 1
	fi

	return 0
}

function setup_agent_station {

	setup_agent_slp
	if [ $? -ne 0 ]; then
		echo "ERROR: SLP agent setup failed!" >> $LOG
		return 1
	fi

	setup_agent_apache
	if [ $? -ne 0 ]; then
		echo "ERROR: Apache agent setup failed!" >> $LOG
		return 1
	fi

	setup_agent_temp
	if [ $? -ne 0 ]; then
		echo "ERROR: Temp agent work around fixes failed!" >> $LOG
		return 1
	fi

	return 0
}

function setup_both_station {

        setup_both_slp
        if [ $? -ne 0 ]; then
                echo "ERROR: SLP both setup failed!" >> $LOG
                return 1
        fi

	## IR3: the following is moved to SUNWnsmdb
        #setup_mgmt_pgsql
        #if [ $? -ne 0 ]; then
        #        echo "ERROR: PGSQL mgmt setup failed!" >> $LOG
        #        return 1
        #fi

        setup_mgmt_tomcat
        if [ $? -ne 0 ]; then
                echo "ERROR: Tomcat mgmt setup failed!" >> $LOG
                return 1
        fi

        $BASEDIR/SUNWnsm/sbin/imqconf >> $LOG 2>&1
        if [ $? -ne 0 ]; then
                echo "ERROR: IMQ mgmt setup failed!" >> $LOG
                return 1
        fi

        setup_mgmt_rmi
        if [ $? -ne 0 ]; then
		    echo "ERROR: RMI Registry mgmt setup failed!" >> $LOG
		    return 1
        fi

        setup_mgmt_temp
        if [ $? -ne 0 ]; then
                echo "ERROR: Temp mgmt work around fixes failed!" >> $LOG
                return 1
        fi

        setup_agent_apache
        if [ $? -ne 0 ]; then
                echo "ERROR: Apache agent setup failed!" >> $LOG
                return 1
        fi

        setup_agent_temp
        if [ $? -ne 0 ]; then
                echo "ERROR: Temp agent work around fixes failed!" >> $LOG
                return 1
        fi
}
