#!/usr/bin/ksh
# Copyright (c) 2000 Sun Microsystems, Inc.  All Rights Reserved.
#
#
# Module Name:
#
#      clcfg.sh
#
# Abstract:
#
# This module is the shell script for setting up highly available
# PC Netlink configurations in a Sun cluster environment.
#
#
# Revision History:
#


#
# Remove all (unwanted) aliases
#
unalias -a


#
# Prevent the callers umask from altering file mode
#
umask 0


PATH=/sbin:/usr/sbin:/usr/bin:/opt/SUNWcluster/bin:/etc/opt/SUNWlznb/sbin:/opt/lanman/bin:/opt/lanman/sbin:/opt/lanman/lib

MasterList=`haget -f mastered`
AdminFSRoot=""

TargetLogicalHost=""
LogicalIP=""
SharedDataDir=""
InitPhyshostFile="initial_master"
LinkFile="directory_links"

Service="pc_netlink"

VARLMDIR=/var/opt/lanman
VARNBDIR=/var/opt/SUNWlznb
ETCLMDIR=/etc/opt/lanman
ETCNBDIR=/etc/opt/SUNWlznb
EXPORTDIR=/export/lanman
NETBIOSCONF=/etc/opt/SUNWlznb/netbios.conf
STARTNETPATH=/opt/lanman/lib/ha/netlink_start_net
STOPNETPATH=/opt/lanman/lib/ha/netlink_stop_net
ABORTNETPATH=/opt/lanman/lib/ha/netlink_abort_net
FMSTARTPATH=/opt/lanman/lib/ha/netlink_fm_start
FMSTOPPATH=/opt/lanman/lib/ha/netlink_fm_stop
FMCHECKPATH=/opt/lanman/lib/ha/netlink_fm_check
GETENT=/usr/bin/getent
CUT=/usr/bin/cut

MAXINST=50
ILIMIT=10

#
# Commonly used function for script message localization
#
pgettext()
{
        # Run gettext with TEXTDOMAINDIR set.
        gttext=gettext  # Trick to fool shxgettext
        TEXTDOMAINDIR=${Install_Script_Dir}/locale ${gttext} ${textdomain} "$1"
}

### textdomain -> Domain that gettext uses to derive the message object name.

    textdomain=`basename $0`



#
# Trap here on signals
#
Sig_Handler()
{
        exit_code=$?

        \exit ${exit_code}
}


Get_AdminFSRoot()
{
    if [ -z "${AdminFSRoot}" ]
    then
	AdminFSRoot=`haget -f pathprefix -h $TargetLogicalHost`
    fi
}



#
# Register_Agents()
#
# Registers netlink service on target logical
# host and specifies location of start/stop
# scripts...returns boolean true/false.
#
Register_Agents()
{

    hareg -r ${Service}$Instance -h $TargetLogicalHost -m START_NET=$STARTNETPATH \
-m STOP_NET=$STOPNETPATH -m ABORT_NET=$ABORTNETPATH -m FM_START=$FMSTARTPATH \
-m FM_STOP=$FMSTOPPATH -m FM_CHECK=$FMCHECKPATH -t STOP_NET=59

    if [ $? -eq 0 ]
    then
        pgettext "HA service $Service has been successfully registered.\n"
        true; return
    fi

    if [ $? -ne 0 ]
    then
        pgettext "HA service $Service failed to register.\n"
        false; return
    fi
}


#
# Start_HA_Netlink()
#
# Starts netlink service on target logical
# host and returns boolean true/false.
#
Start_HA_Netlink()
{

   hareg -y ${Service}${Instance}

   if [ $? -eq 0 ]
   then
        pgettext "HA service ${Service}${Instance} started successfully on $TargetLogicalHost.\n"
        true; return
   fi

   if [ $? -ne 0 ]
   then
        pgettext "HA service $Service failed to start on $TargetLogicalHost.\n"
        false; return
   fi
}

Select_inst() {
    i=1
    while [ $i -le $MAXINST ]
    do
	mask[$i]=1
	i=`expr $i + 1`
    done

    j=1
    while [ $j -le $MAXINST ]
    do
	limit=1
	for k in ${Iarray[$j]}
	do
		# return with error if there is $ILIMIT no. of instances exists already. 
		if [ $limit -ge $ILIMIT ]
		then
			pgettext "Error creating instance -- No more than ${ILIMIT} instances allowed\n"
			return 1
		fi
		limit=`expr $limit + 1`
		mask[$k]=0
	done
	j=`expr $j + 1`
    done
	
    k=1
    while [ $k -lt $MAXINST ]
    do
	if [ mask[$k] -eq 1 ]
	then
		Instance=$k
		break
	fi
	k=`expr $k + 1`
    done
    if [ $Instance = "" ]
    then
	return 1
    else
	return 0
    fi
}


