-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathdad
executable file
·68 lines (54 loc) · 1.5 KB
/
dad
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#! /bin/dash
usage() {
printf '%s [-h|-d DOWNLOAD_DIR] start|stop\n' "${0##*/}"
}
download_dir=${DIANA_DOWNLOAD_DIR:-${XDG_DOWNLOAD_DIR:-${HOME}}}
while getopts 'hd:' opt ; do
case "$opt" in
h)
usage
exit 0
;;
d)
download_dir=$OPTARG
;;
esac
done
shift $((OPTIND - 1))
if [ $# -ne 1 ] ; then
usage
exit 1
fi
daemon_pid=/tmp/diana-daemon.pid
case "$1" in
start)
if [ -e "$daemon_pid" ] ; then
printf '%s\n' 'The daemon is already running.' >&2
exit 1
fi
XDG_DATA_HOME=${XDG_DATA_HOME:-${HOME}/.local/share}
diana_session=${XDG_DATA_HOME}/diana.session
[ ! -e "$diana_session" ] && touch "$diana_session"
aria2c --daemon --enable-rpc --continue --dir "$download_dir" --input-file "$diana_session" --save-session "$diana_session"
pgrep -nx aria2c > "$daemon_pid"
if [ $? -ne 0 ] ; then
printf '%s\n' 'The daemon failed to start.' >&2
rm "$daemon_pid"
exit 1
fi
;;
stop)
if [ ! -e "$daemon_pid" ] ; then
printf '%s\n' 'The daemon is not running.' >&2
exit 1
fi
# kill $(cat "$daemon_pid") > /dev/null 2>&1
pkill -g $(cat "$daemon_pid") > /dev/null 2>&1
if [ $? -ne 0 ] ; then
printf '%s\n' 'Could not stop the daemon.' >&2
exit 1
else
rm "$daemon_pid"
fi
;;
esac