#!/system/bin/sh

die() { printf "Error: %s\n" "$*"; exit 1; }

# 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
    die "${APPNAME} must be run as root"
fi

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

# Source cjdaemon.conf to load user settings if it exists
if [ -f "$CJDPATH"/cjdaemon.conf ]; then
    . "$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

cjdaemon_pid="$CJDPATH"/.cjdaemon.pid

# Create the lock file and start (or if running, restart) cjdaemon
enable_cjdaemon() {
    printf "%s: Enabling cjdns... " "$APPNAME"

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

    # If running, kill cjdaemon (it'll start cjdroute when restarted)
    if [ -f "$cjdaemon_pid" ]; then
        if [ -d "/proc/$(cat "$cjdaemon_pid")" ]; then
            kill "$(cat "$cjdaemon_pid")"
            sleep 1
        fi
        rm -rf -- "$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 "$cjdaemon_pid" ]; then
        if [ -d "/proc/$(cat "$cjdaemon_pid")" ]; then
            printf "Done!\n"
            exit 0
        fi
    fi
    die "Couldn't start cjdaemon."
}

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

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

    # If cjdaemon is running, kill it
    if [ -f "$cjdaemon_pid" ]; then
        if [ -d "/proc/$(cat "$cjdaemon_pid")" ]; then
            kill "$(cat "$cjdaemon_pid")"
            sleep 1
        fi
        rm -rf -- "$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
            die "cjdroute still running."
        fi
    fi
    printf "Done!\n"
}

# 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
        ;;
    *)
        printf "Usage:\n\t%s [option]\n\n" "$APPNAME"
        printf "Options:\n"
        printf "\te|enable: start cjdns and enable at boot\n"
        printf "\td|disable: stop cjdns and disable at boot\n"
        printf "\tr|restart: stop+disable, then start+enable cjdns\n"
        ;;
esac
