Add an enable variable to the /etc/default file (Closes: #518594)
[quix0rs-apt-p2p.git] / debian / init.d
1 #!/bin/sh
2
3 ### BEGIN INIT INFO
4 # Provides:          apt-p2p
5 # Required-Start:    $remote_fs $network
6 # Required-Stop:     $remote_fs
7 # Should-Start:      $named
8 # Should-Stop:
9 # Default-Start:     2 3 4 5
10 # Default-Stop:      0 1 6
11 # Short-Description: apt helper for peer-to-peer downloads of Debian packages
12 # Description:       Apt-p2p is a helper for downloading Debian packages
13 #                    files with APT. It will download any needed files from
14 #                    other Apt-p2p peers in a bittorrent-like manner, and so
15 #                    reduce the strain on the Debian mirrors.
16 ### END INIT INFO
17
18 # /etc/init.d/apt-p2p: start and stop the apt-p2p daemon
19
20 PATH=/sbin:/bin:/usr/sbin:/usr/bin
21
22 rundir=/var/run/apt-p2p/ 
23 pidfile=$rundir/apt-p2p.pid 
24 logfile=/var/log/apt-p2p.log
25 application=/usr/sbin/apt-p2p
26 twistd=/usr/bin/twistd
27 user=apt-p2p
28 group=nogroup
29 enable=true
30
31 [ -r /etc/default/apt-p2p ] && . /etc/default/apt-p2p
32
33 test -x $twistd || exit 0
34 test -r $application || exit 0
35
36 case "x$enable" in
37     xtrue|xfalse)   ;;
38     *)              echo -n "Value of 'enable' in /etc/default/apt-p2p must be either 'true' or 'false'; "
39                     echo "not starting apt-p2p daemon."
40                     exit 1
41                     ;;
42 esac
43
44 # return true if at least one pid is alive
45 alive()
46 {
47     if [ -z "$*" ]; then
48         return 1
49     fi
50     for i in $*; do
51         if kill -0 $i 2> /dev/null; then
52             return 0
53         fi
54     done
55
56     return 1
57 }
58
59
60 case "$1" in
61     start)
62         if "$enable"; then
63             echo -n "Starting apt-p2p"
64             [ ! -d $rundir ] && mkdir $rundir
65             [ ! -f $logfile ] && touch $logfile
66             chown $user $rundir $logfile 
67             [ -f $pidfile ] && chown $user $pidfile
68             # Make cache files readable
69             umask 022
70             start-stop-daemon --start --quiet --exec $twistd -- \
71                 --pidfile=$pidfile --rundir=$rundir --python=$application \
72                 --logfile=$logfile --no_save
73             echo "."        
74         else
75             echo "apt-p2p daemon not enabled in /etc/default/apt-p2p, not starting..."
76         fi
77     ;;
78
79     stop)
80         echo -n "Stopping apt-p2p"
81         start-stop-daemon --stop --quiet --pidfile $pidfile
82         #
83         # Continue stopping until daemon finished or time over
84         #
85         count=0
86         pid=$(cat $pidfile 2>/dev/null)
87         while alive $pid; do
88                 if [ $count -gt 20 ]; then
89                         echo -n " aborted"
90                         break;
91                 elif [ $count = 1 ]; then
92                         echo -n " [wait $count"
93                 elif [ $count -gt 1 ]; then
94                         echo -n " $count"
95                 fi
96                 count=$(expr $count + 1)
97                 sleep 1
98                 start-stop-daemon --stop --quiet --pidfile $pidfile
99         done
100         if [ $count -gt 1 ]; then
101                 echo -n "]"
102         fi
103         echo "."        
104     ;;
105
106     restart)
107         $0 stop
108         $0 start
109     ;;
110     
111     force-reload)
112         $0 restart
113     ;;
114
115     *)
116         echo "Usage: /etc/init.d/apt-p2p {start|stop|restart|force-reload}" >&2
117         exit 1
118     ;;
119 esac
120
121 exit 0