imsh-clients

Clients for imsh screenshot/screencast sharing service
git clone https://git.echoz.io/imsh-clients.git
Log | Files | Refs

imsh-cast-monitor.sh (2060B)


      1 #!/usr/bin/env bash
      2 
      3 argv0="imsh-cast-monitor"
      4 imshCastArgv0="imsh-cast"
      5 wfRecorderArgv0="wf-recorder"
      6 
      7 usage() {
      8 cat <<EOF
      9 $argv0 - Display a flashing circle while imsh-cast is recording.
     10 
     11 Usage: $argv0 [pid-file] [options...]
     12 
     13 Options:
     14   pid-file
     15     PID file to watch for an active process.
     16     Defaults to: \$XDG_RUNTIME_DIR/$imshCastArgv0-0.pid
     17 
     18   -h, --help
     19     Display this message.
     20 EOF
     21 }
     22 
     23 fatalUser() {
     24 cat >&2 <<EOF
     25 Error: $1
     26 
     27 See $argv0 --help for more information.
     28 EOF
     29 exit 2
     30 }
     31 
     32 warn() {
     33   printf "Warning: %s\n" "$1" >&2
     34 }
     35 
     36 status() {
     37   text="$1"; shift
     38   class="$1"; shift
     39   jq -nc \
     40     --arg text "$text" \
     41     --arg class "$class" \
     42     '{"text": $text, "class": [ "imsh-cast-monitor", $class]}'
     43 }
     44 
     45 pidFile="$XDG_RUNTIME_DIR/$imshCastArgv0-0.pid"
     46 positional=()
     47 ignoreOpts=
     48 while [[ $# -gt 0 ]]; do
     49   case "$ignoreOpts$1" in
     50     -h|--help) usage; exit 0;;
     51     --) ignoreOpts=1;;
     52     -*) fatalUser "invalid option: $1";;
     53     *) positional+=("$1");;
     54   esac
     55 done
     56 set -- "${positional[@]}"
     57 
     58 pidFile="${1:-$pidFile}"; shift
     59 
     60 if [[ $# -gt 0 ]]; then
     61   fatalUser "extraneous positional arguments ($*)"
     62 elif [[ -z "$pidFile" ]]; then
     63   fatalUser "missing pid-file argument"
     64 fi
     65 
     66 state=
     67 while true; do
     68   text=
     69   class=
     70   [[ -e "$pidFile" ]] && pid="$(tr -d '[:space:]' <"$pidFile")" || pid=
     71   [[ "$pid" -gt 0 ]] && [[ -e "/proc/$pid" ]] \
     72     && pidArgv0="$(basename "$(tr '\0' '\n' <"/proc/$pid/cmdline" | head -n1)")" \
     73     || pidArgv0=
     74 
     75   if [[ "$pidArgv0" = "$wfRecorderArgv0" ]]; then
     76     state=$((!state))
     77     ((state)) && text=$'\uebb4' || text=$'\uebb5'
     78     class="recording"
     79   elif [[ -n "$pid" ]] || [[ -e "$pidFile" ]]; then
     80     warn "invalid PID file contents, this may have been caused by a previous $imshCastArgv0 crash."
     81     state=
     82     text=$'\ueabd'
     83     class="error"
     84   else
     85     state=
     86     text=""
     87     class="inactive"
     88   fi
     89 
     90   status "$text" "$class"
     91 
     92   inotifywait "$(dirname "$pidFile")" \
     93     --include "$(basename "$pidFile")" \
     94     --timeout 1 \
     95     --event create \
     96     --event delete \
     97     --event modify \
     98     >/dev/null 2>&1
     99 done