#!/bin/sh

# This script backs out the preremove script of SUNWusb 
# and delivers a fixed version, if it hasn't been already done in
# previos revisions of this patch


## tools used
SED=sed
ECHO=echo
CAT=cat
DIFF=diff
RM=rm
CP=cp
MV=mv


## constants
FILETOPATCH=${ROOTDIR}/var/sadm/pkg/SUNWusb/install/preremove
TMPDIR=`echo $PARAMS_FILE|$SED -ne 's/^\(.*[/]\)[^/]*$/\1/p;'`
BACKUPFILE="${TMPDIR}/SUNWusb-preremove-${PatchNum}-backup"
TMPFILE=/${TMPDIR}/${PatchNum}_preremove.tmp


${CAT} > $TMPFILE <<ENDOFSCRIPT
#! /bin/sh
#
# Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
# ident	"@(#)preremove	1.7	05/12/18 SMI"
 
PATH="/usr/bin:/usr/sbin:\${PATH}"
export PATH

EXIT=0

installed() {
	driver=\$1
	grep "^\${driver} " \$BASEDIR/etc/name_to_major > /dev/null 2>&1
	
	return \$?
}

#
# Remove the new framework drivers
#
remove_usba10_driver() {
	if installed usba10_hid
	then
		rem_drv -b \${BASEDIR} usba10_hid || EXIT=1 
	fi

	if installed usba10_hubd
	then
		rem_drv -b \${BASEDIR} usba10_hubd || EXIT=1 
	fi

	if installed usba10_scsa2usb
	then
		rem_drv -b \${BASEDIR} usba10_scsa2usb || EXIT=1 
	fi

	if installed usba10_usb_mid
	then
		rem_drv -b \${BASEDIR} usba10_usb_mid || EXIT=1 
	fi

	if installed usba10_usbprn
	then
		rem_drv -b \${BASEDIR} usba10_usbprn || EXIT=1 
	fi

	if installed usba10_ugen
	then
		rem_drv -b \${BASEDIR} usba10_ugen || EXIT=1 
	fi

	if installed usba10_usbser_edge
	then
		rem_drv -b \${BASEDIR} usba10_usbser_edge || EXIT=1 
	fi

	if installed usba10_ohci
	then
		rem_drv -b \${BASEDIR} usba10_ohci || EXIT=1
	fi

	if installed usba10_ehci
	then
		rem_drv -b \${BASEDIR} usba10_ehci || EXIT=1
	fi

}

if installed hid
then
	rem_drv -b \${BASEDIR} hid || EXIT=1
fi

if installed hubd
then
	rem_drv -b \${BASEDIR} hubd || EXIT=1
fi

if installed scsa2usb
then
	rem_drv -b \${BASEDIR} scsa2usb || EXIT=1
fi

if installed usb_mid
then
	rem_drv -b \${BASEDIR} usb_mid || EXIT=1
fi

if installed usbprn
then
	rem_drv -b \${BASEDIR} usbprn || EXIT=1
fi

case \${ARCH} in
	i386)
		if installed uhci
		then	
			rem_drv -b \${BASEDIR} uhci || EXIT=1
		fi
	;;
	sparc)
		if installed ohci
		then
			rem_drv -b \${BASEDIR} ohci || EXIT=1
		fi
		
		if installed usb_sd
		then
			rem_drv -b \${BASEDIR} usb_sd || EXIT=1 
		fi

		if installed usbser_edge
		then
			rem_drv -b \${BASEDIR} usbser_edge || EXIT=1
		fi

		#remove the new framework drivers
		remove_usba10_driver
	;;
esac

exit \$EXIT
ENDOFSCRIPT

# if there is no difference, exit, the patch already has been applied
if [ -f $FILETOPATCH ]; then
	${DIFF} ${TMPFILE} ${FILETOPATCH} > /dev/null
	if [ "$?" = 0 ]; then
		exit 0
	fi
	# backup the preremove file
	${CP} ${FILETOPATCH} ${BACKUPFILE}  
	# overwrite the SUNWusb preremove script
	${MV} ${TMPFILE} ${FILETOPATCH} || exit 1
fi


