#!/bin/sh

# Source function library.
. /etc/init.d/functions

TABFILE=/etc/auto.tab

# Parse autofs config.
SourceIfNotEmpty /etc/sysconfig/autofs &&
	[ ! -s "$TABFILE" ] ||
	exit 0
	
devices="/mnt/cdrom /mnt/floppy /mnt/zip"
header="# Autogenerated configuration file for autofs"

cat >/etc/auto.master <<EOF
# Format of this file:
# mountpoint map options
# For details of the format look at autofs(8).

$header
/mnt/auto	$TABFILE	--timeout=5
EOF

echo "$header" >"$TABFILE"

# Loop over every line in /etc/fstab
(cat /etc/fstab; echo) | while read device mpoint fstype opts extra; do
	# Ignore empty lines and comments.
	[ -n "${device##\#*}" ] || continue

	for i in $devices; do
		for z in "" 2 3 4 5 6 7 8 9; do
			d="$i$z"
			if [ "$mpoint" = "$d" ]; then
				# Fetch mpoint's basename.
				mpoint="${mpoint##*/}"

				# Hack some options.
				ro=
				umask=
				iocharset=
				codepage=
				quiet=
				SAVED_IFS="$IFS"
				IFS=,
				for opt in $opts; do
					if [ "$opt" = ro ]; then
						ro="$opt"
					elif [ "$opt" = quiet ]; then
						quiet="$opt"
					elif [ -z "${opt##umask=*}" ]; then
						umask="$opt"
					elif [ -z "${opt##iocharset=*}" ]; then
						iocharset="$opt"
					elif [ -z "${opt##codepage=*}" ]; then
						codepage="$opt"
					fi
				done
				IFS="$SAVED_IFS"

				if [ -z "$umask" -a -z "$ro" ]; then
					umask='umask=000'
				fi

				[ -z "$ro" ] || fstype="$fstype,$ro"
				[ -z "$umask" ] || fstype="$fstype,$umask"
				[ -z "$iocharset" ] || fstype="$fstype,$iocharset"
				[ -z "$codepage" ] || fstype="$fstype,$codepage"
				[ -z "$quiet" ] || fstype="$fstype,$quiet"

				echo "$mpoint	-fstype=$fstype	:$device" >>"$TABFILE"
				ln -s "/mnt/auto/$mpoint" "$d/auto" >/dev/null 2>&1
				break 2
			fi
		done
	done
done

[ "$ENABLE" != no ] &&
	[ -x /etc/init.d/autofs -a -s "$TABFILE" ] &&
	ExecIfExecutable /etc/init.d/autofs skipstart ||:
