#!/bin/sh
# Start devtmpfs, create device node
# Start all init scripts in /etc/init.d
# executing them in numerical order.
# start service byroot
[ ! -d /tmp/TOS_CONFIG ] && mkdir /tmp/TOS_CONFIG

# init var dir...
[ -f /etc/tos/scripts/powermanager ] && /etc/tos/scripts/powermanager

# update the php-fpm & nginx
update_flag=/etc/base/.update.app
[ -f "$update_flag" ] && {
    update_ui="/etc/tos/scripts/update_ui.sh"
    chmod +x "${update_ui}"
    ${update_ui} >/dev/null
    [ $? -eq 0 ] && rm -fr "$update_flag"
}
# sync time from hard time
[ -f /etc/tos/scripts/timesync ] && /etc/tos/scripts/timesync

for i in /etc/init.d/S??*; do
    # Ignore dangling symlinks (if any).
    [ ! -f "$i" ] && continue
    echo $i | grep ".en$" >/dev/null
    [ $? -eq 0 ] && continue

    case "$i" in
    *.sh)
        # Source shell script for speed.
        (
          trap - INT QUIT TSTP
          set start
          . $i
        )
        ;;
    *)
        # No sh extension, so fork subprocess.
        shellname=$(basename "$i")
        if [ "$shellname" = "S40network" ]; then
          total=1
          while [ $total -le 10 ]; do
            sleep 1
            echo "*****************************************************************[ $total ]"
            dmesg | awk '/scst_register_device/' >/dev/null
            [ $? -eq 0 ] && break
            let total=total+1
          done
          cat /proc/net/dev
          echo "*****************************************************************"
        fi
        $i start
        ;;
    esac
done

#delete the grub usb
[ -f /etc/init.d/mountusb ] && /etc/init.d/mountusb stop
#start acpid
[ -f /sbin/acpid ] && /sbin/acpid
# sleep for disks
spindown=$(iniparse -c -f /etc/.default.sys -s system -k spindown)
[ "X${spindown}" = "X1" ] && spindown=8
[ "X${spindown}" = "X" ] && spindown=100000

#start system fan control
F8=$(dmidecode | grep "F842")
TNAS_SERVER=$(dmidecode | grep tnas-server)
TNAS_CPU=`cat /proc/cpuinfo | grep C3538`
if [ ! -z "$F8" ]; then
    /usr/sbin/f8ctl /tmp/sys_log ${spindown} >/dev/null 2>&1 &
elif [ ! -z "${TNAS_SERVER}" -o ! -z "$TNAS_CPU" ]; then
    /etc/init.d/smartfan start
else
    /usr/sbin/syscontrol /tmp/sys_log ${spindown} >/dev/null 2>&1 &
fi
[ -z "$(pidof nginx)" ] && service nginx start >/dev/null 2>&1
[ -z "$(pidof php-fpm)" ] && service php-fpm start >/dev/null 2>&1
[ -z "$(pidof TOSDeamon)" ] && service tosdeamon start >/dev/null 2>&1

# mount user data for system
/etc/tos/scripts/mntdata

# quota check for the raid...
quotaCheckfile="/etc/tos/cli.d/quotaCheck.php"
if [ -f "$quotaCheckfile" ]; then
    chmod +x "$quotaCheckfile"
    ($quotaCheckfile >/dev/null &)
fi

REDIS=$(pidof redis-server)
[ -z "$REDIS" ] && service redis start >/dev/null
# finished ...
while [ 1 ]; do
    [ -f "/tmp/databack/complete" ] && break
    sleep 1
    touch "/tmp/databack/complete"
done

buzzer_config=/etc/buzzer/buzzer_config.info
if [ -e $buzzer_config ]; then
    status=$(awk -F '=' '/''/{a=1}a==1&&$1~/'system_boot'/{print $2;exit}' $buzzer_config)
    [ "$status" = '1' ] && buzzer all 1 6
else
    buzzer all 1 6
fi

startService=/etc/tos/scripts/startservice
[ -f "$startService" ] && ($startService >/dev/null &)

[ -f /etc/tos/scripts/sysinit ] && (/etc/tos/scripts/sysinit &)

# system is ok
ps ax | grep /etc/init.d/nas/mdadm-monitor | grep -v grep
if [ $? -ne 0 ]; then
    mdadm --monitor --scan --daemonise --syslog --delay=10 --program=/etc/init.d/nas/mdadm-monitor
fi
