#! /bin/sh

# ident "@(#)postremove	1.4	95/02/02 SMI"
# SUNWpcser postremove script

PATH="/usr/bin:/usr/sbin:${PATH}"
export PATH

NAMEMAJOR="${BASEDIR}/etc/name_to_major"
MINORPERM="${BASEDIR}/etc/minor_perm"
DEVLINKTB="$BASEDIR/etc/devlink.tab"
DRIVERALS="$BASEDIR/etc/driver_aliases"

if test ! -f ${NAMEMAJOR} -o ! -w ${NAMEMAJOR}
then
	echo "rem_major: data file \'${NAMEMAJOR}\' is missing/inacessible." >&2
	exit 1
fi

if test ! -f ${MINORPERM} -o ! -w ${MINORPERM}
then
	echo "rem_minor: data file \'${MINORPERM}\' is missing/inacessible." >&2
	exit 1
fi

if test ! -f ${DEVLINKTB} -o ! -w ${DEVLINKTB}; then
	echo "rem_devlink: data file ${DEVLINKTB} is missing/inacessible." >&2
	exit 1
fi

if test ! -f ${DRIVERALS} -o ! -w ${DRIVERALS}; then
	echo "add_drv_alias: data file ${DRIVERALS} is missing/inacessible." >&2
	exit 1
fi

trap "rm -f /tmp/$$.*;exit" 0 1 2 3 13 15
TMP=/tmp/$$

# removes major numbers from system for device drivers
# Usage:
#	rem_major name1 name2 name3 name4 ....

rem_major()
{
sort +1 -2n ${NAMEMAJOR} | nawk '#
NR == 1	{n=split(namelist,x)
	 if (n < 1)
	    exit(1)
	 for (i=1; i<=n; i++)
	    chklist[x[i]]++
	}
	{if ($1 in chklist)
		print
	}
' namelist="$*" - > $TMP.del
if [ -s $TMP.del ]
then
    fgrep -v -x -f $TMP.del ${NAMEMAJOR} | sort +1 -2n -o ${NAMEMAJOR}
fi
}


# removes minor permissions from system for device drivers
# Usage:
#	rem_minor name1 name2 name3 name4 ....
#    i.e.
#	rem_minor 'sd:*' 'sd:a'

rem_minor()
{
for p in "$@"
do echo "$p"
done | nawk '#
FILENAME != "-"	{permtab[$1] = $0;next}
FILENAME == "-"	{
		 if ($1 !~ /:/) {
			 printf("Bad name:minor pair: %s, skipping.\n", $1)
			 next
		 }
		 if ($1 in permtab)
		     print permtab[$1] >> permdel
		 next
		}
' permdel=$TMP.perm ${MINORPERM} - >&2

if [ -s $TMP.perm ]
then
    fgrep -v -x -f $TMP.perm ${MINORPERM} | sort +0 -1 -o ${MINORPERM}
fi
}


# removes entries from devlink.tab file
# Usage:
#	rem_devlink spec1 spec2 spec3 spec4 ...
#
#	where each <spec> is:
#	    <devfs-spec> <dev-name> <dev-link>
#

rem_devlink()
{
while test $# -ge 2
do
echo "$1	$2"
shift 2
done | nawk '-F	' '#
FILENAME != "-"		{devlinktab[$1] = $0;next}
FILENAME == "-"		{if ($1 in devlinktab)
			     print devlinktab[$1] >> devlinktmp
			}
' devlinktmp=$TMP.devlink ${DEVLINKTB} - >&2

if [ -s $TMP.devlink ]
then
	fgrep -v -x -f $TMP.devlink ${DEVLINKTB} > $TMP.devlinknew && \
	cp $TMP.devlinknew ${DEVLINKTB}
fi
}

# removes entries to driver_aliases file
# Usage:
#	rem_drv_alias spec1 spec2 spec3
#
#	where spec is:
#		<driver> <alias>

