#! /bin/ksh
#
#	Copyright 06/29/98 Sun Microsystems, Inc.  All Rights Reserved.
#
#ident "@(#)informix_svc_start_net.shi   1.13     98/06/29 SMI"
#
# informix_svc_start_net - start all HA DBMS data services on hosts in
#  HA_METASETSERVE
#
# Environment:
# HA_ENV should be set, and point to the appropriate file
# Usage: informix_svc_start_net
#

#	Copyright 11/19/97 Sun Microsystems, Inc.  All Rights Reserved.
#
# @(#)informix_boiler	1.19 97/11/19 SMI

# begin of common ha-dbms boilerplate

# remember our name
argv0=`basename $0`

# Get the BASEDIR and PRODUCTDIR settings from the installed pkgs
_basedir=`pkgparam SUNWscinf BASEDIR 2>/dev/null`
_productdir=`pkgparam SUNWscinf PRODUCTDIR 2>/dev/null`
_basedir=${_basedir:=""}
_productdir=${_productdir:="SUNWcluster"}

LOGGER=/usr/bin/logger

PATH=${_basedir}/${_productdir}/bin:${_basedir}/${_productdir}/ha/informix:${PATH}
export PATH
HA_FILES=/etc/${_basedir}/SUNWscinf; export HA_FILES
HA_VAR=/var/${_basedir}/SUNWscinf; export HA_VAR
ds="informix"

PREFIX="SUNWcluster.ha.${ds}"
HA_FM_NAME="fault_mon"

# include HA utilities library
. dbms_utilities
if [ $? -ne 0 ]; then
	$LOGGER -p local7.err -t "$HA_SLOGTAG" "$argv0: Cannot find HA utilities library"
	exit 1
fi

HA_CLUSTER=$CLUSTNAME; export HA_CLUSTER

# some default files and locations
# don't move this up - it depends on HA_FILES, which gets set above
HA_DATABASES=hainformix_databases
HA_DB_SUPPORT=${HA_FILES}/hainformix_support

# verify that we have the hainformix_support file
if [ ! -r $HA_DB_SUPPORT ] ; then
  logerr "4000" "file ${HA_DB_SUPPORT} does not exist or is not readable!"
  exit 2
fi


# talk_start ... - echo the args, run them, and evaluate return value
talk_start() {
  	logprint "$$: $*"
  	if $* ; then
    		return 0
  	else
    		logprint "$$: $* ... failed"
    		return 1
  	fi
}


# talk_start_bg ... - print the args, then run them in the background
talk_start_bg() {
  	logprint "$$: $*"
  	$* &
  	}


# get_pid [ -u userid ] pattern - find the process id of a running program
# if called with "-u userid", only process for that user will be considered
get_pid() {
	if [ "$1" = "-u" ] ; then
		ps_args="-f -u $2"
		shift
		shift
	else
		ps_args="-ef"
	fi
	PID=`/usr/bin/ps $ps_args | grep "\<$*\>" | nawk -v pat="$*" '
 BEGIN { 
   search_pat = substr(pat, 0, 79)
   }
 { if ( match($0, search_pat) )
     if ( ! match($0, "nawk -v pat=") )
       print $2
  }
'`
}


# kill_proc pattern - find and terminate a process
kill_proc() {
  	logprint "Killing $*..." 
  	get_pid $*
  	if [ "$PID" ] ; then
    		for p in ${PID} ; do
      			logprint "Killing process id $p"
      			kill $p
    		done
    		echo
  	else
    		logprint "process \"$*\" could not be located..." 
  	fi
}

