#!/bin/sh
# Smart-proxy init script
# Starts TLS proxy before dashd (S80) for stratum+ssl:// support

PROXY=/usr/bin/smart-proxy
PIDFILE=/var/run/smart-proxy.pid
LOG=/tmp/smart-proxy.log

start() {
    echo -n "Starting smart-proxy: "
    if [ -f "$PIDFILE" ] && kill -0 $(cat "$PIDFILE") 2>/dev/null; then
        echo "already running"
        return
    fi

    "$PROXY" >> "$LOG" 2>&1 &
    echo $! > "$PIDFILE"
    echo "OK"
}

stop() {
    echo -n "Stopping smart-proxy: "
    if [ -f "$PIDFILE" ]; then
        kill $(cat "$PIDFILE") 2>/dev/null
        rm -f "$PIDFILE"
    fi
    killall smart-proxy 2>/dev/null
    echo "OK"
}

case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    restart|reload)
        stop
        sleep 1
        start
        ;;
    *)
        echo "Usage: $0 {start|stop|restart}"
        exit 1
esac

exit 0
