Clean up the DHT config, making K and HASH_LENGTH constants instead.
[quix0rs-apt-p2p.git] / debian / init.d
1 #!/bin/sh
2
3 ### BEGIN INIT INFO
4 # Provides:          apt-p2p
5 # Required-Start:    $network
6 # Required-Stop:
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=aptp2p
28 group=nogroup
29
30 [ -r /etc/default/apt-p2p ] && . /etc/default/apt-p2p
31
32 test -x $twistd || exit 0
33 test -r $application || exit 0
34
35 # return true if at least one pid is alive
36 alive()
37 {
38     if [ -z "$*" ]; then
39         return 1
40     fi
41     for i in $*; do
42         if kill -0 $i 2> /dev/null; then
43             return 0
44         fi
45     done
46
47     return 1
48 }
49
50
51 case "$1" in
52     start)
53         echo -n "Starting apt-p2p"
54         [ ! -d $rundir ] && mkdir $rundir
55         [ ! -f $logfile ] && touch $logfile
56         chown $user $rundir $logfile 
57         [ -f $pidfile ] && chown $user $pidfile
58         # Make cache files readable
59         umask 022
60         start-stop-daemon --start --quiet --exec $twistd -- \
61             --pidfile=$pidfile --rundir=$rundir --python=$application \
62             --logfile=$logfile --no_save
63         echo "."        
64     ;;
65
66     stop)
67         echo -n "Stopping apt-p2p"
68         start-stop-daemon --stop --quiet --pidfile $pidfile
69         #
70         # Continue stopping until daemon finished or time over
71         #
72         count=0
73         pid=$(cat $pidfile 2>/dev/null)
74         while alive $pid; do
75                 if [ $count -gt 20 ]; then
76                         echo -n " aborted"
77                         break;
78                 elif [ $count = 1 ]; then
79                         echo -n " [wait $count"
80                 elif [ $count -gt 1 ]; then
81                         echo -n " $count"
82                 fi
83                 count=$(expr $count + 1)
84                 sleep 1
85                 start-stop-daemon --stop --quiet --pidfile $pidfile
86         done
87         if [ $count -gt 1 ]; then
88                 echo -n "]"
89         fi
90         echo "."        
91     ;;
92
93     restart)
94         $0 stop
95         $0 start
96     ;;
97     
98     force-reload)
99         $0 restart
100     ;;
101
102     *)
103         echo "Usage: /etc/init.d/apt-p2p {start|stop|restart|force-reload}" >&2
104         exit 1
105     ;;
106 esac
107
108 exit 0