#!/bin/sh
#
# @(#)pdbconf.sh 1.9 95/04/04 SMI
#
#	Copyright (c) 1994 Sun Microsystems, Inc.
#
# pdbconf - SPARCcluster PDB configuration editor script.
#
Myname=`basename $0`

PATH="/opt/SUNWcluster/bin:/usr/5bin:/usr/bin/X11:/usr/local/bin:\
/usr/bsd:/usr/ucb:$PATH"
export PATH

cdbpath="/etc/opt/SUNWcluster/conf"

tmppath=${Tmppath=/usr/tmp}
sedfile=${tmpfile=${tmppath}/sed.tmp$$}
touch $sedfile 
trap "echo \"$Myname: ***interrupt***\" 1>&2; /bin/rm -rf $sedfile; exit 1"\
	      1 2 3 15

get_hostslot()
{
# If host is set then we want to query the $CLUSTNAME.cdb file and get
# the slot location for this host.
if [ -n "$host" ]; then
 for i in 0 1
 do
  if cdbmatch cluster.node.$i.hostname $cdbfile | /bin/grep $host >/dev/null 2>&1
  then
    hostslot=$i
    echo 2>&1 "Host $host is node number $hostslot."
    break;
  fi
 done
fi
}

get_newhost()
{
if [ -n "$newhost" ]; then
 set `echo $newhost | sed -e "s/,/ /g"`
 echo "/cluster.node.0.hostname/s/\(.*\):.*$/\1: $1/" >>$sedfile
 echo "/cluster.node.1.hostname/s/\(.*\):.*$/\1: $2/" >>$sedfile
fi
}

