#!/bin/sh
# 	http://www.berklix.com/~jhs/bin/.sh/rc.shutusb
# Installed to /site/etc/rc.shutusb by ~/public_html/bin/.sh/Makefile
# Called by:	/etc/rc.shutmedia
#	http://www.berklix.com/~jhs/bin/.sh/rc.shutmedia
# See Also 
# 	http://www.berklix.com/~jhs/bin/.sh/rc.shutint
#	http://www.berklix.com/~jhs/bin/.sh/gbde2.sh
# Purpose:
#	unmounts USB sticks & disks mounted on /media of type ufs dos & gbde+ufs.
# Copyright "Julian H. Stacey" <jhs_ERASE_@berklix.com>, public under BSD licence.

# Not needed:
#	In /boot/loader.conf add	hw.usb.no_shutdown_wait=1
# 	9.1:  sysctl -d hw.usb.no_shutdown_wait
#	hw.usb.no_shutdown_wait: No USB device waiting at system shutdown.

DBG="true"	# Normal quiet running
# DBG="echo"	# To debug

$DBG "STARTING $0 `date -u +%Y-%m-%dT%H:%M:%SZ`"

$DBG "Before: /media/*" # mount | grep /media	# brackets.c: "*/"
xx=`/bin/ls -1 /media | grep -v Old 2> /dev/null`
if [ "X $xx" != "X " ];	#{
then
	for i in \
		/media/[a-z0-9]*/s[1-4]/[a-z]* \
		/media/[a-z0-9]*/[a-z]* \
		/media/[a-z0-9]* ; do	#{

		# /media/[a-z0-9]*/s[1-4]/[a-z]* for:
		# /media/sata1t/s1/usr
		# /media/sata1t/s1/var
		# /media/sata1t/s2/tmp
		# /media/sata1t/s2/usr
		# /media/sata1t/s2/var
		# /media/sata1t/s3/usr
		# /media/sata1t/s3/var
		# /media/sata1t/s4/usr
		# /media/sata1t/s4/var

		# /media/[a-z0-9]*/[a-z]* for:
		# /media/sata1t/s1
		# /media/sata1t/s2
		# /media/sata1t/s3
		# /media/sata1t/s4
		# /media/sata1t/data

		# /media/[a-z0-9]* for
		# /media/500g
		# /media/platinum.dos
		# /media/platinum.end
		# /media/platinum.os
		# /media/platinum.ufs

		umount $i
		# Some umount will fail if old junk directories left
		# from previous boots, & /media/Old	will always complain.
		rmdir $i
		#	Some old junk directories may also have sub dirs, so
		#	may need to be manually removed.
		base=`basename $i`

		# JJLATER I will
		# make this more genericly useful for others by deducing
		# from $base a new $extension, & use just the extension
		# to feed to the case switch to determine type of
		# action, but before that I will have to adopt change
		# of mount point names in /site/etc/devd/berklix.conf:
		# probably retain .ufs for a normal UFS, & add .gufs for a GBDE
		# encrypted UFS.

		case $base in	#{
			#(
			delock.dos|delock.end|delock.os|platinum.dos|platinum.end|platinum.os|sanjes.dos)
				$DBG "dos or ufs not encrypted, just rm"
				rm -f /devusb/`basename $base`
				;;
			#(
			delock.ufs|platinum.ufs|sanjes.ufs)
				# .bde encypted, was mounted on .ufs
				name1=`basename $base .ufs`.name # platinum.name
				name2=`cat /devusb/$name1`	 # /dev/da1s3
				# echo "Detaching crypted image $base -> $name2"
				gbde detach $name2
				rm /devusb/$name1
				rm /devusb/`basename $base .ufs`.bde
				;;
			#(
			# Is it possible to set a default) ?
				# $I AM UNKNOWN
				# ;;
		esac	#}
	done	#}
fi	# }
$DBG "After: `echo /media/*`"	# brackets.c: "*/"
# mount | grep /crypt

# Before I wrote this script, after `halt -p` the USB used to hang,
# necessitating a physical power off, (not possible on remote servers)
# so do all we can to ensure file systems are consistent.
sync
# Later the sync above may need to be removed or ``if''d around,
# if this script is also called by halt (reboot.c) with various parameters.
# & probably kernel gurus would assure us appending a "sleep 3;sync"
# would do no good.

$DBG "FINISHING $0 `date -u +%Y-%m-%dT%H:%M:%SZ`"
