summaryrefslogtreecommitdiff
blob: 07ddd6e67c8ad3db0f29b0626b732fb7e2c5a445 (plain)
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/sbin/runscript

opts="start stop restart slow fast info"

depend() {
	need net
}

start() {
	ebegin "Starting mldonkey"
	result=0

	if [ -z "${MLDONKEY_DIR}" ]; then
		ewarn "mldonkey's start script has been changed. You should remove"
		ewarn "BASEDIR and SUBDIR from /etc/conf.d/mldonkey and set MLDONKEY_DIR"
		ewarn "to the corect value (you propably want"
		ewarn "MLDONKEY_DIR=${BASEDIR}/${SUBDIR})"
		MLDONKEY_DIR=${BASEDIR}/${SUBDIR}
		einfo "Using ${MLDONKEY_DIR} as working directory"
	fi
	
	if [ ! -d ${MLDONKEY_DIR} ]; then
		einfo "Directory ${MLDONKEY_DIR} not existing, trying to create..."
		if `mkdir -p ${MLDONKEY_DIR} 2>/dev/null`; then 
			chown ${USER}:users ${MLDONKEY_DIR}
		else
           	eerror "Directory ${MLDONKEY_DIR} could not be created!"
           	((result++))
		fi
	fi
	
	cd ${MLDONKEY_DIR}
	env MLDONKEY_DIR=${MLDONKEY_DIR} start-stop-daemon --quiet --start \
		-c ${USER} -N ${NICE} -x /usr/bin/mlnet &>${LOG} &
	sleep 5
	if ! pgrep -u ${USER} mlnet >/dev/null
	then
		eerror "MLDonkey could not be started! Check logfile: ${LOG}"
		((result++))
	fi
	eend $result
}

stop() {
	BASE="http://"
	if [[ -n ${USERNAME} && -n ${PASSWORD} ]]
	then
		BASE=${BASE}${USERNAME}:${PASSWORD}@
	fi
	BASE=${BASE}${SERVER}:${PORT}
	ebegin "Stopping mldonkey - please wait"
	wget --spider ${BASE}/submit?q=close_fds -q 
	wget --spider ${BASE}/submit?q=save -q 
	wget --spider ${BASE}/submit?q=kill -q 
	# give it a chance to die:
	local timeout=10
	while [ $timeout -gt 0 ] && pgrep -u ${USER} mlnet &>/dev/null; do
		sleep 1
		let 'timeout--'
	done
	start-stop-daemon --oknodo --stop -x /usr/bin/mlnet &>/dev/null
	eend $?
}

restart() {
	svc_stop
	sleep 5
	svc_start
}

slow() {
	if ! service_started mldonkey
	then
		start_service mldonkey
	fi
	ebegin "Reducing bandwidth to ${LOW_DOWN}k/${LOW_UP}k"

	BASE="http://"
	if [[ -n ${USERNAME} && -n ${PASSWORD} ]]
	then
		BASE=${BASE}${USERNAME}:${PASSWORD}@
	fi
	BASE=${BASE}${SERVER}:${PORT}

	wget --spider ${BASE}/submit?q=set+max_hard_download_rate+${LOW_DOWN} -q 

	wget --spider ${BASE}/submit?q=set+max_hard_upload_rate+${LOW_UP} -q 

	eend $?
}

fast() {
	if ! service_started mldonkey
	then
		start_service mldonkey
	fi
	ebegin "Increasing bandwidth to ${HIGH_DOWN}k/${HIGH_UP}k"

	BASE="http://"
	if [[ -n ${USERNAME} && -n ${PASSWORD} ]]
	then
		BASE=${BASE}${USERNAME}:${PASSWORD}@
	fi
	BASE=${BASE}${SERVER}:${PORT}

	wget --spider ${BASE}/submit?q=set+max_hard_download_rate+${HIGH_DOWN} -q 

	wget --spider ${BASE}/submit?q=set+max_hard_upload_rate+${HIGH_UP} -q 

	eend $?
}


info() {
	if service_started mldonkey 
	then
		BASE="http://"
		if [[ -n ${USERNAME} && -n ${PASSWORD} ]]
		then
			BASE=${BASE}${USERNAME}:${PASSWORD}@
		fi

		BASE=${BASE}${SERVER}:${PORT}
		VALUE=$(/usr/bin/wget -O - ${BASE}/submit?q=vo 2> /dev/null| /bin/grep  -C1 max_hard_upload|/usr/bin/tail -n 1|/bin/cut -d\" -f2)
		if [[ ${VALUE} -eq ${LOW_UP} ]]
		then
			INFO="mldonkey runs slow"
		else
			INFO="mldonkey runs fast"
		fi
	else
		INFO="mldonkey is not running"
	fi
	einfo "$INFO"
}