#! /bin/sh
#
# Copyright (c) 2002 by Sun Microsystems, Inc.
#

# A few caviats about the execution environment:
#    - do not assume the normal pkg environment is available.
#    - PATH will be /usr/sbin:/usr/bin

SMCREG=${ROOTDIR}/usr/sadm/bin/smcregister
TOOLBOXBASE=${ROOTDIR}/var/sadm/smc/toolboxes
ADMIN_LIBDIR=${ROOTDIR}/usr/sadm/lib
VOLMGR_LIBDIR=${ROOTDIR}/usr/snadm/lib

# if SMC repository doesn't exist, server was never started,
# so no need to create registration requests.  Instead, the
# tool's registration script will make the registration requests.
if [ ! -d "${ROOTDIR}/var/sadm/smc" ]; then
    exit 0
fi

# Register tools and services if the root dir is `/`
if [ -f ${SMCREG} -a "${ROOTDIR}" = "/" ]; then

	${SMCREG} tool -n com.sun.admin.volmgr.client.VVolMgr.jar \
        	${ADMIN_LIBDIR}/volmgr/VVolMgr.jar \
		${ADMIN_LIBDIR}/volmgr/VVolMgr_classlist.txt \
		${ADMIN_LIBDIR}/volmgr/VVolMgrInfo.xml >/dev/null 2>&1 

	${SMCREG} toolbox add -f tool com.sun.admin.volmgr.client.VVolMgr \
        	-F "/Storage/" >/dev/null 2>&1

	${SMCREG} service -n com.sun.admin.volmgr.server.SVMServices.jar \
        	${ADMIN_LIBDIR}/volmgr/SVMServices.jar \
        	${ADMIN_LIBDIR}/volmgr/SVMServices_classlist.txt \
        	${ADMIN_LIBDIR}/volmgr/SVMServicesInfo.xml \
        	${VOLMGR_LIBDIR}/libsvmJNI.so >/dev/null 2>&1

else
	mkdir -p ${ROOTDIR}/usr/sadm/lib/smc/prereg/SUNWlvmg-patch
	cat > ${ROOTDIR}/usr/sadm/lib/smc/prereg/SUNWlvmg-patch/SUNWlvmgpatch_reg.sh << DONE
SMCREG=/usr/sadm/bin/smcregister
TOOLBOXBASE=/var/sadm/smc/toolboxes
ADMIN_LIBDIR=/usr/sadm/lib

# Register tools and services
if [ -f ${SMCREG} ]; then

    ${SMCREG} tool -n com.sun.admin.volmgr.client.VVolMgr.jar \\
        ${ADMIN_LIBDIR}/volmgr/VVolMgr.jar \\
        ${ADMIN_LIBDIR}/volmgr/VVolMgr_classlist.txt \\
        ${ADMIN_LIBDIR}/volmgr/VVolMgrInfo.xml

    ${SMCREG} toolbox add -f tool com.sun.admin.volmgr.client.VVolMgr \\
    	-F "/Storage/"

    ${SMCREG} service -n com.sun.admin.volmgr.server.SVMServices.jar \\
        ${ADMIN_LIBDIR}/volmgr/SVMServices.jar \\
        ${ADMIN_LIBDIR}/volmgr/SVMServices_classlist.txt \\
        ${ADMIN_LIBDIR}/volmgr/SVMServicesInfo.xml \\
        ${VOLMGR_LIBDIR}/libsvmJNI.so

fi
DONE
	cat > ${ROOTDIR}/usr/sadm/lib/smc/prereg/SUNWlvmg-patch/SUNWlvmgpatch_unreg.sh << DONE
#!/bin/sh
#ident "@(#)SUNWlvmgpatch_unreg.sh   1.1 01/05/29 SMI"

# This script functions very much like a preremove script
# but it does NOT get executed out of pkgrm.  Instead it
# is executed by the SMC server upon its next restart after
# the pkg is removed.
#
# A few caviats about the execution environment:
#    - do not assume the normal pkg environment is available.
#    - PATH will be /usr/sbin:/usr/bin

SMCREG=${ROOTDIR}/usr/sadm/bin/smcregister

# Unregister tools and services
if [ -f ${SMCREG} ]; then

    ${SMCREG} tool -u com.sun.admin.volmgr.client.VVolMgr.jar

fi
DONE
	# Set up the registration script so that when the client boots, the
	# new information is registered.

	PREREG_DIR=/usr/sadm/lib/smc/prereg
	id="SUNWlvmg-patch"
	TIMESTAMP_KEY="timestamp"
	TIMESTAMP=`date -u +"%Y"%m"%d"%H"%M"%S""`

	# Create a directorty to hold the registration file.
	if [ ! -d "${PREREG_DIR}/${id}" ]; then
        	mkdir -p ${PREREG_DIR}/${id}
	fi

	# Create the registration file.  If it exists, don't overwrite it.
	MCREG=${PREREG_DIR}/${id}/${id}.mcreg
	if [ -f "${MCREG}" ]; then
		echo "${MCREG} already exists"
		return
	fi
	echo "[${id}.reg]" >> ${MCREG}
	echo "install=${PREREG_DIR}/${id}/SUNWlvmgpatch_reg.sh" >> ${MCREG}
	echo "remove=${PREREG_DIR}/${id}/SUNWlvmgpatch_unreg.sh" >> ${MCREG}
	echo "${TIMESTAMP_KEY}=${TIMESTAMP}" >> ${MCREG}
	
	# make these scripts executable.
	chmod 755 ${PREREG_DIR}/${id}/SUNWlvmgpatch_reg.sh
	chmod 755 ${PREREG_DIR}/${id}/SUNWlvmgpatch_unreg.sh


fi
exit 0
