matlock

/home/$USER/bin/mp

Dec 7th, 2012
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.94 KB | None | 0 0
  1. #!/bin/bash
  2. #mpd/mpc manager for non root users of mpd + mpc
  3. #v0.3
  4. #built heavily from the init.d script 'mpd'
  5. #@depends mpd, mpc
  6. #Matlock
  7.  
  8.  
  9. #check to see if user has setup the placement of their config file, if not
  10. #prompt to do so
  11. if [ ! -f /home/$USER/.mpdconffile ]; then
  12. echo "Please enter path to mpd config file (example /home/$USER/.mpdconf)"
  13. read -p ">$" mpdconfpath
  14. echo $mpdconfpath > /home/$USER/.mpdconffile
  15. else
  16. mpdconfpath="`cat /home/$USER/.mpdconffile`"
  17. fi
  18.  
  19. #this gobbledegook specifies magic things
  20. dbfile=$(sed -n 's/^[[:space:]]*db_file[[:space:]]*"\?\([^"]*\)\"\?/\1/p' $mpdconfpath)
  21. pidfile=$(sed -n 's/^[[:space:]]*pid_file[[:space:]]*"\?\([^"]*\)\"\?/\1/p' $mpdconfpath)
  22.  
  23.  
  24. #Begin function block
  25.  
  26. #return PID of process if running
  27. function mpd_pid {
  28. if [ -f /home/$USER/.mpd/pid ]; then
  29. pid=`cat /home/$USER/.mpd/pid`
  30. else
  31. pid=`ps aux |grep mpd |grep -v grep |grep -v man |grep -v pts |tr -s [:space:] |cut -d' ' -f2`
  32. fi
  33. }
  34.  
  35. #start the daemon
  36. function start_mpd {
  37. echo -n "Now starting mpd..."
  38. if [ "$FORCE_CREATE_DB" ]; then
  39. mv $dbfile $dbfile-old
  40. fi
  41. mpd
  42. sleep 2
  43. mpd_pid
  44. echo "mpd started with pid $pid"
  45. }
  46.  
  47. #attempt to stop the daemon normally if it refuses, sigterm
  48. function stop_mpd {
  49. mpd_pid
  50. echo -n "Now killing mpd process $pid..."
  51. mpd --kill
  52. sleep 1
  53. if [ "`ps aux |grep $pid |grep -v grep`" ]; then
  54. echo -n "it didn't die normally, now executing..."
  55. kill -9 $pid
  56. fi
  57. echo "mpd should be dead."
  58. sleep 1
  59. }
  60.  
  61. function update {
  62. mpc update >> /dev/null
  63. echo "Now updating database, if mpd stalls, please issue $0 restart"
  64. }
  65.  
  66. function playlist {
  67. clear
  68. echo 'Available Playlists:'
  69. declare -a array
  70. IN=`mpc lsplaylists`
  71. set -- "$IN"
  72. IFS=$'\n'; declare -a array=($*)
  73. j=0
  74. for i in "${array[@]}"; do
  75. echo $(($j+1))": "${array[$j]}
  76. let " j = $j + 1"
  77. done
  78.  
  79.  
  80. echo ------------------------------
  81. echo -n "Plese enter your selection and press enter (1-"$(($j))"): "
  82. read confirm
  83. if [ $confirm -gt $j ] || [[ ! $confirm = *[[:digit:]]* ]]; then
  84. exit 1
  85. else
  86. selection=${array[$(($confirm-1))]}
  87. echo "Now replacing playlist, if mpd stalls, press $0 restart"
  88. mpc clear >> /dev/null
  89. mpc load "$selection" >> /dev/null
  90. mpc play >> /dev/null
  91. sleep 3
  92. np
  93. fi
  94. }
  95. #End function block
  96.  
  97.  
  98. #here is where the actual parameters that are passed are evaluated.
  99. case "$1" in
  100. start)
  101. start_mpd
  102. ;;
  103. stop)
  104. stop_mpd
  105. ;;
  106. restart)
  107. stop_mpd
  108. start_mpd
  109. ;;
  110. create-db)
  111. FORCE_CREATE_DB=1
  112. start_mpd
  113. ;;
  114. pid)
  115. mpd_pid
  116. echo "mpd process id is $pid"
  117. ;;
  118. update)
  119. update
  120. ;;
  121. playlist)
  122. playlist
  123. ;;
  124. *)
  125. echo "Usage: $0 {create-db|update|start|stop|restart|playlist|pid}"
  126. exit 2
  127. ;;
  128. esac
Advertisement
Add Comment
Please, Sign In to add comment