#!/bin/bash

CSA_INSTALL_DIR="/usr/local/hostguard"
DAEMON_EXE_FILE="${CSA_INSTALL_DIR}/bin/hostwatch"
DAEMON_PID_FILE="${CSA_INSTALL_DIR}/run/hostwatch.pid"
WORKER_PID_FILE="${CSA_INSTALL_DIR}/run/hostguard.pid"
CONFIG_TOOL_FILE="${CSA_INSTALL_DIR}/bin/config_tool"

SERVICE_LOG_FILE=/var/log/hostguard/csa-service.log

function write_log()
{
    cur_time=$(date "+%Y-%m-%d %H:%M:%S")
    
    if [ -f $SERVICE_LOG_FILE ];then
        log_size=`ls -l $SERVICE_LOG_FILE | awk '{ print $5 }'`
        if [ $log_size -gt 102400 ];then
            if [ -f $SERVICE_LOG_FILE.1 ];then
                rm -f $SERVICE_LOG_FILE.1
            fi
            mv $SERVICE_LOG_FILE $SERVICE_LOG_FILE.1
        fi
    else
        echo "$cur_time: start to execute csa service." > $SERVICE_LOG_FILE 2>/dev/null
    fi
    
    echo "$cur_time: "$1 >> $SERVICE_LOG_FILE 2>/dev/null
}

function check_user()
{
    if [[ ${USER} == "csa" ]]; then
        write_log "user name is NOT csa."
    fi
}

function get_hostwatch_pid()
{
    write_log "get hostwatch pid ..."
    
    if [ ! -f $DAEMON_PID_FILE ]; then
        echo 0
        return
    fi
    
    pid=$(cat ${DAEMON_PID_FILE})
    if [ $pid -gt 1 ] 2>/dev/null; then
        hostwatch_name=$(cat /proc/$pid/stat 2>/dev/null | awk '{print $2}')
        ret=$(echo $hostwatch_name | grep hostwatch | grep -v grep)
        if [[ -n $ret ]]; then
            echo $pid
            return
        fi
    fi
    
    echo 0
}

function get_hostguard_pid()
{
    write_log "get hostguard pid ..."
    
    if [ ! -f $WORKER_PID_FILE ]; then
        echo 0
        return
    fi
    
    pid=$(cat ${WORKER_PID_FILE})
    if [ $pid -gt 1 ] 2>/dev/null; then
        hostguard_name=$(cat /proc/$pid/stat 2>/dev/null | awk '{print $2}')
        ret=$(echo $hostguard_name | grep hostguard | grep -v grep)
        if [[ -n $ret ]]; then
            echo $pid
            return
        fi
    fi
    
    echo 0
}

start()
{
    ${DAEMON_EXE_FILE} --daemon --umask=027
    write_log "launch hostwatch with csa user."
}

stop()
{
    if [ -z "$(echo "$LD_LIBRARY_PATH"|grep "$CSA_INSTALL_DIR")" ];then
        export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$CSA_INSTALL_DIR/lib:$CSA_INSTALL_DIR/module
    fi
    $CONFIG_TOOL_FILE -t hostguard
}

status()
{
    if [ -z "$(echo "$LD_LIBRARY_PATH"|grep "$CSA_INSTALL_DIR")" ];then
        export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$CSA_INSTALL_DIR/lib:$CSA_INSTALL_DIR/module
    fi
    $CONFIG_TOOL_FILE -q
}


check_user

write_log "execute hostguard $1 ..."

case "$1" in
    start)
        echo "hostguard starting ..." 1>&2
        start
        sleep 1
        status
    ;;

    stop)
        echo "hostguard stopping ..." 1>&2
        stop
        sleep 1
        status
    ;;

    restart)
        echo "hostguard restarting ..." 1>&2
        stop
        sleep 1
        start
        sleep 1
        status
    ;;

    status)
        status
    ;;
    
    *)
        echo $"Usage: $0 {start|stop|restart|status}" 1>&2
        exit 1
    ;;

esac