File: mpd.init

package info (click to toggle)
mpd 0.11.5-3
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 5,888 kB
  • ctags: 3,092
  • sloc: ansic: 29,076; sh: 8,667; asm: 545; makefile: 224; sed: 42
file content (117 lines) | stat: -rw-r--r-- 2,612 bytes parent folder | download
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
#! /bin/sh -e
DAEMON=/usr/bin/mpd
test -x $DAEMON || exit 0
if [ -n "$NORMALPERSON_DEBUG" ]; then
	set -x
	alias ssd="start-stop-daemon -v"
else 
	alias ssd="start-stop-daemon -q"
fi
umask 0022
PATH=/sbin:/bin:/usr/sbin:/usr/bin
PIDFILE=/var/run/mpd.pid 
CONFLIB=/usr/share/mpd/conflib/libmpdconf
MPDCONF=/etc/mpd.conf

check_conf () {
	if [ ! -f "$MPDCONF" ]; then
		echo "Config file: $MPDCONF not found!"
		echo "Run  dpkg-reconfigure mpd  to create $MPDCONF."
		exit 0 
	fi
}

check_dbfile () {
	. $CONFLIB
	DBFILE=`get_mpdconf_key db_file || echo /var/lib/mpd/mpddb`
	if [ ! -f "$DBFILE" ]; then
		# create the DB if it's called by the installer
		if [ -n "$MPD_CREATE_DB" ]; then
			echo -n "MPD db at $DBFILE not found, creating"
			$DAEMON --create-db "$MPDCONF" > /dev/null 2>&1
			echo "."
		else
			echo "MPD db at $DBFILE not found, run: '$0 start-create-db'."
			exit 0
		fi
	fi
}

exit_if_running () {
	. $CONFLIB
	local runpid=`mpd_portused || true`
	if [ -n "$runpid" ]; then
		echo "MPD is already running: $runpid. '$0 restart' to restart."
		exit 0
	fi
}

mpd_start () {
	check_conf
	exit_if_running	
	check_dbfile
	mpd_start_only
}

mpd_start_only () {
	echo -n "Starting Music Player Daemon: mpd"
	ssd -x $DAEMON -b -m -p $PIDFILE --start -- --no-daemon "$MPDCONF"
	echo "."
}

mpd_stop () {
	echo -n "Stopping Music Player Daemon: mpd"
	ssd -p $PIDFILE --stop || echo -n "... MPD is not running"
	rm -f $PIDFILE
	echo "."
}

# a nag function about running as root (MPD defaults to running as 
# the user 'mpd' in Debian.
# Let users know if the recommended defaults are changed
test_mpd_root () {
	MPDUSER=`get_mpdconf_key user`
	[ -n "$MPDUSER" ] || MPDUSER=`whoami`
	if [ "$MPDUSER" = "root" ]; then
		echo "Warning: MPD doesn't need to run as root"
		echo "Put the following line in $MPDCONF:"
		echo "user \"mpd\""
		echo "And run: adduser mpd audio"
	fi
}

# note to self: don't call the non-standard args for this in
# {post,pre}{inst,rm} scripts since users are not forced to upgrade
# /etc/init.d/mpd when mpd is updated
case "$1" in
	start|stop)
		mpd_${1}
		;;
	force-start|start-create-db)
		export MPD_CREATE_DB=1
		mpd_start
		;;
	restart|reload)
		# TODO for 0.11.0: send SIGHUP to MPD to reload the conf
		mpd_stop
		mpd_start
		;;
	force-restart|force-reload)
		export MPD_CREATE_DB=1
		mpd_stop
		mpd_start
		;;
	force-stop)
		mpd_stop
		killall mpd || true
		sleep 2
		killall -9 mpd || true
		;;
	*)
		echo "Usage: /etc/init.d/mpd {start|start-create-db|stop|restart|reload}"
		echo "force- may be prefixed to any argument to force the action"
		exit 1
		;;
esac

exit 0