#!/bin/sh
set -e

if [ "$1" = driver ]; then
	WANTDRIVER=1
elif [ "$1" = floppy ]; then
	WANTFLOPPY=1
fi

MNT=/media

devlist() {
	if [ "$WANTFLOPPY" ]; then
		list-devices floppy
	else
		# Ordering: First look for USB media (also finding whole fixed
		# disks, but that's ok). Then look for floppies. Finally, try
		# scanning partitions last.
		list-devices disk
		list-devices maybe-usb-floppy
		list-devices floppy
		list-devices partition
	fi
}

media_mounted() {
	mount | cut -d' ' -f3 | grep -q "^$MNT$"
}

try_mount() {
	# TO REMOVE, there is a bug somewhere in the kernel, the first
	# mount command fail when changing floppy disk
	# so we have to launch mount twice
	mount $1 -tauto $MNT || true
	umount $MNT || true
	mount $1 -tauto $MNT
	media_mounted && checkcontents $MNT
}

checkcontents() {
	dir="$1"

	if [ "$WANTDRIVER" ]; then
		# Make sure this is driver media by checking the files on
		# it. Check for regular debs, udebs, and to be on the safe
		# side, check for *.ude files (msdos file names...)
		for filename in $dir/*.deb $dir/*.udeb $dir/*.ude \
		    $dir/firmware/*.deb $dir/firmware/*.udeb $dir/firmware/*.ude; do
			if [ -f "$filename" ]; then
				return 0 # success
			fi
		done
		# umount can legitimatly fail if something is keeping
		# it open
		umount $dir 2>/dev/null || true
		return 1
	else
		return 0 
	fi
}

if ! ( media_mounted && checkcontents $MNT ); then
	# Special case for an already mounted /hd-media.
	if [ -d /hd-media ] && checkcontents /hd-media; then
		mount --bind /hd-media /media
		exit 0
	fi

	if ! grep -q ^vfat /proc/modules ; then
		log-output -t mountmedia modprobe -q vfat || true
	fi

	# Repeat twice if necessary, to accomodate devices that need some
	# time to initialise, like USB devices.
	for i in 1 2; do
		for dev in $(devlist); do
			if try_mount $dev; then
				exit 0 # success
			fi
		done
		
		if [ "$i" = 1 ]; then       
			# Give USB time to settle, make sure all devices are
			# seen next time though.
			sleep 5
		fi
	done

	# Maybe the floppy module needs to be explicitly loaded?
	DEV=/dev/fd0
	if [ ! -e $DEV ]; then
		log-output -t mountmedia modprobe -q floppy || true
		update-dev --settle
	fi

	if [ ! -e "$DEV" ] || ! try_mount $DEV; then
		# Maybe the system has an ide-floppy device?
		log-output -t mountmedia modprobe -q ide-floppy || true
		update-dev --settle
		try_mount $DEV
	fi
fi
