#! /bin/sh

RINGTONED="/usr/bin/ringtoned"
PID_FILE=/var/tmp/ringtoned.pid
PLUGINDIR="/usr/lib/ringtoned/plugins-1"
SOUNDS=~/.local/share/sounds
UNCOMPRESSED_SOUNDS=~/.local/share/sounds/ringtoned

THIS=$0

syntax(){
    echo "$THIS [-d] [start|startwait|boot|stop|restart|cleanup|clean-cache]"
    echo
    echo "start"
    echo "    Starts ringtoned and returns."
    echo "    The same as '$THIS 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."
    echo "clean-cache"
    echo "    Removes all the uncompressed wav files."
    echo
    echo "Multiple commands can be specified in sequence."
    echo "'-d' enables debugging messages in ringtoned."
}

cleanup(){
    echo "Cleaning up ringtoned files"

    if [ ! -d $SOUNDS ]; then
        echo "Sounds directory doesn't exist."
        return
    fi

    for ringtoned_file in $SOUNDS/*.wav.ringtoned; do
        if [ -e "$ringtoned_file" ]; then
            normal_file=`echo $ringtoned_file | sed 's/\.ringtoned$//'`
            echo "Restoring $normal_file"
            mv "$ringtoned_file" "$normal_file"
        fi
    done
}

clean_cache(){
    echo "Removing the cached uncompressed wav files"

    if [ ! -d $UNCOMPRESSED_SOUNDS ]; then
        echo "Uncompressed sounds directory doesn't exist."
        return
    fi

    for uncompressed_file in $UNCOMPRESSED_SOUNDS/*.wav; do
        if [ -e "$uncompressed_file" ]; then
            echo "Removing $uncompressed_file"
            rm "$uncompressed_file"
        fi
    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

    if ! ls $PLUGINDIR/*.so > /dev/null 2>&1; then
        echo "No plugins installed, not taking over the handling of ringtones"
        cleanup
        exit 0
    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

    # Nicley  ask ringtoned to quit
    kill -USR1 $RINGTONED_PID

    # Wait for it
    sleep 1
    for i in `seq 10`; do
        if [ ! -d /proc/$RINGTONED_PID ]; then
            break
        fi
        echo "Waiting for ringtoned to quit by itself"
        sleep 1
    done

    # Give up and kill it
    if [ -d /proc/$RINGTONED_PID ]; then
        echo "Ringtoned didn't quit by itself, killing it"
        kill -KILL $RINGTONED_PID
        sleep 1
    fi

    rm $PID_FILE
    echo "Stopped"

    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 [ $# -lt 1 ]; then
    syntax
    exit 1
fi

while [ "x$1" != "x" ]; do
    case "$1" in
        start)
            start
            ;;

        startwait)
            startwait
            ;;

        boot)
            boot
            ;;

        stop)
            stop
            ;;

        restart|reload)
            stop
            start
            ;;

        cleanup)
            cleanup
            ;;

        clean-cache)
            clean_cache
            ;;

        -h|--help|help)
            syntax
            ;;

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

    shift
done
