#!/bin/bash

[ "$1" = start ] || exit 0

mount_event_needed=

eof=
while [ -z "$eof" ]; do
	read -r fsdev fsdir fstype fsopts freq passno ||
		eof=1

	[ -n "$fsdev" ] ||
		continue

	fsoptions=()
	readarray -t fsoptions <<<"${fsopts//,/$'\n'}"

	if [ -z "${fsdev##SOURCE=*}" ]; then
		fsdev="${fsdev#SOURCE=}"
		fsoptions+=("x-initrd-mount-virtual")
		mount_event_needed=1
	fi

	fsopts=
	for (( i=0; i < ${#fsoptions[@]}; i++ )); do
		[ -n "${fsoptions[$i]}" ] ||
			continue
		for (( j=0; j < $i; j++ )); do
			[ "${fsoptions[$i]}" != "${fsoptions[$j]}" ] ||
				continue 2
		done
		fsopts="${fsopts:+$fsopts,}${fsoptions[$i]}"
	done

	printf '%s %s %s %s %s %s\n' \
		"$fsdev" "$fsdir" "$fstype" "$fsopts" "$freq" "$passno"

done < /etc/fstab > /etc/fstab."$$"

mv /etc/fstab."$$" /etc/fstab

if [ -n "$moount_event_needed" ]; then
	. /etc/init.d/functions
	action 'Sending low level mount event:' /lib/uevent/filters/mountdev
fi
