#!/usr/bin/ksh
#
# Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
#  This script has the optional -f option:
#  -f <filename> sources a user definable config file
#  different from sczbt_config.

MYNAME=`basename ${0}`

typeset opt

while getopts 'f:' opt
do
        case "${opt}" in
                f)      MYCONFIG=${OPTARG};;
                *)      echo "ERROR: ${MYNAME} Option ${OPTARG} unknown - early End. Only -f is valid"
                        exit 1;;
        esac
done

# Sourcing the specified config file, either the default one,
# or the one supplied with -f

if [ -n "${MYCONFIG}" ] && [ -f "${MYCONFIG}" ]
then
        echo "sourcing ${MYCONFIG} "
        . ${MYCONFIG}
else
        PKGCONF=`dirname $0`/sczbt_config
        echo "sourcing ${PKGCONF}"
        . ${PKGCONF}
fi

# constructing the parameter file

if [ ! -d ${PARAMETERDIR} ]; then
        echo "The value given for PARAMETERDIR (${{PARAMETERDIR}) in sczbt_config is not a directory!"
        exit 1
fi

if [ ! -f ${PARAMETERDIR}/sczbt_${RS} ]; then
	cat > ${PARAMETERDIR}/sczbt_${RS} <<EOF
#!/usr/bin/ksh
#
# Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
#
# Parameters for sczbt (Zone Boot)
# 
# Zonename	Name of the zone
# Zonebootopt	Zone boot options ("-s" requires that Milestone=single-user)
# Milestone	SMF Milestone which needs to be online before the zone is considered as booted
# Mounts        Mounts is a list of directories and their mount options,
#               which are loopbackmounted from the global zoine into the
#               newly booted zone
#               The format is directory:option
#               e. gr.
#               Mounts="/source1:rw /source2:ro"

#

Zonename=${Zonename}
Zonebootopt=${Zonebootopt}
Milestone=${Milestone}
Mounts="${Mounts}"
EOF

else
        echo "The parameterfile ${PARAMETERDIR}/sczbt_${RS} already exists! Will not overwrite and exit."
        exit 1
fi


# Checking dependencies for SC_NETWORK=true

SC_NETWORK=`echo ${SC_NETWORK}|/usr/bin/tr [:upper:] [:lower:]`

if [ "${SC_NETWORK}" == "true" -a -z "${SC_LH}" ]
then
	echo "Error: SC_LH is required with SC_NETWORK=true"
	exit 1
fi

# Checking dependencies for FAILOVER=true

FAILOVER=`echo ${FAILOVER}|/usr/bin/tr [:upper:] [:lower:]`

if [ "${FAILOVER}" == "true" -a -z "${HAS_RS}" ]
then
	echo "Error: HAS_RS is required with FAILOVER=true"
	exit 1
fi

# Setting Resource_dependencies

if  [ "${SC_LH}" ] 
then
	RESOURCE_DEPENDENCIES="-y Resource_dependencies=${SC_LH}"
else
	RESOURCE_DEPENDENCIES=
fi
 
if  [ "${HAS_RS}" ]
then
	if [ "${RESOURCE_DEPENDENCIES}" ]
	then
		RESOURCE_DEPENDENCIES="$RESOURCE_DEPENDENCIES,${HAS_RS}"
	else
		RESOURCE_DEPENDENCIES="-y Resource_dependencies=${HAS_RS}"
	fi
fi
 
if [ ${SC_NETWORK} == "true" ]; then

/usr/cluster/bin/scrgadm -a -j ${RS} -g ${RG} -t SUNW.gds \
-x Start_command="/opt/SUNWsczone/sczbt/bin/start_sczbt \
-R ${RS} -G ${RG} -P ${PARAMETERDIR} " \
-x Stop_command="/opt/SUNWsczone/sczbt/bin/stop_sczbt \
-R ${RS} -G ${RG} -P ${PARAMETERDIR}" \
-x Probe_command="/opt/SUNWsczone/sczbt/bin/probe_sczbt \
-R ${RS} -G ${RG} -P ${PARAMETERDIR}" \
-y Port_list=10000/tcp -y Network_resources_used=${SC_LH} \
-x Stop_signal=9 ${RESOURCE_DEPENDENCIES}

else

/usr/cluster/bin/scrgadm -a -j ${RS} -g ${RG} -t SUNW.gds \
-x Start_command="/opt/SUNWsczone/sczbt/bin/start_sczbt \
-R ${RS} -G ${RG} -P ${PARAMETERDIR}" \
-x Stop_command="/opt/SUNWsczone/sczbt/bin/stop_sczbt \
-R ${RS} -G ${RG} -P ${PARAMETERDIR}" \
-x Probe_command="/opt/SUNWsczone/sczbt/bin/probe_sczbt \
-R ${RS} -G ${RG} -P ${PARAMETERDIR}" \
-x Network_aware=false \
-x Stop_signal=9 ${RESOURCE_DEPENDENCIES}

fi

St=$?

if [ "${St}" -ne 0 ]; then
	echo "Registration of resource ${RS} failed, please correct the wrong parameters."
	echo "Removing parameterfile ${PARAMETERDIR}/sczbt_${RS} for resource ${RS}."
	rm ${PARAMETERDIR}/sczbt_${RS}
        exit 1
else
        echo "Registration of resource ${RS} succeeded."
fi

# VALIDATE RESOURCE

/opt/SUNWsczone/sczbt/bin/validate_sczbt -R ${RS} -G ${RG} -P ${PARAMETERDIR}

St=$?

if [ "${St}" -ne 0 ]; then
	echo "Validation of resource ${RS} failed, check the syslog for the wrong parameters."
        echo "Removing resource ${RS} from the cluster configuration."

        /usr/cluster/bin/scrgadm -r -j ${RS}
	echo "Removing parameterfile ${PARAMETERDIR}/sczbt_${RS} for resource ${RS}."
	rm ${PARAMETERDIR}/sczbt_${RS}

        exit 1
else
        echo "Validation of resource ${RS} succeeded."
fi

exit 0