# read_ha_databases onconfig - find an entry in the HA_DATABASES file,
# 	parse it apart, and return in variables. Assumes that INFORMIXDIR
#	is set!
read_ha_databases() {
#	conf_line=`grep "^o[nf][f]*	$1	" $HA_DATABASES`
	conf_line=$(get_instance "${ds}" "$1")
        set_inst_name $1

	if [ "$conf_line" = "" ] ; then
		logerr "4010" "$1 missing from HA INFORMIX CCD!"
		return 1
	fi
        on_off_mode=`echo "$conf_line" | cut -s -f 1`
	onconfig=`echo "$conf_line" | cut -s -f 2`
	logical_host=`echo "$conf_line" | cut -s -f 3`
	poll_cycle=`echo "$conf_line" | cut -s -f 4`
	connect_cycle=`echo "$conf_line" | cut -s -f 5`
	timeout=`echo "$conf_line" | cut -s -f 6`
	restart_delay=`echo "$conf_line" | cut -s -f 7`
	dbname=`echo "$conf_line" | cut -s -f 8`
	DB_LOGIN=$dbname
	export DB_LOGIN
	informixserver=`echo "$conf_line" | cut -s -f 9`
        set_logical_host $logical_host

	return 0
}

# find_informix onconfig - finds and sets various Informix related variables:
#	INFORMIXDIR ONINIT INFORMIXSERVER ONCONFIG
# in case of a problem, it returns with an empty INFORMIXDIR variable
find_informix() {

typeset my_node
typeset physical_hosts

  	INFTAB=/var/opt/informix/inftab
	
	INFORMIXDIR=""
	ONINIT=""
	ONCONFIG=""
	INFORMIXSERVER=""

        read_ha_databases $1
	if [ $? -ne 0 ]; then
   		return 1
	fi	
	my_node=`uname -n`
	physical_hosts=$(haget -f physical_hosts -h $logical_host | tr '\012' ' ')
	is_member "$my_node" "$physical_hosts"
	if [ $? -ne 0 ] ; then
   		return 1
	fi	
	
        if inftab_line=`grep "^[	 ]*$1:" $INFTAB` ; then
                informixdir=`echo $inftab_line | awk -F: '{print $2}' -`
                if [ -d $informixdir ] && [ -n "`disk_mounted $1`" ] ; then
                        if [ -x ${informixdir}/bin/oninit ] ; then
                                INFORMIXDIR="$informixdir"
                                ONINIT="${INFORMIXDIR}/bin/oninit"
                        else
                                logerr "4020" "No oninit executable in ${INFORMIXDIR}/bin!"
				return 1
                        fi
		else
                	INFORMIXDIR=""
			return 1
                fi
        else
                logerr "4030" "Database '$1' not found in ${INFTAB}"
		return 1
        fi
 
        if [ -r ${informixdir}/etc/${onconfig} ] ; then
                ONCONFIG="$onconfig"
                ONCONFIGFILE=${informixdir}/etc/${onconfig}
        else
                logerr "4040" "Database '$1' onconfig file not found or is not readable!"
                INFORMIXDIR=""
		return 1
        fi
 
        if [ -r ${informixdir}/etc/sqlhosts ] ; then
                INFORMIXSQLHOSTS=${informixdir}/etc/sqlhosts
        else
                logerr "4050" "sqlhosts file not found or is not readable!"
                INFORMIXDIR=""
		return 1
        fi

#       INFORMIXSERVER=`grep DBSERVERNAME ${INFORMIXDIR}/etc/${ONCONFIG} | nawk
#'{print $2}'`                 

        INFORMIXSERVER="$informixserver"
	sqlhosts_informixserver=`grep "^${informixserver} " ${INFORMIXSQLHOSTS} | head -1 | awk '{print $1}'`

#Maybe a TAB is placed after the informixserver entry in the sqlhosts

	if [ ! "${sqlhosts_informixserver}" ] ; then
		sqlhosts_informixserver=`grep "^${informixserver}	" ${INFORMIXSQLHOSTS} | head -1 | awk '{print $1}'`
	fi 

 
        if [ "${INFORMIXSERVER}" != "${sqlhosts_informixserver}" ] ; then                        
                logerr "4060" "Informixserver $INFORMIXSERVER not found in ${INFORMIXSQLHOSTS}"
                INFORMIXDIR="" 
        fi

        export INFORMIXDIR ONINIT ONCONFIG INFORMIXSERVER

}


