#!/bin/bash # $HeadURL$ $LastChangedRevision$ # Modules . $(miniade) || { echo "${0##*/}: ERROR: miniade failed (hint: run 'miniade' to see error)" >&2; exit 1; } # Configurable stuff TIMEOUT=5 # Other globals main() { local MY_ARGS local 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 FIFO=$HOME/.mplayer/remote # Guts ${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 mplayer # 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: recreating fifo ..." rm -f $FIFO mkdir -p ${FIFO%/*} mkfifo $FIFO miniade_debug 10 "server: launching mplayer server ..." WRA_LAUNCHED_THIS=1 mplayer -prefer-ipv4 -nolirc -really-quiet -slave -idle -noconsolecontrols -input file=$FIFO & # 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 "mplayer server seems not to be running" # Guts miniade_debug 10 "submit: sending 'loadfile $URL' ..." timeout $TIMEOUT sh -c "echo \"loadfile $URL\" > $FIFO" || miniade_error "timeout writing to fifo (hint: is the mplayer server actually running?)" } 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 miniade_debug 10 "stop: sending 'stop' ..." timeout $TIMEOUT sh -c "echo stop > $FIFO" || miniade_error "timeout writing to fifo (hint: is the mplayer server actually running?)" } main "$@"