rem_drv_alias()
{
while test $# -ge 2
do
	echo "$1 $2"
	shift 2
done | nawk '#
FILENAME != "-"		{drvalias[$1] = $0;next}
FILENAME == "-"		{if ($1 in drvalias)
			     print drvalias[$1] >> drvaliastmp
			}
' drvaliastmp=$TMP.drvalias ${DRIVERALS} - >&2

if [ -s $TMP.drvalias ]
then
#	echo Removing 
#	cat $TMP.drvalias

	fgrep -v -x -f $TMP.drvalias ${DRIVERALS} > $TMP.drvaliasnew && \
	cp $TMP.drvaliasnew ${DRIVERALS}
fi
}

#
# this function removes the PCMCIA serial port entries
#	from the $BASEDIR/etc/remote file
#
cleanup_etc_remote()
{

		if fgrep -s "PCMCIA" $BASEDIR/etc/remote
		then
			( echo "/PCMCIA"
			  echo "d" ; echo "d"
			  echo "." ; echo "w" ; echo "q"
			) | ed -s $BASEDIR/etc/remote > /dev/null
		fi
		if egrep -s '^pc0:' $BASEDIR/etc/remote
		then
			( echo "/^pc0:"
			  echo "d" ; echo "d"
			  echo "." ; echo "w" ; echo "q"
			) | ed -s $BASEDIR/etc/remote > /dev/null
		fi
		if egrep -s '^pc1:' $BASEDIR/etc/remote
		then
			( echo "/^pc1:"
			  echo "d" ; echo "d"
			  echo "." ; echo "w" ; echo "q"
			) | ed -s $BASEDIR/etc/remote > /dev/null
		fi
		if egrep -s '^pc2:' $BASEDIR/etc/remote
		then
			( echo "/^pc2:"
			  echo "d" ; echo "d"
			  echo "." ; echo "w" ; echo "q"
			) | ed -s $BASEDIR/etc/remote > /dev/null
		fi
		if egrep -s '^pc3:' $BASEDIR/etc/remote
		then
			( echo "/^pc3:"
			  echo "d" ; echo "d"
			  echo "." ; echo "w" ; echo "q"
			) | ed -s $BASEDIR/etc/remote > /dev/null
		fi
		if egrep -s '^pc4:' $BASEDIR/etc/remote
		then
			( echo "/^pc4:"
			  echo "d" ; echo "d"
			  echo "." ; echo "w" ; echo "q"
			) | ed -s $BASEDIR/etc/remote > /dev/null
		fi
		if egrep -s '^pc5:' $BASEDIR/etc/remote
		then
			( echo "/^pc5:"
			  echo "d" ; echo "d"
			  echo "." ; echo "w" ; echo "q"
			) | ed -s $BASEDIR/etc/remote > /dev/null
		fi
		if egrep -s '^pc6:' $BASEDIR/etc/remote
		then
			( echo "/^pc6:"
			  echo "d" ; echo "d"
			  echo "." ; echo "w" ; echo "q"
			) | ed -s $BASEDIR/etc/remote > /dev/null
		fi
		if egrep -s '^pc7:' $BASEDIR/etc/remote
		then
			( echo "/^pc7:"
			  echo "d" ; echo "d"
			  echo "." ; echo "w" ; echo "q"
			) | ed -s $BASEDIR/etc/remote > /dev/null
		fi
}

#
# cleanup the $BASEDIR/etc/remote file
#
cleanup_etc_remote

#
# If $BASEDIR isn't / (ie, we're doing a netinstall or a CD-ROM remove)
# then update name_to_major, minor_perm, and devlink.tab by hand.
# otherwise, use rem_drv.
#
if [ "$BASEDIR" = "/" ] ; then
	#
	# We're modifying the running system
	#
	if /bin/fgrep -s "pcser" /etc/name_to_major; then
	        /usr/sbin/rem_drv pcser
	        rc=$?
	        test $rc -eq 0 && exit 0
	        #
	        # fall through if error in rem_drv
	        #
	fi
fi

rem_major pcser
rem_minor 'PC,serial:* 0666 bin bin'
fgrep -v pcser $BASEDIR/etc/driver_aliases >$TMP
mv $TMP $BASEDIR/etc/driver_aliases

# rem_drv_alias 'pcser' 'PC,serial'

touch ${BASEDIR}/reconfigure

exit 0
