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

START=99
USE_PROCD=1

start_service(){
	local apphost=`iniparse -c -f /etc/.default.sys -s system -k apphost`
	if [ -z "$apphost" -o "$apphost" = "http://app.tnas.online" ]; then
		iniparse -a -f /etc/.default.sys -s system -k apphost -v https://app.terra-master.com
	fi
	chmod 644 /etc/passwd /etc/group /etc/shadow /etc/base/nasdb
    # update the php-fpm & nginx
    update_flag=/etc/base/.update.app
    if [ -f "$update_flag" ]; then
        updateAppWeb="/etc/tos/scripts/update_ui.sh"
        chmod +x "${updateAppWeb}"
        ${updateAppWeb} > /dev/null
        [ $? -eq 0 ] && rm -fr "$update_flag"
    fi

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

    # clear the flag reinstall...
    if [ -e /.eraseAcl ]; then
        rm -f /.eraseAcl
        # clear the acl for raid...
        df-json | awk '/\/mnt\/md[0-9]+$/ {print $8}' | while read line
        do
            [ ! -d "$line" ] && continue
            for name in `ls -1 $line`; do
                dir="${line}/${name}"
                [ ! -d "$dir" ] && continue;
                [ "$name" = "public" ] && continue
                [ "$name" = "appdata" ] && continue
                [ "$name" = "application" ] && continue
                [ "$name" = "@system" ] && rm -fr $dir/*
                [ "${name::1}" = "@" ] && continue
                /usr/bin/ter_setfacl -R -b $dir
            done
        done
    fi
	
    /etc/init.d/redis start >/dev/null 2>&1
	# quota check for the raid...
	quotaCheckfile="/etc/tos/cli.d/quotaCheck.php"
	[ -f "$quotaCheckfile" ] && {
		chmod +x "$quotaCheckfile"
		($quotaCheckfile > /dev/null &)
	}
	
	# finished ...
	while [ 1 ]
	do
		[ -f "/tmp/databack/complete" ] && break
		[ ! -d /tmp/databack ] && mkdir /tmp/databack
        sleep 1
		touch "/tmp/databack/complete"
	done
    
    #start msgcenter...
    /etc/init.d/msg start >/dev/null 2>&1
    TOSDeamon=`pidof TOSDeamon`
    [ -z "$TOSDeamon" ] && /etc/init.d/tosdeamon start >/dev/null 2>&1
	#service sshd start
	startService=/etc/tos/scripts/startservice
	[ -f "$startService" ] && {
		($startService > /dev/null &)
	}

	[ -f /etc/tos/scripts/sysinit ] && {
		/etc/tos/scripts/sysinit
	}

	#sudo 4755
	permission=`ls -al /usr/bin/sudo | awk '{print $1}'`
	if [ $permission != '-rwsr-xr-x' ]; then
		chmod 4755 /usr/bin/sudo
	fi
	
	# beep start
	ledtest  12 0
	sleep 1
	ledtest  12 1
	# 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
	exit 0
}
