#!/bin/sh
#
#ident "$Header: /net/projects/S2.6/cvs/os_net/src_ws/usr/src/tadcmd/locations/initloc.sh,v 1.4 1998/07/29 09:27:50 ang Exp $"
#
LOCROOT=${LOCROOT:-/var/locations}
export LOCROOT
pNAME=`basename $0`

usage() {
    echo "Usage: $pNAME [-v] [ locations.. ]" 1>&2
    exit 2
}

#
# get_ans(): gets an asnwer from the user.
#		$1  instruction/comment/description/question
#		$2  default value
#
get_ans() {
	if [ -z "$2" ]
	then
		echo "$1 \c"
	else
		echo "$1 [$2] \c"
	fi
	read ANS
	if [ -z "$ANS" ]
	then
		ANS=$2
	fi
}

#
# get_yesno(): get the yes or no answer.
#		$1  message type  or message.
#
#
#
get_yesno() {
    ANS="X"
    RVAL=1

    while true
    do
	get_ans "$1" ""
	case $ANS in
	Yes|YES|yes|y|Y)
	    RVAL=0
	    break
	    ;;
	No|NO|no|n|N)
	    break
	    ;;
	esac
    done
    return $RVAL
}

do_ptz() {
    eval `echo "Sys\c"; grep "^TZ=" /etc/default/init`
    if [ "$SysTZ" != "PORTABLE_TZ" ]
    then
	cat <<EOF

    The default timezone on the machine is currently set to "$SysTZ"
    but does not use portable time, see libptz(4).

EOF
	if get_yesno "        Would you like the system to use portable time?"
	then
	    sed -e "/^TZ=/s;$SysTZ;PORTABLE_TZ;" /etc/default/init >/tmp/init$$
	    cp /tmp/init$$ /etc/default/init
	    rm -f /tmp/init$$
	    echo "$SysTZ" >/usr/share/lib/zoneinfo/portabletime
	    MT=`uname -m`
	    case $MT in
	    sun4m)
		if [ -f /usr/platform/sun4m/lib/libptz.so.1 ]
		then
		    if [ ! -f /usr/platform/sun4m/lib/libc_psr.so.1 ]
		    then
			ln -s libptz.so.1 /usr/platform/sun4m/lib/libc_psr.so.1
		    fi
		fi
		;;
	    sun4u)
		if [ -f /usr/platform/sun4u/lib/libptz.so.1 ]
		then
		    if [ ! -f /usr/platform/sun4u/lib/origlibc_psr.so.1 ]
		    then
			mv /usr/platform/sun4u/lib/libc_psr.so.1 /usr/platform/sun4u/lib/origlibc_psr.so.1
			ln -s libptz.so.1 /usr/platform/sun4u/lib/libc_psr.so.1
		    fi
		fi
		;;
	    esac
	fi
    fi
}

do_saveloc() {
    echo "\n    Saving current machine setup as location \"Original\".."
    echo >/etc/location
    saveloc -m "Original System Configuration" Original
}

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

while getopts n:h: c
do
    case $c in
    n)
	NodeName=$OPTARG
	;;
    h)
	HostName=$OPTARG
	;;
    \?)
	usage
    esac
done
shift `expr $OPTIND - 1`
NARGS=$#

if [ -f /etc/location ]
then
    LOC=`cat /etc/location`
    cat <<EOF
    It would appear that locations has already been initialized.
    (/etc/location exists and location is "$LOC").

EOF
    echo "Exiting"
    exit 1
fi

cat <<EOF
    This is an interactive program that initializes your system to enable
    correct operation of the locations(1) suite of programs. This involves
    separating the machine name (nodename) from the name by which the system
    is known on the network interfaces. It is required to allow CDE and
    other continuous system services such as license servers to continue
    un-interrupted during network reconfiguration.

EOF

NodeName=`uname -n`
EtcNodeName=`cat /etc/nodename`
if [ -z "$NodeName" -o -z "$EtcNodeName" -o "$NodeName" != "$EtcNodeName" ]
then
    echo "    The system name is not consistent:"
    echo "      uname reports \"$NodeName\""
    echo "      /etc/nodename set to \"$EtcNodeName\""
    echo "Exiting"
    exit 1
fi


for F in /etc/hostname.*
do
    if [ ! -f "$F" ]
    then
	continue
    fi
    HostName=`cat $F`
    InterFace=`echo $F | sed -e "s/.*\.//"`
    if [ "$HostName" = "$NodeName" ]
    then
	cat <<EOF
    Network interface "$InterFace" is configured with the same name
    as the system. In order to enable locations one of them must
    be changed. If there are software licenses tied to the system
    name it is usually better to change the network interface name.
    Other machines may need to be informed that one name is an alias
    of the other.

EOF
	if get_yesno "          Do you want to change the network name?"
	then
	    get_ans "          New interface name? " "${HostName}-n"
	    NHostName=$ANS
	    sed -e "s/loghost//" /etc/hosts | sed -e "s/$NodeName/$NHostName/" | sed -e "s/localhost/$NodeName localhost loghost/" >/tmp/hosts$$
	    cp /tmp/hosts$$ /etc/hosts
	    rm -f /tmp/hosts$$
	    echo $NHostName >/etc/hostname.$InterFace
	elif get_yesno "          Do you want to change the system name?"
	then
	    get_ans "          New system name? " "${HostName}-s"
	    NNodeName=$ANS
	    sed -e "s/loghost//" /etc/hosts | sed -e "s/localhost/$NNodeName localhost loghost/"  >/tmp/hosts$$
	    cp /tmp/hosts$$ /etc/hosts
	    rm -f /tmp/hosts$$
	    for NF in /etc/net/*/hosts
	    do
		sed -e "s/$HostName/$NNodeName/g" $NF >/tmp/nhosts$$
		cp /tmp/nhosts$$ $NF
		rm -f /tmp/nhosts$$
	    done
	    echo $NNodeName >/etc/nodename
	else
	    echo "No other alternative!"
	    exit 1
	fi
	do_ptz
	do_saveloc

	cat <<EOF
    You must now reboot the system for any modifications to become effective.

EOF
	exit 0
    fi
done

cat <<EOF
    It would seem that none of the network interfaces have the same name
    as the machine system name. Locations should be able to function without
    hinder in this situation.

    Your network system setup has not been modified.

EOF

do_ptz
do_saveloc

cat <<EOF
    You must now reboot the system for any modifications to become effective.

EOF
exit 0
