#!/bin/sh

action="$1"
LOG_FILE="/var/log/hubvpn.log"

case "$action" in
    clear_log)
        if [ -f "$LOG_FILE" ]; then
            cat /dev/null > "$LOG_FILE"
        fi
        echo '{"code":0,"message":"ok"}'
        ;;
    status)
        pid=$(pidof hubvpn 2>/dev/null || true)
        if [ -n "$pid" ]; then
            echo "{\"running\":true,\"pid\":\"$pid\"}"
        else
            echo '{"running":false,"pid":null}'
        fi
        ;;
    restart)
        /etc/init.d/hubvpn restart >/dev/null 2>&1
        echo '{"code":0,"message":"restarted"}'
        ;;
    *)
        echo "Usage: $0 {clear_log|status|restart}" >&2
        exit 1
        ;;
esac
