#!/bin/ksh
#
# Copyright (c) 2001 Sun Microsystems, Inc.
# All rights reserved.
#
#ident   "@(#)scm 1.55     01/06/25 SMI"
#
#######################################################################
#
#   This file contains system setup requirements for scm.
#   It should be located in /etc/init.d directory with the following links:
#
#       ln /etc/init.d/scm /etc/rc0.d/K84scm
#       ln /etc/init.d/scm /etc/rc2.d/S002scm
#
#USAGE="Usage: $0 { start | stop }
#
#######################################################################

set_system_type()
{
	if [ "`/usr/bin/isalist | grep -cw 'sparcv9'`" -ne 0 ]
	then
		PKG=/usr/kernel/drv/sparcv9
		ISS=2
	else
		PKG=/usr/kernel/drv
		ISS=1
	fi

	if [ -x /usr/sbin/clinfo ]
	then
		CLINFO=/usr/sbin/clinfo
	else
		CLINFO=/bin/false
	fi

	SCMBIN=/usr/opt/SUNWesm/SUNWscm/sbin
	SCMLIB=/usr/opt/SUNWscm/lib
	SYS=$ISS
}


do_stopsdbc ()
{
    if [ ! -r /dev/sdbc ]
    then
	return
    fi

    ${SCMBIN}/scmadm -d
    if [ $? -ne 0 ] ; then
	# If the disable failed that means we have pinned data.
	echo Cache Not Deconfigured
    fi
}

do_stopnskernd ()
{
  ps -e | grep nskernd > /dev/null 2>&1
  if [ $? -ne 0 ] ; then
    # only stop nskernd if sdbc is stopped
    pid=`ps -e | grep -w nskernd | sed -e 's/^  *//' -e 's/ .*//'`
    if [ -n "$pid" ] ; then
      kill -15 $pid
    fi
  fi
}

do_stopdscfglockd ()
{
  pid=`ps -e | grep -w dscfglockd | sed -e 's/^  *//' -e 's/ .*//'`
  if [ -n "$pid" ] ; then
    kill -15 $pid
  fi
}

do_stop ()
{
  do_stopsdbc
  do_stopnskernd
}

do_nskernd ()
{
  ps -e | grep -w nskernd > /dev/null 2>&1
  if [ $? -ne 0 ] ; then
    ${SCMLIB}/nskernd
    if [ $? -ne 0 ] ; then
      echo "Error: Unable to start nskernd"
      exit 1
    fi
  fi
}

do_dscfglockd ()
{
  ps -e | grep -w dscfglockd > /dev/null 2>&1
  if [ $? -ne 0 ] ; then
    rm -f /var/tmp/cfglockd.pid
  fi
  if $CLINFO
  then
    # clustered start of dscfglockd
    if [ -f /var/opt/SUNWesm/dscfglockd.cf ]
    then
      ${SCMLIB}/dscfglockd -f /var/opt/SUNWesm/dscfglockd.cf
    else
      echo "WARNING: Mis-Configuration of StorEdge Dataservices for clusters"
      echo "WARNING: Can't find configuration file for dscfglockd"
    fi
  else
    # non clustered start of dscfglockd here, if required
    if [ -f /var/opt/SUNWesm/dscfglockd.cf ]
    then
      ${SCMLIB}/dscfglockd -f /var/opt/SUNWesm/dscfglockd.cf
    fi
  fi
}

do_sdbc ()
{
      ${SCMBIN}/scmadm  -e
      if [ $? -ne 0 ] ; then
        echo "Error: Cache initialization failed"
        exit 1
      fi
}


#
# do_bitmapfs()
# - mount any bitmap filesystems that the StorEdge Data Services
#   software is going to need before S01MOUNTFSYS is run.
#
do_bitmapfs()
{
	TMPFSTAB=/var/tmp/scm$$
	VFSTAB=/etc/vfstab

	if $CLINFO
	then
		# in cluster
		if [ "$CLUSTERTAG" != "" ]
		then
			# cluster node start, bitmapfs not supported, return
			return
		else
			# physical host start, nothing to do, return
			return
		fi
	else
		:	# non-clustered start
	fi

	/usr/bin/rm -f $TMPFSTAB
	/usr/bin/touch $TMPFSTAB >/dev/null 2>&1
	if [ ! -f $TMPFSTAB ]
	then
		echo "Could not create $TMPFSTAB"
		echo "StorEdge Data Service bitmap filesystems not mounted"
		return
	fi

	$SCMBIN/scmadm -L |
	while read bitmap
	do
		case "$bitmap" in
		'#'*|'')
			continue	# ignore comment or blank line
			;;
		esac

		/usr/bin/awk '
			BEGIN {
				found = 0
			}

			("'$bitmap'" == $1 || "'$bitmap'" == $3) {
				# matches block device or mount point
				found = 1
				# print, converting "mount at boot" to "yes"
				printf("%s %s %s %s %s yes %s\n", \
					$1, $2, $3, $4, $5, $7)
				exit
			}

			END {
				if (!found) {
					print \
"Error: '$bitmap' found in configuration but not found in '$VFSTAB'" |"cat >&2"
				}
			}' $VFSTAB
	done >$TMPFSTAB

	if [ -s $TMPFSTAB ]
	then
		echo "Mounting StorEdge Data Service bitmap filesystems"
		/usr/sbin/mountall $TMPFSTAB
	fi

	/usr/bin/rm -f $TMPFSTAB
}


do_start ()
{
  do_bitmapfs
  do_nskernd
  do_sdbc
}


do_usage ()
{
  echo "Usage: $0"
  echo "   start"
  echo "   stop"
  exit 1
}

set_system_type

USED=0
ACTION=
CLUSTERTAG=

case $# in 
'0')
     do_usage
     ;;
'1') 
     ACTION=$1
     USED=1
     ;;
'2')
     ACTION=$1
     CLUSTERTAG="$2"
     USED=1
     exit 0
     ;;
'*')
     do_usage
     ;;
esac

if [ $USED = 0 ] ; then
     do_usage
fi

if [ $ACTION = "start" ] ; then
  do_start
elif [ $ACTION = "stop" ] ; then
  do_stop
else 
  do_usage
fi

exit 0