validate_interfaces()
{
 if [ $# -gt 2 ]; then
   if [ "$2" = "$4" ]; then
     echo 2>&1 "$Myname: Interfaces specified must be unique!"
     exit 3
   fi
 fi
}

validate_controllers()
{
 for i in $*
 do
  if `echo $i | /bin/grep -v '^[0-9A-Fa-f]\{12\}' >/dev/null 2>&1`
  then
    echo 2>&1 "$Myname: Possible illegal serial number: $i"
  fi
 done
 return 0
}

get_controllers()
{
if [ $# -gt 0 ]; then
 newcontrollers=`echo $* | sed -e "s/,/ /g"`
 set $newcontrollers
 if [ -n "$newcontrollers" ]; then
  validate_controllers $newcontrollers
  qctl=`cdbmatch ctlreserve.node.quorumctl $cdbfile 2>&1| cut -d" " -f1`
  echo "/ctlreserve.node.quorumctl/s/\(.*\):.*$/\1: $1/" >>$sedfile
  echo "/ctlreserve.node.ctllist/s/\(.*\):.*$/\1: $newcontrollers/"\
	 >> $sedfile
 fi
fi
}


get_interfaces()
{
newinterfaces=$*
if [ -n "$newinterfaces" ]; then
 echo "newinterfaces = $newinterfaces"
 validate_interfaces $newinterfaces
 set $newinterfaces
 while [ $# -gt 0 ]
 do
  cif=`cdbmatch cluster.node.${hostslot}.if.$1 $cdbfile 2>&1| cut -d" " -f1`
  echo "/cluster.node.${hostslot}.if.$1/s/$cif/$2/" >> $sedfile
  echo "/cluster.node.${hostslot}.haiface.$1/s/$cif/$2/">> $sedfile
  shift 2
 done
fi
}

Sanity_checks()
{
if [ -n "$newinterfaces" -a -z "$host" ]; then
 echo 2>&1 "$Myname: New interfaces specified without a host!"
 exit 2
elif [ -n "$host" -a \( -z "$newinterfaces" -a -z "$newcontrollers" \) ]; then
 echo 2>&1 "$Myname: Host specified without new interfaces!"
 exit 2
elif [ -z "$newinterfaces" -a -z "$newhost" -a -z "$newcontrollers" ]; then
 echo 2>&1 "$Myname: Nothing to set."
 exit 0
fi
}

get_cluster_name()
{
if [ -z "$CLUSTNAME" ];then
 echo 2>&1 "$Myname: No cluster name specified!"
 exit 1
else
 cdbfile=$cdbpath/$CLUSTNAME.cdb
# Ok now lets see if this is legal.
 if cdbmatch cluster.cluster_name $cdbfile 2>/dev/null \
  | /bin/grep $CLUSTNAME >/dev/null 2>&1
 then
  :
 else
   echo 2>&1 "$Myname: No Such or Illegal cluster name: $CLUSTNAME!"
   exit 2
 fi
fi
}

display_config()
{
 get_cluster_name
 host0=`cdbmatch cluster.node.0.hostname $cdbfile`
 host1=`cdbmatch cluster.node.1.hostname $cdbfile`
 h0if0=`cdbmatch cluster.node.0.if.0 $cdbfile | cut -d" " -f1`
 h0if1=`cdbmatch cluster.node.0.if.1 $cdbfile | cut -d" " -f1`
 h1if0=`cdbmatch cluster.node.1.if.0 $cdbfile | cut -d" " -f1`
 h1if1=`cdbmatch cluster.node.1.if.1 $cdbfile | cut -d" " -f1`
 h0ctl0=`cdbmatch ctlreserve.node.ctllist $cdbfile`
 h0qrm0=`cdbmatch ctlreserve.node.quorumctl $cdbfile`
 echo 2>&1 "
Current Configuration for Cluster $CLUSTNAME:

  Hosts in cluster: $host0 $host1

  Private Network Interfaces for 
      $host0: $h0if0 $h0if1
      $host1: $h1if0 $h1if1
  
  SSA Controllers for 
      $host0: $h0ctl0
      $host1: $h0ctl0

  Quorum Controller:
      $h0qrm0
"
}

usage()
{
 echo 2>&1 "Usage: 

$Myname clustername -h <new hostname1> <new hostname2>

$Myname clustername -i <hostname> <if0> <if1>

$Myname clustername -c <hostname> <ct0> <ct1> ...

$Myname clustername -p

$Myname clustername -u

$Myname clustername -v
"
exit 0
}

   if [ $# -ge 2 ]; then
     CLUSTNAME=$1
     shift;
   else
     usage
     exit 1
   fi
   while getopts puvf:c:i:h: c
   do
	case $c in
	 u|v) echo 2>&1 "$Myname: Option not implemented yet."
	      exit 0;;
	   p) shift
	      display_config
	      exit 0
	      ;;
	   f) CLUSTNAME=$OPTARG
	      shift 2; OPTIND=1;;	# move -f <name> out of here!
	   h) shift; 		# get rid of the -h parameter!
	      if [ $# -ne 2 ]; then
		echo 2>&1 "$Myname: Illegal Hostname format. \
Should be:\n$Myname clustername -h newhost1 newhost2"
	        exit 2
	      else
		newhost=$1,$2
		shift 2
	      fi
	      ;;
	   i) shift
	      if [ $# -ne 3 ]; then
		echo 2>&1 "$Myname: Illegal interface format. \
Should be:\n$Myname clustername -i hostname ifX ifY"
		exit 2
	      else
		host=$1
		newinterfaces="0 "$2" 1 "$3
		shift 3
	      fi
		;;
	   c) shift
	      if [ $# -lt 3 ]; then
		echo 2>&1 "$Myname: Illegal controller format. \
Should be:\n$Myname clustername -c hostname ctlX ctlY ..."
		exit 2
	      else
		host=$1
		newcontrollers=$2
		shift 2; for i in $* ; do
		  newcontrollers="${newcontrollers} $i"
		done
	      fi
		;;
	  *) echo $*
	esac
   done
#fi

CLUSTNAME=${CLUSTNAME:=ops}

Sanity_checks

get_cluster_name

get_hostslot

get_newhost 

get_interfaces $newinterfaces

get_controllers $newcontrollers

cp $cdbfile ${cdbfile}.o
sed -f $sedfile ${cdbfile}.o > $cdbfile






