#!/bin/sh
DIR=/data/capsula-custom-voice
PID=/run/capsula-custom-voice.pid
case "$1" in
start)
    [ -f "$DIR/enabled" ] || exit 0
    if [ -f "$PID" ] && kill -0 "$(cat "$PID")" 2>/dev/null; then exit 0; fi
    nohup "$DIR/voice-service" >>/tmp/capsula-custom-voice.log 2>&1 &
    echo $! > "$PID"
    ;;
stop)
    if [ -f "$PID" ]; then
        p=$(cat "$PID")
        if [ "$(readlink /proc/$p/exe)" = "$DIR/voice-service" ]; then kill "$p"; fi
        rm -f "$PID"
    fi
    ;;
restart) "$0" stop; "$0" start ;;
*) echo "Usage: $0 start|stop|restart"; exit 1 ;;
esac
