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

set -u

function disable {
    
#
# Uninstall the Sun Ray, utadem, utserial and utparallel pseudo drivers
#
    
       
# remove any existing driver with this name
#
    sed \
	-e "/name=$SUNRAY[ 	]/d" \
	-e "/name=$UTADEM[ 	]/d" \
	-e "/name=$UTSERIAL[ 	]/d" \
	-e "/name=$UTPARALLEL[ 	]"/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
    rm -f $DEVTAB.old
    ln $DEVTAB $DEVTAB.old
    mv -f $NEWFILE $DEVTAB
    
# uninstall the drivers (if loaded)
    $REM_DRV $SUNRAY
    $REM_DRV $UTADEM
    $REM_DRV $UTSERIAL
    $REM_DRV $UTPARALLEL
    
    echo "### removed Sun Ray pseudo drivers"
    
    return 0

}

function enable {
    
    typeset ADD_DRV="/usr/sbin/add_drv"    
    
    ret=0
    
#
# Install the Sun Ray, utadem, utserial and utparallel 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=$SUNRAY[ 	]/d" \
	-e "/name=$UTADEM[ 	]/d" \
	-e "/name=$UTSERIAL[ 	]/d" \
	-e "/name=$UTPARALLEL[ 	]"/d \
	$DEVTAB > $NEWFILE
    
# add these drivers to the end of the link table
    echo "type=ddi_pseudo;name=$SUNRAY	\M0" >> $NEWFILE
    echo "type=ddi_pseudo;name=$UTADEM	\M0" >> $NEWFILE
    echo "type=ddi_pseudo;name=$UTSERIAL	\D"  >> $NEWFILE
    echo "type=ddi_pseudo;name=$UTPARALLEL	\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
#
# for each of the drivers to be installed, first remove the old version,
# then check that the 32-bit version of the driver and the conf file are
# both there before calling add_drv.
#
    
#
# utadem
#
    
    $REM_DRV $UTADEM >/dev/null 2>&1
    
    
    if [ -f "${TARGDIR}/${UTADEM}" -a -f "${TARGDIR}/${UTADEM}.conf" ]; then
	$ADD_DRV -m '* 0600 bin bin' $UTADEM
	echo "### successfully installed Sun Ray audio pseudo driver ($UTADEM)"
    else
	echo "Error:  Sun Ray audio pseudo driver ($UTADEM) files not in $TARGDIR"
	ret=1
    fi
    
#
# utserial
#
    
    $REM_DRV $UTSERIAL >/dev/null 2>&1
    
    if [ -f "${TARGDIR}/${UTSERIAL}" -a -f "${TARGDIR}/${UTSERIAL}.conf" ]; then
	$ADD_DRV -m '* 0600 bin bin' $UTSERIAL
	echo "### successfully installed Sun Ray serial port pseudo driver ($UTSERIAL)"
    else
	echo "Error:  Sun Ray serial port pseudo driver ($UTSERIAL) files not in $TARGDIR"
	ret=1
    fi
    
#
# utparallel
#
    
    $REM_DRV $UTPARALLEL >/dev/null 2>&1
    
    if [ -f "${TARGDIR}/${UTPARALLEL}" -a -f "${TARGDIR}/${UTPARALLEL}.conf" ]; then
	$ADD_DRV -m '* 0600 bin bin' $UTPARALLEL
	echo "### successfully installed Sun Ray parallel port pseudo driver ($UTPARALLEL)"
    else
	echo "Error:  Sun Ray parallel port pseudo driver ($UTPARALLEL) files not in $TARGDIR"
	ret=1
    fi
    
#
# sunray
#
    $REM_DRV $SUNRAY >/dev/null 2>&1
    
    if [ -f "${TARGDIR}/${SUNRAY}" -a -f "${TARGDIR}/${SUNRAY}.conf" ]; then
	$ADD_DRV -m '* 0666 bin bin' $SUNRAY
	echo "### successfully installed Sun Ray pseudo driver"
    else
	echo "Error:  Sun Ray pseudo driver 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 Sun Ray, utadem, utserial and utparallel pseudo drivers
#

    SUNRAY="sunray"
    UTADEM="utadem"
    UTSERIAL="utserial"
    UTPARALLEL="utparallel"
    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 utadem, utserial and utparallel pseudo drivers"


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