#!/bin/sh /etc/rc.common

START=11

boot() {
	local enabled=`uci get tuning_boot.@fstab_delay[0].enabled 2>/dev/null`
	[ -z "$enabled" -o "$enabled" = 0 ] && return 0

	local timeout=`uci get tuning_boot.@fstab_delay[0].timeout 2>/dev/null`
	[ -z "$timeout" -o "$timeout" = 0 ] && return 0

	local devices=`uci get tuning_boot.@fstab_delay[0].device 2>/dev/null`

	if [ -n "$devices" ]; then
		echo "fstab_delay: wait up to $timeout seconds for device(s): $devices" >/dev/kmsg
		local i=0
		local dev
		local ready
		while [ $i -lt $timeout ]; do
			ready=1
			for dev in $devices ; do
				blkid --uuid $dev >/dev/null 2>&1 || { ready=0; break; }
			done
			[ $ready -eq 1 ] && break
			sleep 1
			i=$(($i+1))
		done
		if [ $i -lt $timeout ]; then
			echo "fstab_delay: ready after $i seconds" >/dev/kmsg
		else
			echo "fstab_delay: wait device(s) timeout" >/dev/kmsg
		fi
	else
		echo "fstab_delay: sleep $timeout seconds before fstab" >/dev/kmsg
		sleep $timeout
	fi
	return 0
}
