#!/bin/ksh -p
#
# ident "@(#)utpkg-utstkctl.ksh	1.4 09/09/17 SMI"
#
# Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#

set -u

function disable {
    
    
#
# Uninstall the utdisk and utdiskctl pseudo drivers
#    
      
# remove any existing driver with this name
    /bin/sed -e "/name=$UTDISK[ 	]/d" \
	-e "/name=$UTDISKCTL[ 	]/d" \
	$DEVTAB > $NEWFILE
    
# "Atomic update"; rename is supposed to be atomic so a crash will
# either leave the old file or the new file as $DEVTAB
    /usr/bin/rm -f "${DEVTAB}.old"
    /usr/bin/ln "${DEVTAB}" "${DEVTAB}.old"
    /usr/bin/mv -f "${NEWFILE}" "${DEVTAB}"
    
# uninstall the drivers (if loaded), remove utdisk first
    $REM_DRV $UTDISK
    $REM_DRV $UTDISKCTL
    
    return 0
        
}

function enable {

    
    typeset ADD_DRV="/usr/sbin/add_drv"    
    ret=0
    
#
# Install the utdiskctl and utdisk pseudo drivers
#
    
    /bin/rm -f $NEWFILE
    
# save a backup copy of the driver link table if it doesn't already exist.
    if [ ! -e $DEVTAB.utbak ]; then
	/usr/bin/cp -f $DEVTAB $DEVTAB.utbak
    fi

    
# remove any existing driver with these names
    /bin/sed -e "/name=$UTDISK[ 	]/d" \
	-e "/name=$UTDISKCTL[ 	]/d" \
	$DEVTAB > $NEWFILE
    
# add these drivers to the end of the link table
# utdiskctl should be added first so that it'll be loaded first on reboot
    echo "type=ddi_pseudo;name=$UTDISKCTL	\D"  >> $NEWFILE
    echo "type=ddi_pseudo;name=$UTDISK	\D"  >> $NEWFILE
    
# Atomic update; rename is supposed to be atomic so a crash will
# either leave the old file or the new file as $DEVTAB
    /bin/rm -f "${DEVTAB}.old"
    /bin/ln "${DEVTAB}" "${DEVTAB}.old"
    /bin/mv -f "${NEWFILE}" "${DEVTAB}"
    
    cd $TARGDIR
#
# First remove older versions, removing utdisk first, before utdiskctl
# then check that the 32-bit version of the driver and the conf file are
# both there before calling add_drv.
# When adding drv, do utdiskctl first
#
    
    $REM_DRV $UTDISK >/dev/null 2>&1
    $REM_DRV $UTDISKCTL >/dev/null 2>&1
    
    if [ -f "${TARGDIR}/${UTDISKCTL}" -a -f "${TARGDIR}/${UTDISKCTL}.conf" ]; then
	$ADD_DRV -m '* 0600 bin bin' $UTDISKCTL
    else
	logit "Error:  Sun Ray disk controller pseudo driver ($UTDISKCTL) files not in $TARGDIR"
	ret=1
    fi
    
    if [ -f "${TARGDIR}/${UTDISK}" -a -f "${TARGDIR}/${UTDISK}.conf" ]; then
	$ADD_DRV -m '* 0600 bin bin' $UTDISK
    else
	logit "Error:  Sun Ray disk pseudo driver ($UTDISK) files not in $TARGDIR"
	ret=1
    fi
    
    return $ret
    
}


########################################################################
# Local Variables
# They must defined here to resolve some of the parameter resolution
# problem
########################################################################
local_variables() {

    REM_DRV="/usr/sbin/rem_drv"
#
# The utdisk and utdiskctl pseudo drivers
#
    
    UTDISK="utdisk"
    UTDISKCTL="utdiskctl"
    DEVTAB="/etc/devlink.tab"
    NEWFILE="${DEVTAB}.new"
    TARGDIR="/usr/kernel/drv"

}


########################################################################
#
# Configurable parameter
#
########################################################################
# NONE


########################################################################
#
# Variables used in the shared script utctl-shlib
#
########################################################################
DESCR="Installs/Uninstalls the utdisk and utdiskctl pseudo drivers"


########################################################################
#
# Execution starts here
#
PROGPATH=`dirname $0`
. $PROGPATH/utctl-shlib