#
# ConfigureNodes()
#
# For each remaining node in the Cluster
# remotely prepare them for HA Netlink by
# shutting down lmx daemons, netbios, and
# relocating their shared data to backup.
#
Configure_Nodes()
{

    i=1
    #
    # get list of all phys. machines in cluster
    #
    ConfiguredMaster=`haget -f master -h $TargetLogicalHost`
    ClusterNodes=`haget -f physical_hosts -h $TargetLogicalHost`
    for node in ${ClusterNodes}
    do

        #
        # for every node that isnt the target logical
        # host master (which has previously been conf.)
        # remotely prepare for HA netlink
        #
        if [ ${node} != ${ConfiguredMaster} ]
        then

            #
            # grab first private link for physical host
            #
            NodePrivateLinks=`haget -f private_links -h $node`
            IpAddress=`echo $NodePrivateLinks | cut -d" " -f1`

            #
            # get the instance list for each physical host
            #
            Iarray[$i]="`rsh $IpAddress /opt/lanman/lib/instget`"
	    i=`expr $i + 1`
	else
	    Iarray[$i]="`/opt/lanman/lib/instget`" 
	    i=`expr $i + 1`
        fi

    done

    Select_inst
    Success=$?

    for node in ${ClusterNodes}
    do
        if [ ${node} != ${ConfiguredMaster} ]
        then

            #
            # grab first private link for physical host
            #
          NodePrivateLinks=`haget -f private_links -h $node`
          IpAddress=`echo $NodePrivateLinks | cut -d" " -f1`

            #
            # get the instance list for each physical host
            #
	 if [ $Success -eq 0 ] 
	 then 
            rsh $IpAddress /opt/lanman/lib/instset -a $Instance $IDIR $TargetLogicalHost
 	 else
            rsh $IpAddress /opt/lanman/lib/instset -c
	 fi
	else
	  if [ $Success -eq 0 ]
	  then
	    /opt/lanman/lib/instset -a $Instance $IDIR $TargetLogicalHost
	  else
	    /opt/lanman/lib/instset -c
	  fi
        fi
    done

    if [ $Success -eq 0 ]
    then
    	instdirs -a $Instance $IDIR cluster
	Success=$?
	if [ $Success -eq 0 ]
	then
		TargetIP="`$GETENT hosts $TargetLogicalHost | $CUT -f1`"
		srvconfig -I $Instance -s "lmxserver,ipaddress=$TargetIP"

		PCNL_INSTANCE=$Instance
		export PCNL_INSTANCE
		export IDIR
		if [ $Runjoindomain != "no" ]
		then
			JoinDomainDoNotRestartServer=yes joindomain
		fi
	fi
    fi
    return $Success
}


#
# main configuration routine
#
ConfigureHA()
{

    # register HA agents for PC Netlink data service
    # with cluster
    if  Register_Agents; [ $? -ne 0 ]
    then
        pgettext "Unable to register PC Netlink service...exiting.\n"
        exit 1
    fi

    sleep 2

    # start HA PC Netlink service
    if  Start_HA_Netlink; [ $? -ne 0 ]
    then
        pgettext "Unable to start PC Netlink service...exiting.\n"
        exit 1
    else
        pgettext "Success.\n"
        exit 0
    fi
}




# boolean_t
Not_Hosting_Master()
{

    if [ -n "${MasterList}" ]
    then
        false; return
    else
        true; return
    fi
}




# boolean_t
Not_Super_User()
{
        if [ ! "$userId" ]
        then
                userId=`/usr/xpg4/bin/id -u 2>/dev/null`;  rval=$?

                if [ $rval -ne 0 -o ! "$userId" ]
                then
                        pgettext "ERROR: Unable to determine user id.\n"
#                        Cleanup
                        exit $rval
                fi
        fi

        test "${userId}" != "0"
}



#main

    trap Sig_Handler INT QUIT TERM

    if Not_Super_User
    then
                pgettext "You must be root to execute this command.\n"
                exit 5
    fi


    if Not_Hosting_Master
    then
                pgettext ""\
#"This command must be run on a machine in the cluster that is currently\n"\
#"mastering a logical host.\n"
                exit 5
    fi

    while [ /bin/true ]
    do
	case $1 in
		-c)
		 	if [ $# -ge 3 ]
			then
			   IDIR=$2
			   TargetLogicalHost=$3
			   Configure_Nodes
			   exit $?
			fi
			exit 1	
			;;
		-r)
			if [ $# -ge 3 ] 
			then
			    Instance=$2
			    TargetLogicalHost=$3
			    ConfigureHA
			fi
			;;
		-a)
		 	if [ $# -ge 3 ]
			then
			   IDIR=$2
			   TargetLogicalHost=$3
			   Configure_Nodes
			   rval=$?
			   if [ $rval -eq 0 ]
			   then 
			        ConfigureHA
			   fi
			   exit $rval
			fi
			;;
		*)
			break
			;;
	esac
    done

