1#!/system/bin/sh
2
3# Config and runtime directory (if changed, also change in cjdaemon and 99cjdroute)
4CJDPATH="/sdcard/cjdns"
5
6# Set the name of this script
7APPNAME="${0##*/}"
8
9# Exit with an error if the user isn't root
10if [ ! `whoami` = "root" ]; then
11    echo "Error: ${APPNAME} must be run as root"
12    exit 1
13fi
14
15# Create the daemon folder if it doesn't exist
16if [ ! -e "$CJDPATH" ]; then
17    install -d "$CJDPATH" || exit 1
18fi
19
20# Source cjdaemon.conf to load user settings if it exists
21if [ -f "$CJDPATH"/cjdaemon.conf ]; then
22    source "$CJDPATH"/cjdaemon.conf
23fi
24
25# Set $CJDCFG to the default if it wasn't set by cjdaemon.conf
26if [ -z "$CJDCFG" ]; then
27    CJDCFG="cjdroute.conf"
28fi
29
30# Create the lock file and start (or if running, restart) cjdaemon
31enable_cjdaemon() {
32    echo -n "${APPNAME}: Enabling cjdns... "
33
34    # Create the lock file, enabling cjdaemon
35    if [ ! -f "$CJDPATH"/.lock ]; then
36        touch "$CJDPATH"/.lock
37    fi
38
39    # If running, kill cjdaemon (it'll start cjdroute when restarted)
40    if [ -f "$CJDPATH"/.cjdaemon.pid ]; then
41        if [ -d /proc/`cat "$CJDPATH"/.cjdaemon.pid` ]; then
42            kill `cat "$CJDPATH"/.cjdaemon.pid`
43            sleep 1
44        fi
45        rm "$CJDPATH"/.cjdaemon.pid
46    fi
47
48    # Start cjdaemon (which will start cjdroute if the phone is awake)
49    cjdaemon &
50    sleep 1
51
52    # Exit successfully if cjdaemon started, otherwise exit with an error
53    if [ -f "$CJDPATH"/.cjdaemon.pid ]; then
54        if [ -d /proc/`cat "$CJDPATH"/.cjdaemon.pid` ]; then
55            echo "Done!"
56            exit 0
57        fi
58    fi
59    echo "Error! Couldn't start cjdaemon."
60    exit 1
61}
62
63# Remove the lock file then kill cjdaemon and cjdroute if either is running
64disable_cjdaemon() {
65    echo -n "${APPNAME}: Disabling cjdns... "
66
67    # Remove the lock file, disabling cjdaemon
68    if [ -f "$CJDPATH"/.lock ]; then
69        rm "$CJDPATH"/.lock
70    fi
71
72    # If cjdaemon is running, kill it
73    if [ -f "$CJDPATH"/.cjdaemon.pid ]; then
74        if [ -d /proc/`cat "$CJDPATH"/.cjdaemon.pid` ]; then
75            kill `cat "$CJDPATH"/.cjdaemon.pid`
76            sleep 1
77        fi
78        rm "$CJDPATH"/.cjdaemon.pid
79    fi
80
81    # If cjdroute is running, kill it
82    if [ `pgrep cjdroute | wc -l` -gt 0 ]; then
83        killall cjdroute
84        sleep 1
85
86        if [ ! `pgrep cjdroute | wc -l` -eq 0 ]; then
87            echo "Error! cjdroute still running"
88            exit 1
89        fi
90    fi
91    echo "Done!"
92}
93
94# Parse commandline arguments and behave accordingly
95case "$1" in
96    e|-e|enable|--enable)
97        enable_cjdaemon
98        ;;
99    d|-d|disable|--disable)
100        disable_cjdaemon
101        ;;
102    r|-r|restart|--restart)
103        disable_cjdaemon
104        enable_cjdaemon
105        ;;
106    *)
107        echo -e "Usage:\n\t${APPNAME} [option]\n"
108        echo -e "Options:"
109        echo -e "\te|enable: start cjdns and enable at boot"
110        echo -e "\td|disable: stop cjdns and disable at boot"
111        echo -e "\tr|restart: stop+disable, then start+enable cjdns"
112        ;;
113esac
114