#!/bin/sh
# chkconfig: 2345 99 10
# description: Daemon Nativo DDNS em Go

EXEC="/sbin/ddnsd"
PIDFILE="/var/run/ddns-clients/ddns-clients.pid"

case "$1" in
    start)
        if [ -f $PIDFILE ] && kill -0 $(cat $PIDFILE) 2>/dev/null; then
            echo "DDNS Go Daemon já está em execução."
        else
            echo "Iniciando DDNS Go Daemon..."
            $EXEC > /dev/null 2>&1 &
            echo $! > $PIDFILE
            echo "DDNS Daemon iniciado [PID: $(cat $PIDFILE)]."
        fi
        ;;
    stop)
        if [ -f $PIDFILE ]; then
            echo "Parando DDNS Go Daemon..."
            kill -TERM $(cat $PIDFILE) 2>/dev/null
            rm -f $PIDFILE
            echo "Parado com sucesso."
        else
            echo "DDNS Go Daemon não está rodando."
        fi
        ;;
    restart)
        $0 stop
        sleep 1
        $0 start
        ;;
    status)
        if [ -f $PIDFILE ] && kill -0 $(cat $PIDFILE) 2>/dev/null; then
            echo "DDNS Go Daemon rodando [PID: $(cat $PIDFILE)]"
        else
            echo "DDNS Go Daemon parado."
        fi
        ;;
    *)
        echo "Uso: $0 {start|stop|restart|status}"
        exit 1
        ;;
esac