make_rpc_call() {
  	logprint "Calling $3 for onconfig $2 on host $1..."
  	ha_dbms_call $1 $2 $3 informix
  	}

#end of common ha-dbms boiler plate
#include_boiler

prog="start_net"
echo "HA_METASETSERVE=\"$1\"" > $HA_VAR/ha_env
echo "HA_NOMETASETSERVE=\"$2\"" >> $HA_VAR/ha_env

# startup_informix - runs Informix startup command for one onconfig
#   uses variables ONCONFIG, oninit, informix_onwer
startup_informix() {
        lognotice "${prog}.2000" "Starting up Informix OnLine Server $INFORMIXSERVER"
#   Start  the Informix OnLine Server pointed by the $ONCONFIG
                su $informix_owner -c $INFORMIXDIR/bin/oninit
                if [ $? -ne 0 ] ; then
                        logerr "${prog}.4001" \
"Informix OnLine Server ${INFORMIXSERVER} NOT started look for the error in the OnLine log file"
                fi
  }

# startup_onconfig - start up onconfig $ONCONFIG
startup_onconfig() {

	export ONCONFIG

	# Create a symbolic link on the logical host which points to the
	# informix home directory on the physical host.

#	INF_DB_FILE=/etc/opt/SUNWhadf/hadf/hainformix_databases
#	logical_host=`grep "$ONCONFIG" $INF_DB_FILE | awk '{print $3}'`
#	if [ "$logical_host" != "" ] ; then
#		ln -s $INFORMIXDIR "/$logical_host/inflink_$ONCONFIG"
#	fi

  	find_informix $ONCONFIG
    if [ "$INFORMIXDIR" = "" ] && [ -n "`disk_mounted $ONCONFIG`" ] ; then
       logerr "${prog}.4010" \
	"INFORMIXDIR is not set in the environment or does not point to a correct directory"
        return
    fi  

    LIB_LINK="iasfs07a.so iasfd07c.so ismdd07a.so \
                  ismdd07b.so iosm07a.so ipldd07a.so"

    # create symbolic links for informix runtime libraries
    for LIB in $LIB_LINK ; do
          if [ -f $INFORMIXDIR/lib/$LIB ]; then
                  if [ ! -f /usr/lib/$LIB ]; then
                       ln -s $INFORMIXDIR/lib/$LIB /usr/lib/$LIB 2> /dev/null
                  fi
          fi
    done

  
    informix_owner=`ls -ld ${INFORMIXDIR}/bin | nawk '{print $3}'`

    INFORMIXSERVER=`grep DBSERVERNAME ${INFORMIXDIR}/etc/${ONCONFIG} | nawk '{print $2}'` ; export INFORMIXSERVER

    ONCONFIG_FILE=${INFORMIXDIR}/etc/${ONCONFIG}

      if [ -f $ONCONFIG_FILE ] ; then
                count=`su $informix_owner -c ${INFORMIXDIR}/bin/onstat -a | wc | nawk '{print $1}'`
                # find out if database is indeed up
                if [ $count -gt 1 ] ; then
                      lognotice "${prog}.2010" "Informix OnLine Server ${INFORMIXSERVER} already running!"
                                return
                fi
          
         	startup_informix &
 
      else
        logerr "${prog}.4020" \
	 "could not find config file ${ONCONFIG_FILE} for Informix OnLine Server ${INFORMIXSERVER}"
        logerr "${prog}.4030" \
	"Informix OnLine Server ${INFORMIXSERVER} NOT started"
      fi
}


# get the list of databases, and then start them up
for ONCONFIG in `informix_status_svcs -mode on -hosts $1` ; do
 	startup_onconfig 
done

