#!/bin/sh

. /etc/init.d/globals

SRVFLAG=/var/etc/.srv

MTAB=/etc/mtab
FSTAB=/etc/fstab
FSTAB_VAR=/var/etc/fstab

is_mount()
{
	RET=1
	test -f $MTAB || return $RET
	while read _DEV _MTPT _FSTYPE _OPTS _REST
	do
		case "$_FSTYPE" in
			"tmpfs")
				continue
			;;
		esac
		case "$1" in
			"$_DEV"|"$_MTPT")
				RET=0
				break
			;;
		esac
	done < $MTAB
	return $RET
}

mount_local()
{
	SHOWINFO "mount all local stuff from $FSTAB"
	test -f $FSTAB || return
	while read DEV MTPT FSTYPE OPTS REST
	do
		case "$DEV" in
			""|\#*)
				continue
			;;
		esac
		case "$OPTS" in
			noauto|*,noauto|noauto,*|*,noauto,*)
				continue
			;;
		esac
		case "$FSTYPE" in
			swap)
				#SHOWINFO "enable all swaps from $FSTAB"
				#swapon -a
				SHOWINFO "ignoring all swaps from $FSTAB"
				continue
			;;
			nfs|cifs)
				continue
			;;
			*)
				test -d $MTPT || mkdir -p $MTPT;
				SHOWINFO "trying to mount $DEV to $MTPT"
				if OUT=$(mount $MTPT 2>&1 >/dev/null)
				then
					RET=$?
					LOGINFO "mount: $MTPT - success ($RET)"
				else
					RET=$?
					LOGWARN "mount: $MTPT - failed ($RET)"
					echo "$OUT" | LOGWARN
				fi
			;;
		esac
	done < $FSTAB
}

mount_netfs()
{
	SHOWINFO "mount all netfs stuff from $FSTAB_VAR"
	test -f $FSTAB_VAR || return
	rm -f $SRVFLAG
	while read DEV MTPT FSTYPE OPTS REST
	do
		case "$DEV" in
			""|\#*)
				continue
			;;
		esac
	        case "$OPTS" in
			noauto|*,noauto|noauto,*|*,noauto,*)
				continue
			;;
		esac
		case "$FSTYPE" in
			nfs|cifs)
				if ! is_mount $MTPT; then
					test -d $MTPT || mkdir -p $MTPT;
					SHOWINFO "trying to mount $DEV to $MTPT"
					(
					try=51
					while(true); do
						if OUT=$(mount -t $FSTYPE -o $OPTS $DEV $MTPT 2>&1 >/dev/null); then
							RET=$?
							LOGINFO "mount: $MTPT - success ($RET)"
							test -e $SRVFLAG || touch $SRVFLAG
							break
						else
							RET=$?
							LOGWARN "mount: $MTPT - failed ($RET)"
							echo "$OUT" | LOGWARN
							if [ ${try:1:1} -eq ${try:0:1} ]; then
								LOGWARN "mount: $MTPT - cancel!"
								break
							fi
							try=$((try+1))
							LOGWARN "mount: $MTPT - try ${try:1:1} in 30 seconds ..."
							sleep 30
						fi
					done
					) &
				else
					SHOWINFO "already mounted $MTPT"
					test -e $SRVFLAG || touch $SRVFLAG
				fi
			;;
			*)
				continue
			;;
		esac
	done < $FSTAB_VAR
}

umount_netfs()
{
	SHOWINFO "unmount all netfs stuff from $MTAB"
	test -f $MTAB || return
	rm -f $SRVFLAG
	while read DEV MTPT FSTYPE OPTS REST
	do
		case "$OPTS" in
			noauto|*,noauto|noauto,*|*,noauto,*)
				continue
			;;
		esac
		case "$FSTYPE" in
			nfs|cifs)
				SHOWINFO "trying to unmount $DEV from $MTPT"
				(
				if OUT=$(umount -f $MTPT 2>&1 >/dev/null); then
					RET=$?
					LOGINFO "umount: $MTPT - success ($RET)"
				else
					RET=$?
					LOGWARN "umount: $MTPT - failed ($RET)"
					echo "$OUT" | LOGWARN
					test -e $SRVFLAG || touch $SRVFLAG
				fi
				) &
			;;
			*)
				continue
			;;
		esac
	done < $MTAB
}

if [ -e /tmp/.flash.start ]; then
	SHOWINFO "flash.start flag found"
	umount_netfs
	SHOWINFO "exiting"
	exit 0
fi

case "$1" in
	"start")
		mount_local
		mount_netfs
	;;
	"start_netfs")
		mount_netfs
	;;
	"stop_netfs")
		umount_netfs
	;;
	"stop")
		umount_netfs

		#SHOWINFO "unmount all sysfs, tmpfs, devpts and usbfs mounts"
		#umount -a -t sysfs, tmpfs, devpts, usbfs

		#SHOWINFO "disable all swaps"
		#swapoff -a

		#SHOWINFO "detach jffs2 filesystems"
		#umount -l -t jffs2
	;;
	*)
		echo "[$BASENAME] Usage: $0 {start|start_netfs|stop_netfs|stop}"
	;;
esac