#! /bin/sh

# ident "@(#)smond_conf.sh 1.4	01/03/27 SMI"

SMOND_CONF_FILE=/var/opt/SUNWcluster/smond.conf
LOCKFILE=/var/opt/SUNWcluster/run/.smond.lock

pname=$0
args=$*
numargs=$#
hostnames=
snmphost=
UID=`id|sed -e "s/uid=\([0-9][0-9]*\)..*/\1/"`
linecount=0
diff=0

usage()
{
   echo "Usage:"
   echo "      smond_conf -h <clustname/hostname> <clustname/hostname> ..."
   echo
   echo "  where <clustname/hostname> is the name of the cluster/host"
   echo "  to gather mond packets from"

   return 0
}

write_header()
{
  file=$1

  rm -f $file

  echo "#" > $file
  echo "# smond.conf - Configuration file for smond" >> $file
  echo "#" >> $file
  echo "# Lines starting with a '#' are ignored as comment lines." >> $file
  echo "# Specify the list of hostnames and/or cluster names separated" >> $file
  echo "# by white space (regular space or tabs), after the key" >> $file
  echo "# "targetHosts", followed by the character '='. (There can" >> $file
  echo "# be white space in between the key and the '=' character.)" >> $file
  echo "# Entries can be extended to a next line using the '\' character" \
		>> $file
  echo "# followed by newline ("'\\n'"), at the end of each line." >> $file
  echo "#" >> $file
}

sig_handler ()
{
  echo "Caught signal $1 - restoring old configuration"

  if [ -f /var/opt/SUNWcluster/run/$$.smond.conf ]; then
	rm -f $SMOND_CONF_FILE
	mv -f /var/opt/SUNWcluster/run/$$.smond.conf $SMOND_CONF_FILE
  fi

  rm -f $LOCKFILE 

  exit 1
}

lock()
{
  if [ -f $LOCKFILE ]; then
	echo "lock file - $LOCKFILE already present !"
	echo "Another user already trying to configure smond. Exiting..."
	exit 1
  fi

  /bin/ln -s CLUSTERHOME/smond $LOCKFILE

  return 0
}

save_config()
{
  if [ -f $SMOND_CONF_FILE ]; then
    cp -f $SMOND_CONF_FILE /var/opt/SUNWcluster/run/$$.smond.conf
  fi

  return 0
}

parse_cmd_line()
{

	while [ "$#" -ne 0 ]
	do
		case "$1" in 
			-h)
				shift
        			while [ $# -ne 0 ]
        			do
               			 case $1 in
              			  -?)     break;;

                        	   *)     numchars=`echo $hostnames | wc -c`
					  diff=`expr $numchars - $linecount`
					  if [ $diff -gt 50 ]; then
						linecount=`expr $linecount + $diff`
						hostnames="$hostnames \\ \\n"
					  fi
					  hostnames="$hostnames $1"
                                	  shift;;
                		  esac
        			done

				# echo "hostnames = $hostnames"
				;;
                        -t)
                                shift 
                                while [ $# -ne 0 ]
                                do
                                 case $1 in
                                  -?)     break;;
 
                                   *)     snmphost="$snmphost $1"
                                          shift;;
                                  esac
                                done
				# echo "snmphost = $snmphost"
				;;
			*)
				echo "Incorrect arguments"
				usage
				exit 1;;
		esac
	done
}

if [ $UID -ne 0 ]; then
	echo "You must be super-user to run this program."
	exit 1
fi

trap 'sig_handler' 1 2 3 15

if [ "$#" -eq 0 ]; then
	usage
	exit 1
fi

parse_cmd_line $args

if [ -z "$hostnames" ]; then
	echo "No hostnames or cluster names specified !"
	echo
	usage
	exit 1
fi

lock
save_config

write_header $SMOND_CONF_FILE
echo "targetHosts = $hostnames" >> $SMOND_CONF_FILE


#
# link smond into rc2.d & rc3.d
#
if [ ! -f /etc/rc2.d/K95smond ]; then
        /bin/rm -f /etc/rc2.d/K95smond
	#echo "Linking CLUSTERHOME/smond_ctl to /etc/rc2.d/K95smond"
        /bin/ln -s CLUSTERHOME/smond_ctl /etc/rc2.d/K95smond
fi

if [ ! -f /etc/rc3.d/S95smond ]; then
        /bin/rm -f /etc/rc3.d/S95smond
	#echo "Linking CLUSTERHOME/smond_ctl to /etc/rc3.d/S95smond"
        /bin/ln -s CLUSTERHOME/smond_ctl /etc/rc3.d/S95smond
fi


#echo "Finished configuring $SMOND_CONF_FILE"
echo "Finished configuring ..."

rm -f $LOCKFILE
rm -f /var/opt/SUNWcluster/run/$$.smond.conf

echo "Run CLUSTERHOME/smond_ctl { start | stop } to initiate smond"
exit 0
