#!/bin/sh

printf "$*\n"
log=/var/log/devpub-mount/03-devpub-mount
mkdir -p $(dirname $log)
printf "$*\n" >> $log

if [ $1 = "device-attach" ]; then
    device=$2
    case $device in
    cd*)
	/usr/pkg/sbin/devpub-mount $device &
	;;
    
    sd*)
	for partition in /dev/$device[a-z]*; do
	    if ! fstyp $partition 2>&1 | fgrep -q 'Device not configured'; then
		# Allow members of operator to reformat the device
		# and image the parent device
		chmod g+w $partition ${partition%[a-z]}
		/usr/pkg/sbin/devpub-mount $partition >/dev/null 2>&1 &
	    fi
	done
	;;
    
    *)
	# Ignore
	;;

    esac
elif [ $1 = "device-detach" ]; then
    rm -f $log
fi
