#!/bin/bash
# SMS API Service management script

case "$1" in
    start)
        echo "Starting SMS API Server..."
        nohup ~/bin/start-sms-api.sh > ~/logs/sms-api-service.log 2>&1 &
        echo $! > ~/.sms-api.pid
        echo "Service started (PID: $(cat ~/.sms-api.pid))"
        ;;
    stop)
        if [ -f ~/.sms-api.pid ]; then
            PID=$(cat ~/.sms-api.pid)
            kill $PID 2>/dev/null || true
            rm -f ~/.sms-api.pid
            echo "Service stopped"
        else
            echo "Service not running"
        fi
        ;;
    status)
        if [ -f ~/.sms-api.pid ]; then
            PID=$(cat ~/.sms-api.pid)
            if kill -0 $PID 2>/dev/null; then
                echo "Service running (PID: $PID)"
            else
                echo "Service dead (stale PID file)"
                rm -f ~/.sms-api.pid
            fi
        else
            echo "Service not running"
        fi
        ;;
    restart)
        $0 stop
        sleep 2
        $0 start
        ;;
    *)
        echo "Usage: $0 {start|stop|status|restart}"
        exit 1
        ;;
esac
