#!/bin/bash # $HeadURL$ $LastChangedRevision$ # Modules . $(miniade) || { echo "${0##*/}: ERROR: miniade failed (hint: run 'miniade' to see error)" >&2; exit 1; } # Configurable stuff # Other globals main() { local MY_ARGS local AUDACIOUS_PIDS PID PROGNAME # Defaults for options MODE=unset # Process options special_opts_handler() { case $1 in --server|--submit|--stop) MODE=${1#--} ;; *) return 1 ;; esac } miniade_process_options --help-handler=usage --special-opts-handler=special_opts_handler MY_ARGS "$@" && set -- "${MY_ARGS[@]}" # Process arguments # (delegated) # Sanity checks and derivations [ $MODE != unset ] || usage [ "X$LOGNAME" != X ] || miniade_error "LOGNAME: not set" miniade_get_progname PROGNAME LOCKFILE=/tmp/$PROGNAME-$LOGNAME.lock # Guts # Foster *any* audacious process owned by this user with the same DISPLAY. if ! miniade_lock_is_fresh $LOCKFILE; then AUDACIOUS_PIDS=() while read PID; do [[ $PID =~ ^[1-9][0-9]*$ ]] || continue [ -d /proc/$PID ] || continue [ "X$(stat -c %U /proc/$PID)" = "X$LOGNAME" ] || continue [ "X$(readlink /proc/$PID/exe)" = "X$(which audacious)" ] || continue [ "X$(strings /proc/$PID/environ | sed -nr 's/^DISPLAY=//p')" = "X$DISPLAY" ] || continue AUDACIOUS_PIDS+=( $PID ) done < <(ls /proc) miniade_debug 10 "main: AUDACIOUS_PIDS=( ${AUDACIOUS_PIDS[*]} )" case ${#AUDACIOUS_PIDS[*]} in 0) : ;; 1) echo ${AUDACIOUS_PIDS[0]} > $LOCKFILE ;; *) miniade_warning "ambigious fostering requirement" ;; esac fi ${MODE//-/_} "$@" } usage() { local PROGNAME miniade_get_progname PROGNAME echo "Usage: $PROGNAME [ ] { --server | --submit | --stop }" exit 0 } server() { local PROG # Process arguments [ $# = 0 ] || usage # Sanity checks and derivations miniade_validate_command audacious audtool # Guts miniade_debug 10 "server: locking ..." if ! miniade_lock $LOCKFILE; then miniade_debug 10 "server: server is already running; this is fine" return 0 fi miniade_debug 10 "server: launching audacious server ..." WRA_LAUNCHED_THIS=1 audacious > /dev/null 2>&1 & # Since *we* locked above, then we can rewrite the lock file :-) But # we need to do it atomically, which means we need to use mv. See # https://unix.stackexchange.com/questions/24395/. echo $! > $LOCKFILE.$$ mv $LOCKFILE.$$ $LOCKFILE # Let it settle. sleep 5 } submit() { local URL # Process arguments [ $# = 1 ] || usage URL=$1 # Sanity checks and derivations miniade_lock_is_fresh $LOCKFILE || miniade_error "audacious server seems not to be running" # Guts audtool --playlist-clear audtool --playlist-addurl "$URL" sleep 5 audtool --playback-play } stop() { # Process arguments [ $# = 0 ] || usage # Sanity checks and derivations # No need to send stop if it's not running miniade_lock_is_fresh $LOCKFILE || return 0 # Guts audtool --playlist-clear } main "$@"