Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2017
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.89 KB | None | 0 0
  1. #!/bin/bash
  2. #by Csandriel
  3. #22.02.17
  4.  
  5. #plays radio and can save track/artist data for later downloading
  6. #uses mpg123 for mp3 streaming
  7.  
  8. #username, will be used to launch the script, if it called by root.
  9. #To avoid ussues with permissions
  10. USER=user
  11. #keep track of all played traks ^_^. Must have liberal permissions
  12. #items for the best-list is taken from the tail of this "journal"
  13. LOG="/home/bin/.mus.log"
  14. #these files too must be accesible for writing to everyone
  15. #best list
  16. BEST="/store0/mp3/xes-bestmusic.log"
  17. #will run in background. Need a reference to kill with.
  18. PIDFILE="/home/bin/.xes.pid"
  19.  
  20. #radio address:port. May be extracted from playlist distributed by station.
  21. #TODO:add an option to switch stantions. Maybe from configfile.
  22. ADDRESS="http://46.163.111.34:8030"
  23.  
  24. USAGE_MSG="Invalid argument! \n Usage: xes [-k|-s|-l|-clear] \n  
  25. where \n -k -- kill; \n -s -- save to best-list \n -l -- roll-out best-list
  26. -clear -- clear best-list"
  27. RESTART_MSG=\'"$PIDFILE' exists. It seems xes have been launched yet. Killing/Cleaning/Restart. \n Log-file will be reseted."
  28.  
  29. #if launched as root then restart script as user
  30. if [ $UID -eq 0 ] ; then echo "DROP ROOT & recurse"; echo "$0 $@"; su $USER -c "$0 $@"; exit; fi
  31.  
  32.  
  33. dokill () {
  34.     cat "$PIDFILE"|xargs kill; rm "$PIDFILE" "$LOG"
  35.     }
  36.  
  37. case $1 in
  38.  
  39.     -k) if [ -f $PIDFILE ] ; then dokill ; else echo "xes is down. There is left only to kill yourself"; fi
  40.         ;;
  41.     -s) cat $LOG|tail -n1|sed "s_.*Title=\([^;]*\).*_\1_" >> $BEST 
  42.         ;;
  43.     -l) cat "$BEST"            
  44.         ;;
  45.     -clear) echo "BEST-LIST WILL BE CLEANCED! Are You sure? [y/<any key>]"
  46.         read r; if [ "$r" == "y" ] ; then > "$BEST" ; else exit 0; fi          
  47.         ;;
  48.     *) if [ $# -eq 0 ]  
  49.         then
  50.         if [ -f $PIDFILE ]
  51.                 then echo -e $RESTART_MSG; dokill;
  52.         fi
  53.         mpg123 "$ADDRESS" &> $LOG & PID=$!  
  54.         echo "$PID" > "$PIDFILE"
  55.  
  56.  
  57.      else echo -e "$USAGE_MSG"
  58.      exit 1
  59.      fi
  60.      ;;
  61. esac
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement