summaryrefslogtreecommitdiffstats
path: root/krebs/5pkgs/simple/qrscan.nix
blob: 7d99dcee718a86bb0475fe3a2bd2f6a863b34dd8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
{ coreutils, gnused, writeDashBin, zbar }:

writeDashBin "qrscan" ''
  set -efu

  tmpdir=$(${coreutils}/bin/mktemp --tmpdir -d qrscan.XXXXXXXX)
  codefile=$tmpdir/code

  cleanup() {
    ${coreutils}/bin/rm "$codefile"
    ${coreutils}/bin/rmdir "$tmpdir"
  }

  ${coreutils}/bin/mkfifo "$codefile"

  ${zbar}/bin/zbarcam > "$codefile" &
  zbarcampid=$!

  exec < "$codefile"
  while read -r code; do
    code=$(printf %s "$code" | ${gnused}/bin/sed -n 's/^QR-Code://p')
    if test -n "$code"; then
      ${coreutils}/bin/kill "$zbarcampid"
      echo "$code"
    fi
  done
''