#!/bin/sh
#
#ident "$Header: /net/projects/S2.6/cvs/os_net/src_ws/usr/src/tadcmd/locations/chloc.sh,v 1.4 1997/12/05 12:40:03 ang Exp $"
#
# Change from on location to another.
#
# Rely on /etc/location to determine the current location setting,
# if there is none set do not do the shutdown section. If -U is specified
# shutdown to no services.
#
LOCROOT=${LOCROOT:-/var/locations}
PATH=/bin:/usr/bin:/usr/sbin:/usr/ucb
SH=${SH:-/bin/sh}
export LOCROOT SH PATH
pNAME=`basename $0`
FORCE=0
UFLAG=0

usage() {
    echo "Usage: $pNAME [-f] <new location>|- [<location sub-level>]" 1>&2
    echo "       $pNAME -U" 1>&2
    exit 2
}

shutdown () {
    Done=
    for f in $1/K*
    {
        if [ -s ${f} ]
        then
    	${SH} ${f} stop
    
    	# If a script fails go no further..
    	# Re-start all previously stopped scripts
    	if [ $? -ne 0 ]
    	then
    	    echo "\"$f stop\" script failed.." 1>&2
    	    if [ -n "$Done" ]
    	    then
    		echo "Backing out of location change.." 1>&2
    		${SH} ${f} start
    		for rs in $Done
    		{
    		    ${SH} ${rs} start
    		}
    	    fi
    	    return 1
    	fi
    	Done="$f $Done"
        fi
    }
    return 0
}

startup () {
    for f in $1/S*
    {
	if [ -s ${f} ]
	then
	    ${SH} ${f} start
	fi
    }
}

USER=`id | cut -f1 "-d " | sed -e "s/.*(//" -e "s/).*//"`
if [ "$USER" != "root" ]
then
    echo "You need to be root to run $pNAME" 1>&2
    exit 1
fi

if [ ! -f /etc/location ]
then
    echo "There is no \"/etc/location\" file - has the system been initialized"
    echo "for locations? See the initloc(1m) man page."
    exit 1
fi

while getopts fU c
do
    case $c in
    f)
	FORCE=1
	;;
    U)
	UFLAG=1
	FORCE=1
	;;
    \?)
	usage
    esac
done
shift `expr $OPTIND - 1`

NLOC=$1
NLOCLEV=$2
if [ -s /etc/location ]
then
    oIFS="$IFS"
    IFS=":"
    set `cat /etc/location`
    CLOC=$1
    CLOCLEV=$2
    IFS="$oIFS"
else
    CLOC=
    CLOCLEV=
fi

if [ $UFLAG = 0 ]
then
    if [ -z "$NLOC" ]
    then
	usage
    fi
    if [ "$NLOC" = "-" ]
    then
	NLOC=$CLOC
    fi
    if [ ! -d $LOCROOT/$NLOC -o ! -d $LOCROOT/$NLOC/rc${NLOCLEV}.d ]
    then
	echo "No such location \"$NLOC ${NLOCLEV}\"" 1>&2
	exit 3
    fi

    if [ "$CLOC" = "$NLOC" -a "$CLOCLEV" = "$NLOCLEV" ]
    then
	if [ $FORCE -eq 0 ]
	then
	    echo "Location \"$NLOC $NLOCLEV\" already set." 1>&2
	    exit 0
	elif [ "$CLOC" = "$NLOC" -a -n "$NLOCLEV" ]
	then
	    FORCE=0
	fi
    fi

    # Do some sanity checking for compatibility between the current
    # and destination locations.
    #   i) Check for an entry in /etc/hosts for any interface hostnames
    #      in the destination location.
    if [ "$NLOC" != "$CLOC" ]
    then
    	HSF=`ls  $LOCROOT/$NLOC/databases/etc/hostname.??*[0-9] 2>/dev/null`
    	if [ -n "$HSF" ]
    	then
	    for F in $HSF
	    do
		N=`cat $F`
		if grep `cat $F` /etc/hosts >/dev/null
		then
		    :
		else
		    echo "No IP address for \"$N\" in /etc/hosts" 1>&2
		    echo "(From $F)"
		    exit 1
		fi
	    done
    	fi
    fi
fi

if [ ! -z "$CLOC" ]
then
    # Set the exported location for the scripts.
    LOCATION=$CLOC
    LOCDB=$LOCROOT/$LOCATION/databases
    export LOCATION LOCDB
    
    # Shutdown everything in the current location.
    if [ -n "$LOCATION" -a -d $LOCROOT/$LOCATION/rc.d ]
    then
	if [ -n "$CLOCLEV" ]
	then
	    if shutdown $LOCROOT/$LOCATION/rc${CLOCLEV}.d
	    then
		:
	    else
		exit 1
	    fi
	fi
	if [ "$CLOC" != "$NLOC" -o $FORCE -eq 1 ]
	then
	    if shutdown $LOCROOT/$LOCATION/rc.d
	    then
		:
	    else
		exit 1
	    fi
	fi
    fi
    
    . >/etc/location
fi

if [ $UFLAG -eq 1 ]
then
    exit 0
fi

# Change the exported LOCATION to the new one.
LOCATION=$NLOC
LOCDB=$LOCROOT/$LOCATION/databases
export LOCATION LOCDB

# Startup things in the new location
if [ -d $LOCROOT/$LOCATION/rc.d ]
then
    if [ "$CLOC" != "$NLOC" -o $FORCE -eq 1 ]
    then
	startup $LOCROOT/$LOCATION/rc.d
    fi
    if [ -n "$NLOCLEV" ]
    then
	startup $LOCROOT/$LOCATION/rc${NLOCLEV}.d
    fi
fi

echo "${NLOC}:${NLOCLEV}" >/etc/location
