#!/system/bin/sh

# Config and runtime directory (if changed, also change in cjdaemon and 99cjdroute)
CJDPATH="/sdcard/cjdns"

# Set the name of this script
APPNAME="${0##*/}"

# Exit with an error if the user isn't root
if [ ! `whoami` = "root" ]; then
    echo "Error: ${APPNAME} must be run as root"
    exit 1
fi

# Create the daemon folder if it doesn't exist
if [ ! -e "$CJDPATH" ]; then
    install -d "$CJDPATH" || exit 1
fi

# Source cjdaemon.conf to load user settings if it exists
if [ -f "$CJDPATH"/cjdaemon.conf ]; then
    source "$CJDPATH"/cjdaemon.conf
fi

# Set $CJDCFG to the default if it wasn't set by cjdaemon.conf
if [ -z "$CJDCFG" ]; then
    CJDCFG="cjdroute.conf"
fi

# Create the lock file and start (or if running, restart) cjdaemon
enable_cjdaemon() {
    echo -n "${APPNAME}: Enabling cjdns... "

    # Create the lock file, enabling cjdaemon
    if [ ! -f "$CJDPATH"/.lock ]; then
        touch "$CJDPATH"/.lock
    fi

    # If running, kill cjdaemon (it'll start cjdroute when restarted)
    if [ -f "$CJDPATH"/.cjdaemon.pid ]; then
        if [ -d /proc/`cat "$CJDPATH"/.cjdaemon.pid` ]; then
            kill `cat "$CJDPATH"/.cjdaemon.pid`
            sleep 1
        fi
        rm "$CJDPATH"/.cjdaemon.pid
    fi

    # Start cjdaemon (which will start cjdroute if the phone is awake)
    cjdaemon &
    sleep 1

    # Exit successfully if cjdaemon started, otherwise exit with an error
    if [ -f "$CJDPATH"/.cjdaemon.pid ]; then
        if [ -d /proc/`cat "$CJDPATH"/.cjdaemon.pid` ]; then
            echo "Done!"
            exit 0
        fi
    fi
    echo "Error! Couldn't start cjdaemon."
    exit 1
}

# Remove the lock file then kill cjdaemon and cjdroute if either is running
disable_cjdaemon() {
    echo -n "${APPNAME}: Disabling cjdns... "

    # Remove the lock file, disabling cjdaemon
    if [ -f "$CJDPATH"/.lock ]; then
        rm "$CJDPATH"/.lock
    fi

    # If cjdaemon is running, kill it
    if [ -f "$CJDPATH"/.cjdaemon.pid ]; then
        if [ -d /proc/`cat "$CJDPATH"/.cjdaemon.pid` ]; then
            kill `cat "$CJDPATH"/.cjdaemon.pid`
            sleep 1
        fi
        rm "$CJDPATH"/.cjdaemon.pid
    fi

    # If cjdroute is running, kill it
    if [ `pgrep cjdroute | wc -l` -gt 0 ]; then
        killall cjdroute
        sleep 1

        if [ ! `pgrep cjdroute | wc -l` -eq 0 ]; then
            echo "Error! cjdroute still running"
            exit 1
        fi
    fi
    echo "Done!"
}

# Parse commandline arguments and behave accordingly
case "$1" in
    e|-e|enable|--enable)
        enable_cjdaemon
        ;;
    d|-d|disable|--disable)
        disable_cjdaemon
        ;;
    r|-r|restart|--restart)
        disable_cjdaemon
        enable_cjdaemon
        ;;
    *)
        echo -e "Usage:\n\t${APPNAME} [option]\n"
        echo -e "Options:"
        echo -e "\te|enable: start cjdns and enable at boot"
        echo -e "\td|disable: stop cjdns and disable at boot"
        echo -e "\tr|restart: stop+disable, then start+enable cjdns"
        ;;
esac
