#!/bin/bash

notify-send "Bitte QR-Code in die Webcam halten!"
TEXT="$(zbarcam --oneshot --raw)"

if [ -z "$TEXT" ] ; then
        notify-send "Kein QR- oder Barcode erkannt"
        exit 1
fi

case "$TEXT" in
        https://*)
        kdialog --yesnocancel "Der QR-Code enthielt folgenden Text:\n\n$TEXT" --yes-label "Mit Firefox öffnen" --no-label "Kopieren"
        case "$?" in
                0)
                CMD="firefox"
                ;;
                1)
                CMD="copy"
                ;;
        esac
        ;;
        *)
        kdialog --yesno "Der QR-Code enthielt folgenden Text:\n\n$TEXT" --yes-label "Kopieren" --no-label "Schliessen" && CMD="copy"
        ;;
esac

case "$CMD" in
        copy)
        printf "%s" "$TEXT" | xclip -selection clipboard
        notify-send "Kopiert"
        ;;
        firefox)
        notify-send "Öffne Firefox …"
        firefox -url "$TEXT" &
        ;;
        *)
        notify-send "Abgebrochen"
        ;;
esac

