#! /bin/sh

RINGTONED=ringtoned
PID_FILE=/var/tmp/ringtoned.pid

THIS=$0

syntax(){
    echo "$0 [start|startwait|boot|stop|restart|cleanup]"
    echo
    echo "start"
    echo "    Starts ringtoned and returns."
    echo "    The same as '$0 startwait &'."
    echo "startwait"
    echo "    Starts ringtoned, waits until it exits and then cleans up the"
    echo "    ringtone files."
    echo "boot"
    echo "    Same as start, but doesn't start if ringtoned was disabled by"
    echo "    creating '~/.config/ringtoned/disabled'."
    echo "    Used by scripts run at boot time."
    echo "stop"
    echo "    Stops the running ringtoned started with the start command and"
    echo "    makes sure to clean up the ringtones."
    echo "restart"
    echo "    Stops ringtoned (if it's running) and restarts it."
    echo "cleanup"
    echo "    Cleans up the ringtones used by ringtoned, thus restoring the"
    echo "    default behaviour."
}

cleanup(){
    echo "Cleaning up ringtoned files"

    SOUNDS=~/.local/share/sounds
    if [ ! -d $SOUNDS ]; then
        echo "Sounds directory doesn't exist."
        return
    fi

    for ringtoned_file in `ls $SOUNDS/*.wav.ringtoned 2> /dev/null`; do
        normal_file=`echo $ringtoned_file | sed 's/\.ringtoned$//'`
        echo "Restoring $normal_file"
        mv $ringtoned_file $normal_file
    done
}

sighandler(){
    echo "SIGINT received"
    stop
}

startwait(){
    echo "Starting ringtoned"

    if [ -e $PID_FILE ]; then
        RINGTONED_PID=`cat $PID_FILE 2> /dev/null`
        if [ -e /proc/$RINGTONED_PID/cmdline ]; then
            echo "Ringtoned is already running with pid $RINGTONED_PID"
            exit 1
        else
            rm $PID_FILE
        fi
    fi

    $RINGTONED &
    RINGTONED_PID=$!
    echo $RINGTONED_PID > $PID_FILE
    echo "Started ringtoned with pid $RINGTONED_PID"

    trap sighandler SIGINT
    wait $RINGTONED_PID
    trap - SIGINT
    echo "Ringtoned terminated with exit code $?"
    cleanup
}

start(){
    $THIS startwait &
    sleep 1
}

boot(){
    if [ -e ~/.config/ringtoned/disabled ]; then
        echo "Ringtoned is disabled, not running automatically"
        exit 0
    fi

    start
}

stop(){
    echo "Stopping ringtoned"

    RINGTONED_PID=`cat $PID_FILE 2> /dev/null`
    if [ -z $RINGTONED_PID ]; then
        echo "Ringtoned doesn't seem to be running"
        cleanup
        return
    fi

    kill -9 $RINGTONED_PID
    rm $PID_FILE
    echo "Stopped"

    sleep 1
    cleanup
}

if [ `id -u` = "0" ]; then
    echo "You cannot control ringtoned as root!" >&2
    exit 1
fi

if [ "x$1" = "x-d" ]; then
    export RINGTONED_DEBUG=true
    shift
fi

if [ $# != 1 ]; then
    syntax
    exit 1
fi

case "$1" in
    start)
        start
        ;;

    startwait)
        startwait
        ;;

    boot)
        boot
        ;;

    stop)
        stop
        ;;

    restart|reload)
        stop
        start
        ;;

    cleanup)
        cleanup
        ;;

    -h|--help|help)
        syntax
        exit 0
        ;;

    *)
        echo "Unknown command '$1'" >&2
        echo >&2
        syntax
        exit 1
        ;;
esac
