about summary refs log tree commit diff
path: root/pkgs/openlab
diff options
context:
space:
mode:
authorProfpatsch <mail@profpatsch.de>2017-02-05 21:42:52 +0100
committerProfpatsch <mail@profpatsch.de>2017-02-05 21:42:52 +0100
commit69fd5c5856ef7047b8fe654081cfe261bceb72ca (patch)
treee810c76858a1f6ecbd0185c705ebdc9c0e03424b /pkgs/openlab
parentfec655a4ce4598970720fe8cd4273f962735b8af (diff)
machines/hannswurscht: improved stackenblocken script
Diffstat (limited to 'pkgs/openlab')
-rw-r--r--pkgs/openlab/stackenblocken/default.nix76
1 files changed, 63 insertions, 13 deletions
diff --git a/pkgs/openlab/stackenblocken/default.nix b/pkgs/openlab/stackenblocken/default.nix
index 01a1568c..af8bddd8 100644
--- a/pkgs/openlab/stackenblocken/default.nix
+++ b/pkgs/openlab/stackenblocken/default.nix
@@ -1,4 +1,4 @@
-{ lib, fetchFromGitHub, writeScriptBin
+{ lib, fetchFromGitHub, writeScriptBin, curl
 , haskellPackages, mpg321 }:
 
 let
@@ -13,21 +13,71 @@ let
  jingle = "${repo}/stackenblocken_jingle.mp3";
 
  script = ''
-   #!/bin/sh
+    #!/usr/bin/env bash
+    percent=10
+    no_stackenblocken="no STACKENBLOCKEN today"
+    tmpd=$(mktemp -d)
 
-   # kill everything on SIGINT
-   trap exit SIGINT
-   # also running background processes
-   trap "kill 0" EXIT
+    # kill everything on SIGINT
+    trap exit SIGINT
+    # also running background processes
+    trap "kill 0" EXIT
 
-   for i in $(seq 2); do
-     echo "starting .labping bot"
-     ${lib.getBin bot}/bin/stackenblocken &
+    function icsfile {
+      awk -v date=''${1:-nodate} '
+        /BEGIN:VEVENT/ { cache = 1; }
+        /DTSTART:/ {
+          if( index( $0, date ) )
+            printf( "%s", cached_lines );
+          else
+            drop = 1;
+          cached_lines = "";
+          cache = 0;
+        }
+        cache  {
+          cached_lines = cached_lines $0 "\n";
+          next;
+        };
+        !drop { print; }
+        /END:VEVENT/ { drop = 0; }
+      '
+    }
 
-     echo "DOING STACKENBLOCKEN"
-     ${lib.getBin mpg321}/bin/mpg321 --gain 40 -q ${jingle}
-   done
- '';
+    function check_events {
+      ${lib.getBin curl}/bin/curl -s https://openlab-augsburg.de/veranstaltungen/events.ics \
+        | icsfile `date +%Y%m%d` \
+        > "$tmpd/events-today"
+
+      # filter out events that have the no-stackenblocken tag
+      # and skip it on those days
+      if <"$tmpd/events-today" grep -q "CATEGORIES.*no-stackenblocken"; then
+        events=$(<$tmpd/events-today sed -ne 's/SUMMARY:\(.*\)$/\1/p')
+        echo "$no_stackenblocken because of event(s):"
+        echo "$events"
+        exit 0
+      fi
+    }
+
+    function check_random {
+      rnumber=$RANDOM
+      ((rnumber %= 100))
+      # lt for percent (numbers begin from 0)
+      if [ $rnumber -lt $percent ]; then
+        echo "$no_stackenblocken because lucks says so! ($percent% chance)"
+        exit 0
+      fi
+    }
+
+    check_events
+    check_random
+
+    for i in $(seq 2); do
+      echo "starting .labping bot"
+      ${lib.getBin bot}/bin/stackenblocken &
+      echo "DOING STACKENBLOCKEN"
+      ${lib.getBin mpg321}/bin/mpg321 --gain 40 -q ${jingle}
+    done
+  '';
 
 
 in