#!/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=/tmp/$PROGNAME-$LOGNAME.fifo # 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 vlc # Guts } submit() { local URL # Process arguments [ $# = 1 ] || usage URL=$1 # Sanity checks and derivations # Guts stop # Start it as detached as possible, like a daemon. bash -c "cd / && WRA_STARTED_THIS=1 ffplay -loglevel warning -nodisp \"$URL\" > /dev/null 2>&1 < /dev/null &" } stop() { # Process arguments [ $# = 0 ] || usage # Sanity checks and derivations # Guts # Collect pids of all ffplay processes that wra started for us. WRA_STARTED_FFPLAY_PIDS=() for PID_DIR in /proc/*; do miniade_debug 20 "stop: $PID_DIR: considering ..." [[ $PID_DIR =~ ^/proc/([1-9][0-9]*)$ ]] || { miniade_debug 20 "stop: $PID_DIR: rejecting as non-numeric ..."; continue; } # discard output from stat failing to access too-short-lived pids. [ "X$(stat -c %U $PID_DIR 2>/dev/null)" = "X$LOGNAME" ] || { miniade_debug 20 "stop: $PID_DIR: rejecting as not ours ..."; continue; } [ "$(readlink $PID_DIR/exe)" = /usr/bin/ffplay ] || { miniade_debug 20 "stop: $PID_DIR: rejecting as not ffplay ..."; continue; } strings $PID_DIR/environ | fgrep -xq WRA_STARTED_THIS=1 || { miniade_debug 10 "stop: $PID_DIR: rejecting as not an ffplay that wra started ..."; continue; } miniade_debug 10 "stop: $PID_DIR: got one (${PID_DIR#/proc/})" WRA_STARTED_FFPLAY_PIDS+=( ${PID_DIR#/proc/} ) done < <(pgrep ffplay) # There should be 0 or 1 pids that wra started. If 1 kill it. case ${#WRA_STARTED_FFPLAY_PIDS[*]} in 0) : ;; 1) kill ${WRA_STARTED_FFPLAY_PIDS[0]} CONFIRMED_KILLED_FLAG=false for X in {1..40}; do [ -d /proc/${WRA_STARTED_FFPLAY_PIDS[0]} ] || { CONFIRMED_KILLED_FLAG=true; break; } sleep 0.25 done $CONFIRMED_KILLED_FLAG || miniade_error "${WRA_STARTED_FFPLAY_PIDS[0]}: failed to kill" ;; *) miniade_error "an unexpected number of ffplay processes were found" ;; esac } main "$@"