#!/bin/ksh
#ident @(#)imha_start_net	1.7 99/03/15 SMI
# Copyright 1997, Sun Microsystems, Inc.
# All Rights Reserved

# START_NET script for SIMS 4.0 HA interface.  Brings SIMS up.
# Called by the HA framework with three arguments:
# $1: Comma separated list of logical hosts this physical host masters,
#     or null if service is off
# $2: Comma separated list of logical hosts this physical host is not
#     to master, or all logical hosts if this service is off.
# $3: Timeout allowed for stop, in seconds.

typeset MASTERED_HOSTS="$1"
typeset NOT_MASTERED_HOSTS="$2"
typeset TIMEOUT="$3"

prog_path=${0%/*}


. $prog_path/imha_common

getpids() {
    /bin/ps -ef | \
    /bin/grep "$1" | \
    /bin/grep -v /bin/grep | \
    /bin/awk '{print $2}'
}

# If not mastered, do not try to start anything

if [[ ! -f $SIMSbase/etc/opt/SUNWmail/sims.cnf ]]; then
  exit 0
fi

# Get the logical host name SIMS runs on, stripped of any DNS qualifiers.

loghost=$(/bin/grep '^logicalHostname=' $SIMSbase/etc/opt/SUNWmail/sims.cnf)
loghost=${loghost##*=}
loghost=${loghost%%.*}

# If our logical host is not one of those that this physical host masters,
# do nothing.

oldIFS="$IFS"
IFS=","

is_right_host=no
for h in $MASTERED_HOSTS
do
	if [[ "$loghost" = "$h" ]]
	then
		is_right_host=yes
	fi
done

if [[ "$is_right_host" = "no" ]]
then
        loglevel debug $(printf $(gettext 'Since %s is not in \"%s\" nothing happens here') "$loghost" "$MASTERED_HOSTS")
	loglevel debug $(gettext "start_net terminating with nothing to do")
	
	exit 0
fi

# Since the web server may have other uses, it may have been started
# via an /etc/rc.?d script.  We need to make sure the web server restarts
# after the network reconfiguration is done, and the logical disks are
# mounted.  So we stop the web server; this is harmless if it's not
# running.  im.server will restart the web server if it's not running.

if [[ -f /etc/init.d/httpd ]]
then
	/etc/init.d/httpd stop
	
	# httpd may not have exited when /etc/init.d/httpd stop
	# exits.  Wait until it does.  Otherwise im.server will
	# think that it's already running and not restart it,
	# resulting in no web server running.

	while [ -f /var/http/tmp/httpd.pid ]
	do
		_pid=`/bin/head -n 1 /var/http/tmp/httpd.pid`
		if /bin/ps -p $_pid 2> /dev/null 1> /dev/null
		then
			sleep 1 # give it time to exit
		else
			break
		fi
	done
fi

# Since "im.server start" is idempotent in itself, we wouldn't need to do
# any special idempotency checking.  However, SIMS startup time will
# probably exceed the timeout requirements if a imcheck -c
# is needed; in that case we run part2 of the start script in the background
# and exit from this script so the framework won't get excited and decide
# switchover has failed.  We shouldn't run imcheck -t, to see if the
# message store is corrupt, if imaccessd is already running.

loglevel notice $(gettext 'HA framework requested SIMS start.')

# check to see if imaccessd is running already.  If so, imcheck -c is never
# needed.  If imaccessd is NOT running, imcheck -t will tell us if
# imcheck -c is necessary.

SCHEDULER=imaccessd

count=`ps -ef|/bin/grep ${SCHEDULER} | /bin/grep -v grep | /bin/grep -c ${SCHEDULER}`

if ((count > 1))
then
	imcheck_needed=no
elif /opt/SUNWmail/sbin/imcheck -t
then    
	imcheck_needed=no
else
	loglevel notice $(gettext 'SIMS restart delayed for imcheck')
	imcheck_needed=yes
fi

# We need to start the SDS process first because other processes such as SPM
# relying on SDS to be up.  This may take a long time but let it be..
# 
if [[ -f /etc/init.d/dsserv && "$(getpids lib/dsserv)" = "" ]]
then
	/etc/init.d/dsserv start &
        while [[ -z "$(getpids lib/dsserv)" ]]
	do
		sleep 1  # wait for it to start
	loglevel notice $(gettext 'waiting for SDS to come up...')
	done
fi

# Kill sendmail if it's running

if [[ ! -z $(getpids sendmail) ]]; then
	/usr/bin/kill $(getpids sendmail)
fi

# Now re-link sendmail

if [[ -f /opt/SUNWmail/imta/bin/sendmail ]]; then
  if [[ -L /usr/lib/sendmail ]]; then
    /bin/rm -rf /usr/lib/sendmail
    /bin/ln -s /opt/SUNWmail/imta/bin/sendmail /usr/lib/sendmail
  fi
else
  loglevel notice $(gettext 'Missing /opt/SUNWmail/imta/bin/sendmail...')
fi

# Now rebuild imta cache

if /usr/bin/ls /var/opt/SUNWmail/imta/queue_cache/*; then
   /bin/rm /var/opt/SUNWmail/imta/queue_cache/*
  /opt/SUNWmail/imta/sbin/imta cache -sync &
fi

if [[ "$imcheck_needed" = "yes" ]]
then
	# startup will be slow, run part 2 in background.
	loglevel notice $(gettext 'SIMS startup will continue automatically after the delay')
	imha_start_net_part2 "$imcheck_needed" &
else
	imha_start_net_part2 "$imcheck_needed"
fi
	
exit 0

# Local variables:
# mode:ksh
# ksh-indent:8
# ksh-group-offset:-8
# End:
