| 1 | #!/bin/sh |
| 2 | |
| 3 | # Example playlist script, by Moritz Grimm |
| 4 | # Public Domain where available. Anywhere else: all possible permissions |
| 5 | # are granted and all warranties whatsoever are disclaimed. Use at your |
| 6 | # own risk. |
| 7 | # |
| 8 | # The script plays a simple playlist, but also logs when and which new |
| 9 | # song is queued. Because it writes the entire playlist each time it is |
| 10 | # run, the script will probably not perform well with huge (500+kB) |
| 11 | # playlist files. |
| 12 | # |
| 13 | # Before using it, the configuration variables below should be adjusted |
| 14 | # appropriately. |
| 15 | # |
| 16 | # When using this script in multiple instances of ezstream, use |
| 17 | # different STATE_DIR locations or rename the script to a unique name. |
| 18 | |
| 19 | |
| 20 | ######################### |
| 21 | ## BEGIN CONFIGURATION ####################################################### |
| 22 | ######################### |
| 23 | |
| 24 | |
| 25 | # STATE_DIR |
| 26 | # Directory where this script keeps state. The directory should only be |
| 27 | # writeable by the user and nobody else (therefore, /tmp is not |
| 28 | # suitable.) |
| 29 | STATE_DIR="." |
| 30 | |
| 31 | # PLAYLIST |
| 32 | # The playlist that should be played by this script. |
| 33 | PLAYLIST="playlist.txt" |
| 34 | |
| 35 | # LOGFILE (optional) |
| 36 | # File to log song changes to. If not set, logging to file is disabled |
| 37 | # (and this script can be replaced by $PLAYLIST.) |
| 38 | LOGFILE="ezstream.log" |
| 39 | |
| 40 | # REPEAT (optional) |
| 41 | # Set to 1 to repeat the playlist indefinitely, set to 0 or leave commented |
| 42 | # out to allow ezstream to determine what to do at end-of-playlist. |
| 43 | #REPEAT=1 |
| 44 | |
| 45 | |
| 46 | ####################### |
| 47 | ## END CONFIGURATION ######################################################### |
| 48 | ####################### |
| 49 | |
| 50 | _myname="$(basename $0)" |
| 51 | |
| 52 | # Ignore SIGHUP, and exit 1 on SIGINT and SIGTERM |
| 53 | trap '' 1 |
| 54 | trap 'exit 1' 2 15 |
| 55 | |
| 56 | # Check configuration above: |
| 57 | if [ -z "${STATE_DIR}" -o ! -d "${STATE_DIR}" ]; then |
| 58 | echo "${_myname}: STATE_DIR is not configured, does not exist or is not a directory." >&2 |
| 59 | exit 1 |
| 60 | fi |
| 61 | if [ -z "${PLAYLIST}" -o ! -e "${PLAYLIST}" ]; then |
| 62 | echo "${_myname}: PLAYLIST is not configured or does not exist." >&2 |
| 63 | exit 1 |
| 64 | fi |
| 65 | test -n "${REPEAT}" || REPEAT=0 |
| 66 | |
| 67 | # Set up helper files: |
| 68 | _state="${STATE_DIR}/${_myname}.state" |
| 69 | _playlist="$(mktemp "${STATE_DIR}/${_myname}.XXXXXXXXXX")" |
| 70 | if [ $? -ne 0 ]; then |
| 71 | echo "${_myname}: Unable to create temporary file." >&2 |
| 72 | exit 1 |
| 73 | fi |
| 74 | # Delete temporary file on exit |
| 75 | trap 'rm -f ${_playlist}' 0 |
| 76 | |
| 77 | # Strip comments and empty lines from PLAYLIST, to support .m3u: |
| 78 | sed -e 's,#.*,,g' < ${PLAYLIST} | grep -v '^[[:space:]]*$' >> ${_playlist} |
| 79 | if [ $? -ne 0 ]; then |
| 80 | echo "${_myname}: Unable to prepare playlist." >&2 |
| 81 | exit 1 |
| 82 | fi |
| 83 | |
| 84 | # Create state file, if it does not exist: |
| 85 | test -f "${_state}" || touch "${_state}" |
| 86 | if [ $? -ne 0 ]; then |
| 87 | echo "${_myname}: Unable to create state file." >&2 |
| 88 | exit 1 |
| 89 | fi |
| 90 | |
| 91 | # Read current track no. from state file: |
| 92 | read _track_no < "${_state}" |
| 93 | if [ -z "${_track_no}" ]; then |
| 94 | _track_no=1 |
| 95 | fi |
| 96 | |
| 97 | # Handle the end-of-playlist case: |
| 98 | if [ ${_track_no} -gt ${_num_tracks} ]; then |
| 99 | if [ ${REPEAT} -ne 1 ]; then |
| 100 | # We're done. |
| 101 | # Output an empty line to indicate end-of-playlist: |
| 102 | echo |
| 103 | rm -f "${_state}" |
| 104 | exit 0 |
| 105 | fi |
| 106 | _track_no=1 |
| 107 | fi |
| 108 | |
| 109 | # Get the current track from the playlist: |
| 110 | _track="$(head -n ${_track_no} ${_playlist} | tail -n 1)" |
| 111 | |
| 112 | # Output: |
| 113 | echo "${_track}" |
| 114 | test -z "${LOGFILE}" || \ |
| 115 | echo "$(date '+%b %d %H:%M:%S') playlist=\"${PLAYLIST}\" no=${_track_no} track=\"${_track}\"" \ |
| 116 | >> "${LOGFILE}" |
| 117 | |
| 118 | # Increment track number and store: |
| 119 | : $((_track_no += 1)) |
| 120 | echo "${_track_no}" > "${_state}" |
| 121 | |
| 122 | exit 0 |