#!/bin/sh
#
# Copyright 2000 Sun Microsystems, Inc. All Rights Reserved
#
# @(#)activate.sh	1.30 00/04/13 Sun Microsystems, Inc.
#

exec 3<&0	# save stdin for later use

PATH="/opt/SUNWicg/SunScreen/bin:/usr/bin:/usr/sbin"
export PATH

ATM_CONFIGFILE=/etc/opt/SUNWconn/atm/atmconfig
SS_BINDIR=/opt/SUNWicg/SunScreen/bin
SS_LIBDIR=/opt/SUNWicg/SunScreen/lib
SS_ADMDIR=/opt/SUNWicg/SunScreen/ssadm
SS_DIR=/opt/SUNWicg/SunScreen
SS_ETCDIR=/etc/opt/SUNWicg/SunScreen
SS_VARDIR=/var/opt/SUNWicg/SunScreen
CONFIGS=$SS_ETCDIR/configs
TEMP=$SS_ETCDIR/.temp
ACTIVE=$SS_ETCDIR/.active
OLD=$SS_ETCDIR/.old
read SCREENNAME <$SS_ETCDIR/name

quiet=false

set_num=31	# SI18N_ADMIN_SCRIPTS
LS()		# localize a string
{
	msg_num=$1; shift
	if [ -x ${SS_LIBDIR}/catgets ]
	then ${SS_LIBDIR}/catgets $set_num $msg_num "$*"
	else echo "$*\c"
	fi
}

not()
{
	"$@" && return 1
	return 0
}

error()
{
#LOCO: the program is exiting due to a fatal error
	echo>&2 "ssadm activate: $1 -- `LS 1 Exiting...`"
	exit 1
}

PUSH_TO_ADMIN_GROUP()
{
    [ -s Slaves ] || return 0

    SSADM_TICKET_FILE=$SS_ETCDIR/.cluster_ticket
    if [ ! -s $SSADM_TICKET_FILE ]
    then (umask 077; ssadm -G >$SSADM_TICKET_FILE)
    fi

    if [ "${LANG:-C}" != "C" ]
    then lang="$LANG"
    else lang=en_US
    fi

    classdir=${SS_DIR}/admin
    package=com.sun.sunscreen.internal.ssadm
    CLASSPATH=$classdir
    export CLASSPATH
    if java ${package}.Multivate -T 30 -U "$user" -l "$LANG" ${1+"$@"}
    then status=$?
    else status=$?
    fi
    mv SlavesStatus Slaves
    return $status
}

PUSH_TO_HA_CLUSTER()
{
	[ -s HA ] || return 0
	grep '^HA.*primary' Screen >/dev/null || return 0
	$SS_LIBDIR/ss_ha_sync_config
	$quiet || echo `LS 6 "Configuration synchronized on HA cluster."`
}

# Returns false if a slave has been added or its address changed.
ADMIN_GROUP_UNCHANGED()
{
	old=$1
	new=$2
	sed -n 's/^\([^ ]* [^ ]*\).*/\1/p' $old > oldSlaves
	sed -n 's/^\([^ ]* [^ ]*\).*/\1/p' $new > newSlaves
	if fgrep -vxf oldSlaves newSlaves >/dev/null
	then return 1
	else return 0
	fi
}

set -e

domain=default
unset policy
unset user
partialfailok=false
local_only=false
verify_only=false
existing_temp=false
gui_messages=false

while getopts klmnqtD:d:U: c
do case $c in
   k) partialfailok=true;;
   l) local_only=true;;
   m) gui_messages=true; gui_flag=-m;;
   n) verify_only=true;;
   q) quiet=true;;
   t) existing_temp=true;;
   D|d) domain=$OPTARG;;
   U) user=$OPTARG;;
   *) echo `LS 7 "Usage: ssadm activate [-kln] policy"`; exit 1;;
   esac
done
[ $OPTIND -eq 1 ] || shift `expr $OPTIND - 1`
policy=$1

: ${user=${SSADM_USER-`/bin/logname || /bin/id`}}

/usr/sbin/sync

verifying=`LS 13 "Verifying"`


if $existing_temp
then
	cd $TEMP
else
	[ ! -d $TEMP ] || /bin/rm -r $TEMP

	if $gui_messages
	then echo "++$SCREENNAME++3++$verifying"
	fi

	/bin/mkdir -m 0770 $TEMP
	/bin/chgrp sys $TEMP
	cd $TEMP

	if [ -n "$policy" ]
	then
		if [ -f "$policy" ]
		then policyfile="$policy"
		elif [ -f "$CONFIGS/$policy" ]
		then policyfile="$CONFIGS/$policy"
		elif [ -f "$CONFIGS/$domain/$policy" ]
		then policyfile="$CONFIGS/$domain/$policy"
		     if find "$CONFIGS/$domain/Registry" "$CONFIGS/$domain/.ezdb" -prune -newer "$policyfile" -print 2>/dev/null | grep . >/dev/null
		     then $SS_ADMDIR/edit -d "$domain" -u "$policy" >/dev/null
		     fi
		else msg=`LS 8 "No such policy found:"`
		     error "$msg $policy."
		fi

		registryfile=`sed -n 's/^LOAD *"*\([^"]*\)"*$/\1/p' "$policyfile"`
		if [ -n "$registryfile" ] && [ ! -r "$registryfile" ]
		then registryfile=`dirname "$policyfile"`/"$registryfile"
		fi
		if [ -n "$registryfile" ]
		then $SS_LIBDIR/db_gather -d $domain >.dbstuff
		     registryfile="$registryfile .dbstuff"
		fi

		cat $registryfile "$policyfile" | grep -v '^LOAD ' >Configuration

		origin="$SCREENNAME $domain $policy"
		case $policy in
		*.*)	: ;;
		*)	version=`sed -n 's/^POLICY_VERSION *//p' "$policyfile"`
			if [ -n "$version" ]
			then origin="$origin.$version"
			fi;;
		esac
		echo "@origin $origin" >>Configuration
	else
		[ ! -t 0 ] && [ "${SSADM_ISATTY:-0}" -le 0 ] || $quiet ||
			echo>&2 `LS 9 "ssadm activate: reading configuration from standard input..."`
		cat >Configuration
		origin="`sed -n 's/^@origin //p' Configuration`"
	fi

	echo $origin >active
	echo $user >>active

	if [ -f /kernel/drv/skip_key -a ! -c /dev/skip_key ]
	then	modload -p drv/skip_key >/dev/null
		sh /etc/init.d/skipkey start
	fi

	$SS_LIBDIR/ss_compiler <Configuration
	if $gui_messages && not $local_only
	then sl=
	     exec <Slaves
	     while read name address rest
	     do sl="$sl\"$name\" "
	     done
	     echo "++$SCREENNAME++6++$sl"
	fi
	$SS_LIBDIR/natcompiler
	find . -name '*.data' -print | while read f
	do /usr/ccs/lib/cpp $f | $SS_LIBDIR/datacompiler $f
	done
	find . -name '*.pfl' -print | while read f
	do $SS_LIBDIR/icgc +q -o $f.out $f
	done
	$SS_LIBDIR/db_scatter <Configuration

	if $local_only || $verify_only || [ ! -s Slaves ] ||
	   ADMIN_GROUP_UNCHANGED $ACTIVE/Slaves Slaves
	then :
	else	# hack to handle slave's first activate
	     ${SS_LIBDIR}/ss_active_config -d . activate
	fi

	if $gui_messages
	then echo "++$SCREENNAME++4++"
	fi

	if not $local_only && [ -s Slaves ]
	then if not PUSH_TO_ADMIN_GROUP -nq $gui_flag
	     then if $gui_messages && not $verify_only
		  then sleep 1
		       echo "++++7++"
		       read response <&3
		       case $response in
		       1) :;;
		       *) exit 1;;
		       esac
		  else $partialfailok || exit 1
		  fi
	     fi
	fi
fi

if $verify_only
then
	$quiet || echo `LS 10 "Configuration verified successfully (not activated)."`
	exit 0
fi

$local_only || PUSH_TO_ADMIN_GROUP -t $gui_flag || exit

PUSH_TO_HA_CLUSTER

if [ -s $SS_VARDIR/boottime ]
then
	if ${SS_LIBDIR}/ss_activate $TEMP $ACTIVE
	then
		#
		# copy temp config to .active
		#
		/usr/sbin/sync
		if [ -d $ACTIVE ]
		then rm -rf $OLD
		     mv $ACTIVE $OLD
		fi
		mv $TEMP $ACTIVE
		/usr/sbin/sync

		#
		# restart proxies and ha
		/etc/init.d/proxy restart >/dev/null 2>&1
		[ ! -s HA ] || $SS_LIBDIR/ss_ha_restart >/dev/null 2>&1
	else
		cd /
		rm -r $TEMP
		exit 1
	fi
else
	${SS_LIBDIR}/ss_boot $TEMP $ACTIVE
fi

if [ -r "$ATM_CONFIGFILE" ]
then
        ${SS_LIBDIR}/cleanupatm
fi

if $gui_messages
then echo "++$SCREENNAME++0++"
fi

$SS_LIBDIR/logmsg -A activate -F NOTICE -P activate "reason:utf8=\"activated $origin\"; user:utf8=\"$user\""
$quiet || echo `LS 11 "Configuration activated successfully on"` $SCREENNAME.
