Construct cmd string before passing to spawn
[khatus.git] / bin / khatus_loop
index e5174c5..dd45962 100755 (executable)
@@ -1,5 +1,30 @@
 #! /bin/bash
 
+set -e
+
+produce_volume() {
+    pactl list sinks \
+    | awk '
+        /^\tMute:/ {
+            printf("%s,", $0);
+        }
+        /^\tVolume:/ {
+            for (i=2; i<=NF; i++) printf(" %s", $i);
+        }' \
+    | awk -v RS=',' '
+        /^[ \t]*Mute:/        {mute  = $2}
+        /^[ \t]*front-left:/  {left  = $4}
+        /^[ \t]*front-right:/ {right = $4}
+        END {
+            if (mute == "yes") {
+                printf("x")
+            } else {
+                printf("%s %s", left, right)
+            }
+        }
+        '
+}
+
 produce_mpd_state() {
     echo 'status' \
     | nc 127.0.0.1 6600 \
@@ -88,7 +113,8 @@ produce_mpd_song() {
 }
 
 produce_weather() {
-    metar -d "$WEATHER_STATION_ID" 2>&1 \
+    weather_station_id="$1"
+    metar -d "$weather_station_id" 2>&1 \
     | awk '
         /METAR pattern not found in NOAA data/ {
             failures++
@@ -119,6 +145,12 @@ consume() {
         -v opt_debug=0 \
         -v opt_mpd_song_max_chars=10 \
         '
+            /^in:VOLUME/\
+            {
+                split_msg_parts()
+                db["volume"] = msg_body
+            }
+
             /^in:MPD_STATE/\
             {
                 split_msg_parts()
@@ -160,6 +192,7 @@ consume() {
             }
 
             function make_bar(    position, bar, sep, i, j) {
+                position[++i] = sprintf("(%s)", db["volume"])
                 position[++i] = make_status_mpd()
                 position[++i] = db["weather_temperature"]
                 position[++i] = db["datetime"]
@@ -230,21 +263,60 @@ spawn() {
 }
 
 main() {
-    dir_bin="$1"
-    dir_data="$2"
-    pipe="$dir_data/pipe"
-
-    WEATHER_STATION_ID='KJFK'
-
+    # Defaults
+    dir_data="$HOME/.khatus"
+    weather_station_id='KJFK'
+
+    # User-overrides
+    OPTS=$(
+        getopt \
+            -o '' \
+            -l data-dir:,weather-station: \
+            -- "$@"
+    )
+    eval set -- "$OPTS"
+    while true
+    do
+        case "$1" in
+            --data-dir)
+                dir_data="$2"
+                shift 2
+                ;;
+            --weather-station)
+                weather_station_id="$2"
+                shift 2
+                ;;
+            --)
+                shift
+                break
+                ;;
+        esac
+    done
+
+    pipe="$dir_data/khatus_data_pipe"
+
+    ( echo "Khatus starting with the following parameters:"
+      ( echo "    dir_data|= $dir_data"
+        echo "    pipe|= $pipe"
+        echo "    weather_station_id|= $weather_station_id"
+      ) | column -ts\|
+      echo ''
+    ) >&2
+
+    mkdir -p "$dir_data"
     rm -f "$pipe"
     mkfifo "$pipe"
 
-    spawn produce_datetime  "$pipe" 'in:DATE_TIME' 1
-    spawn produce_weather   "$pipe" 'in:WEATHER'   $(( 30 * 60 ))
-    spawn produce_mpd_state "$pipe" 'in:MPD_STATE' 1
-    spawn produce_mpd_song  "$pipe" 'in:MPD_SONG'  1
-    spawn produce_bar_req   "$pipe" 'out:BAR'      1
-    consume "$pipe"
+    cmd_produce_weather="produce_weather $weather_station_id"
+
+    # TODO: Redirect each worker's stderr to a dedicated log file
+    spawn produce_datetime                 "$pipe" 'in:DATE_TIME' 1
+    spawn "$cmd_produce_weather"           "$pipe" 'in:WEATHER'   $(( 30 * 60 ))
+    spawn produce_mpd_state                "$pipe" 'in:MPD_STATE' 1
+    spawn produce_mpd_song                 "$pipe" 'in:MPD_SONG'  1
+    spawn produce_volume                   "$pipe" 'in:VOLUME'    1
+    spawn produce_bar_req                  "$pipe" 'out:BAR'      1
+          consume                          "$pipe"
 }
 
 main $@
This page took 0.030367 seconds and 4 git commands to generate.