#!/bin/sh

. /etc/PG.conf

RAMDISK="/usr/ramdisk"
CMD_DIR="${RAMDISK}/var/webui_pipe.d"
WEBUICTL="${RAMDISK}/app/webui/appctrl"
INSTLL_TOOL="${RAMDISK}/app/webui/bin/install_tool"

OS=`uname`


set_ssh()
{
    local args=$1

    if [ "${OS}" = "Linux" ]; then
        if [ "${args}" = "off" ]; then
            service sshd stop >/dev/null 2>&1 &
            /etc/rc.d/init.d/sshd stop >/dev/null 2>&1 &
        else
            service sshd restart >/dev/null 2>&1 &
            /etc/rc.d/init.d/sshd restart  >/dev/null 2>&1 &
        fi
    elif [ "${OS}" = "FreeBSD" ]; then
        if [ "${args}" = "off" ]; then
            /etc/rc.d/sshd onestop >/dev/null 2>&1
        else
            /etc/rc.d/sshd onerestart >/dev/null 2>&1
        fi
    fi
}


set_time()
{
    local _time="$*"

	timefmt=`echo "${_time}" | awk -v os=${OS} \
    '{
		split($1, arg1, "-");
		split($2, arg2, ":");

		if(os=="Linux")
			printf "-s %s", $0;
		else
			printf arg1[1]arg1[2]arg1[3]arg2[1]arg2[2]"."arg2[3];
	}'`

	date "${timefmt}"

	[ "${OS}" = "Linux" ] && hwclock -w
}


read_pipe_cmd()
{
    local cmd=""
    local args=""
    local cmd_file="$1"
    
    [ ! -f "${cmd_file}" ] && return

    cmd=`cat ${cmd_file} | cut -d' ' -f1`
    args=`cat ${cmd_file} | cut -d' ' -f2-`

    rm -f ${cmd_file}
    
    case "${cmd}" in
        "ssh")
            set_ssh ${args}
            ;;

        "set_sys_time")
            set_time "${args}"
            ;;

        "sys_reboot")
            ${WEBUICTL} set_power reboot >/dev/null 2>&1 &
            ;;
            
        "sys_shutdown")
            ${WEBUICTL} set_power shutdown >/dev/null 2>&1 &
            ;;
            
        "mgt_network")
            dns1=`echo "${args}" | cut -d' ' -f1`
            dns2=`echo "${args}" | cut -d' ' -f2`
            echo "nameserver ${dns1}" > /etc/resolv.conf
            echo "nameserver ${dns2}" >> /etc/resolv.conf
            ${WEBUICTL} start mgt_network >/dev/null 2>&1 &
            ;;

        "vmgt_network")
            ${WEBUICTL} start vmgt_network >/dev/null 2>&1 &
            ;;

        "ctel_vmgt")
            ${WEBUICTL} start ctel_vmgt >/dev/null 2>&1 &
            ;;

        "cact_vmgt")
            ${WEBUICTL} start cact_vmgt >/dev/null 2>&1 &
            ;;
            
        "set_time")
            set_time ${args}
            ;;

        "start_httpd")
            ${WEBUICTL} start httpd >/dev/null 2>&1 &
            ;;

        "start_monitor")
            ${RAMDISK}/bin/ipectrl stop monitor >/dev/null 2>&1
            ${RAMDISK}/bin/ipectrl start monitor >/dev/null 2>&1
            ;;

        "start_warring")
            ${WEBUICTL} start "warring" >/dev/null 2>&1 &
            ;;

        "upload_app")
            ${INSTLL_TOOL} upload_app ${args} >/dev/null 2>&1 &
            ;;

        "install_app")
            ${INSTLL_TOOL} install_app ${args} >/dev/null 2>&1 &
            ;;

        "start_app")
            ${INSTLL_TOOL} app_base_action start ${args} >/dev/null 2>&1 &
            ;;

        "stop_app")
            ${INSTLL_TOOL} app_base_action stop ${args} >/dev/null 2>&1 &
            ;;

        "enable_app")
            ${INSTLL_TOOL} app_base_action enable ${args} >/dev/null 2>&1 &
            ;;

        "disable_app")
            ${INSTLL_TOOL} app_base_action disable ${args} >/dev/null 2>&1 &
            ;;
            
        "upgrade_system")
            EVENTFILE=${PGETC}/log/pending_events
            mkdir -p ${PGETC}/log
            echo "upgrade_system yes" >> ${EVENTFILE}
            ;;

        "download_app")
            ${INSTLL_TOOL} download_app ${args} >/dev/null 2>&1 &
            ;;
            
        *)
            ;;
    esac
}


mkdir -p ${CMD_DIR}
chmod 777 ${CMD_DIR}

while true
do
    sleep 1

    for cmd_file in `find ${CMD_DIR} -type f`
    do
        read_pipe_cmd "${cmd_file}"
    done

    #chmod -R 777 ${RAMDISK}
    #chmod -R 777 ${PGETC}
    #chmod -R 777 /var/tmp/
done
