#!/bin/sh

. /etc/profile
# Start all init scripts in /etc/init.d
# executing them in numerical order.
#
# Turn off ERROR_NO_HD and ERROR_HD_WRONG_FORMAT
if [ ! -x /sbin/hal_app ]; then
    /sbin/set_status_led 3 0
fi    
# QNAP_PIC_STATUS_OFF=0x59, QNAP_PIC_POWER_LED_BLINK=0x4C 
if [ x`/sbin/getcfg Misc "Buzzer Quiet Enable"` != "xTRUE" ]; then
    if [ -x /sbin/hal_app ]; then
		/sbin/hal_app --se_buzzer enc_id=0,mode=0
    else
	    /sbin/pic_raw 80
    fi
fi
if [ -x /sbin/hal_app ]; then
    /sbin/hal_app --status_led_set enc_id=0,event=0x20000,status=1
    /sbin/hal_app --power_led_blink enc_id=0,enable=1
else
    /sbin/pic_raw 89
    /sbin/pic_raw 76
fi

waitrc()
{
	if [ -z "$1" ] || [ -z "$2" ] || [ -z "$3" ];then
		return 1
	fi
	cnt=$1
	while [ $cnt -lt $2 ]; do
		/bin/ps | grep "$3" | grep -v grep >/dev/null
		if [ $? = 0 ]; then
			sleep 2
		else
			cnt=$2
		fi
		cnt=$(($cnt+1))
	done
	return 0
}

#for QPKG
w=10
for i in /etc/rcK.d/QK??* ;do
     # Ignore dangling symlinks (if any).
     [ -f "$i" ] || continue

     case "$i" in
	*.sh)
		w=0
	    # Source shell script for speed.
	    (
		trap - INT QUIT TSTP
		set stop &
		. $i
	    )
	    ;;
	*)
		w=0
	    # No sh extension, so fork subprocess.
	    [ -f $i ] && $i stop &
	    ;;
    esac
done
waitrc $w 10 "/etc/rcK.d/QK"

w=0
for i in /etc/rcK.d/K0?* ;do
	# Ignore dangling symlinks (if any).
	[ -f "$i" ] || continue
	case "$i" in
	*.sh)
	    # Source shell script for speed.
	    (
		trap - INT QUIT TSTP
		set stop
		. $i &
	    )
	    ;;
	*)
	    # No sh extension, so fork subprocess.
	    $i stop &
	    ;;
    esac
done
waitrc $w 5 "/etc/rcK.d/K0"

for i in /etc/rcK.d/K1?* ;do
	# Ignore dangling symlinks (if any).
	[ -f "$i" ] || continue
	case "$i" in
	*.sh)
	    # Source shell script for speed.
	    (
		trap - INT QUIT TSTP
		set stop
		. $i &
	    )
	    ;;
	*)
	    # No sh extension, so fork subprocess.
	    $i stop &
	    ;;
    esac
done
waitrc $w 5 "/etc/rcK.d/K1"

for i in /etc/rcK.d/K2?* ;do
	# Ignore dangling symlinks (if any).
	[ -f "$i" ] || continue
	case "$i" in
	*.sh)
	    # Source shell script for speed.
	    (
		trap - INT QUIT TSTP
		set stop
		. $i &
	    )
	    ;;
	*smb)
	    $i stop
	    ;;
	*ftp)
	    $i stop
	    ;;
	*nfs)
	    $i stop
	    ;;
	*)
	    # No sh extension, so fork subprocess.
	    $i stop &
	    ;;
    esac
done
waitrc $w 5 "/etc/rcK.d/K2"

for i in /etc/rcK.d/K[3-5]?* ;do
	# Ignore dangling symlinks (if any).
	[ -f "$i" ] || continue
	case "$i" in
	*.sh)
	    # Source shell script for speed.
	    (
		trap - INT QUIT TSTP
		set stop
		. $i &
	    )
	    ;;
	*)
	    # No sh extension, so fork subprocess.
	    $i stop &
	    ;;
    esac
done
waitrc $w 30 "/etc/rcK.d/K3"
waitrc $w 10 "/etc/rcK.d/K4"
waitrc $w 10 "/etc/rcK.d/K5"

for i in /etc/rcK.d/K[6-9]?* ;do
     # Ignore dangling symlinks (if any).
     [ -f "$i" ] || continue

     case "$i" in
	*.sh)
	    # Source shell script for speed.
	    (
		trap - INT QUIT TSTP
		set stop
		. $i
	    )
	    ;;
	*)
	    # No sh extension, so fork subprocess.
	    $i stop
	    ;;
    esac
done

/bin/umount -a
