Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- #mpd/mpc manager for non root users of mpd + mpc
- #v0.3
- #built heavily from the init.d script 'mpd'
- #@depends mpd, mpc
- #Matlock
- #check to see if user has setup the placement of their config file, if not
- #prompt to do so
- if [ ! -f /home/$USER/.mpdconffile ]; then
- echo "Please enter path to mpd config file (example /home/$USER/.mpdconf)"
- read -p ">$" mpdconfpath
- echo $mpdconfpath > /home/$USER/.mpdconffile
- else
- mpdconfpath="`cat /home/$USER/.mpdconffile`"
- fi
- #this gobbledegook specifies magic things
- dbfile=$(sed -n 's/^[[:space:]]*db_file[[:space:]]*"\?\([^"]*\)\"\?/\1/p' $mpdconfpath)
- pidfile=$(sed -n 's/^[[:space:]]*pid_file[[:space:]]*"\?\([^"]*\)\"\?/\1/p' $mpdconfpath)
- #Begin function block
- #return PID of process if running
- function mpd_pid {
- if [ -f /home/$USER/.mpd/pid ]; then
- pid=`cat /home/$USER/.mpd/pid`
- else
- pid=`ps aux |grep mpd |grep -v grep |grep -v man |grep -v pts |tr -s [:space:] |cut -d' ' -f2`
- fi
- }
- #start the daemon
- function start_mpd {
- echo -n "Now starting mpd..."
- if [ "$FORCE_CREATE_DB" ]; then
- mv $dbfile $dbfile-old
- fi
- mpd
- sleep 2
- mpd_pid
- echo "mpd started with pid $pid"
- }
- #attempt to stop the daemon normally if it refuses, sigterm
- function stop_mpd {
- mpd_pid
- echo -n "Now killing mpd process $pid..."
- mpd --kill
- sleep 1
- if [ "`ps aux |grep $pid |grep -v grep`" ]; then
- echo -n "it didn't die normally, now executing..."
- kill -9 $pid
- fi
- echo "mpd should be dead."
- sleep 1
- }
- function update {
- mpc update >> /dev/null
- echo "Now updating database, if mpd stalls, please issue $0 restart"
- }
- function playlist {
- clear
- echo 'Available Playlists:'
- declare -a array
- IN=`mpc lsplaylists`
- set -- "$IN"
- IFS=$'\n'; declare -a array=($*)
- j=0
- for i in "${array[@]}"; do
- echo $(($j+1))": "${array[$j]}
- let " j = $j + 1"
- done
- echo ------------------------------
- echo -n "Plese enter your selection and press enter (1-"$(($j))"): "
- read confirm
- if [ $confirm -gt $j ] || [[ ! $confirm = *[[:digit:]]* ]]; then
- exit 1
- else
- selection=${array[$(($confirm-1))]}
- echo "Now replacing playlist, if mpd stalls, press $0 restart"
- mpc clear >> /dev/null
- mpc load "$selection" >> /dev/null
- mpc play >> /dev/null
- sleep 3
- np
- fi
- }
- #End function block
- #here is where the actual parameters that are passed are evaluated.
- case "$1" in
- start)
- start_mpd
- ;;
- stop)
- stop_mpd
- ;;
- restart)
- stop_mpd
- start_mpd
- ;;
- create-db)
- FORCE_CREATE_DB=1
- start_mpd
- ;;
- pid)
- mpd_pid
- echo "mpd process id is $pid"
- ;;
- update)
- update
- ;;
- playlist)
- playlist
- ;;
- *)
- echo "Usage: $0 {create-db|update|start|stop|restart|playlist|pid}"
- exit 2
- ;;
- esac
Advertisement
Add Comment
Please, Sign In to add comment