-
Notifications
You must be signed in to change notification settings - Fork 34
/
qr
executable file
·87 lines (76 loc) · 2.81 KB
/
qr
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/bin/bash
set -e
usage() {
echo >&2 "Usage: ${0##*/} $(gawk -v line_n=100 \
'/^\s+case\>/ {parse=1; next}
/^\s+esac\>/ {exit}
!parse {next}
match($0, /^\s*([\-|a-z0-9]+)\)/, a) {
if (ac==0 || ac<2) ac=""; else ac=sprintf(ac==2?" ..":" <%s>", ac-1)
if (ap) {
ac=sprintf("[%s%s] ", ap, ac); acn=length(ac)
if (an && an+acn>line_n) { printf("\n "); an=acn; } else an+=acn
printf("%s", ac); }
ap=a[1]; ac=0 }
{for (i=1;i<NF;i++) if (match($i, /\<shift\>/)) ac++}' $0)"
echo >&2
echo >&2 "Create/decode QR code image."
echo >&2 " Create: displays it (using feh, default if DISPLAY= is set) or prints path to stdout."
echo >&2 " Decode: requires image(s), prints decoded contents to stdout."
echo >&2 "Different parameters determine type of data encoded there."
echo >&2 "NOTE: anything running on the system can see all passed command-line options!!!"
echo >&2
echo >&2 "Examples:"
echo >&2 " Create QR from stdin to display: qr <somefile"
echo >&2 " From raw string (arg or stdin): qr -r line-of-text"
echo >&2 " WiFi AP: qr -v -s myssid -p some-long-passphrase"
echo >&2 " Decode QR from image to stdout: qr image.png"
echo >&2
exit ${1:-1}
}
display= out= opts= verbose= image=()
raw= wifi= wifi_ssid= wifi_pass=
while [[ -n "$1" ]]; do
case "$1" in
-h|--help) usage 0 ;;
-v) shift; verbose=t ;;
-x) shift; set -x ;;
-d|--display) shift; display=t ;;
-o|--out-file) shift; out=$1; shift ;;
--opts) shift; opts=$1; shift ;;
-r|--raw-text) shift; raw=$1; shift || raw=$(</dev/stdin) ;;
-s|--ssid) shift; wifi=t; wifi_ssid=$1; shift ;;
-p|--pass) shift; wifi=t; wifi_pass=$1; shift ;;
-i|--image) shift; image+=( "$1" ); shift ;;
welp) exit 1 ;;
*) image+=( "$1" ); shift ;;
esac
done
[[ "${#image[@]}" -eq 0 ]] || {
err=0
for img in "${image[@]}"; do zbarimg -q "$img" || err=1; done
exit "$err"; }
[[ -n "$display" || -n "$out" || -z "$DISPLAY" ]] || display=t
dst=$out
[[ -n "$dst" ]] || {
[[ -z "$display" ]] \
&& { dst="$HOME"/Desktop; [[ -d "$dst" ]] || dst=$HOME; } \
|| { [[ -n "$XDG_RUNTIME_DIR" ]] && dst=$XDG_RUNTIME_DIR || dst=/tmp; }
dst="${dst}/qr-$(printf '%(%Y%m%d%H%M%S)T' -1).png"; }
opts=( $opts )
qr=()
[[ -z "$raw" ]] || qr+=( "$raw" )
[[ -z "$wifi" ]] || {
[[ "${#qr[@]}" -eq 0 ]] || { echo >&2 "ERROR: conflicting output mode opts"; exit 1; }
qr+=( "WIFI:T:WPA;S:${wifi_ssid};P:${wifi_pass};;" ); }
[[ "${#qr[@]}" -gt 0 ]] || qr+=( -r /dev/stdin )
[[ "${#qr[@]}" -gt 0 ]] || { echo >&2 "ERROR: no parameters to encode specified"; exit 1; }
[[ -z "$verbose" ]] || echo "${qr[@]}"
qrencode -o "$dst" "${opts[@]}" -- "${qr[@]}" \
|| { err=$?; echo >&2 "ERROR: qrencode operation failed"; exit $err; }
if [[ -z "$display" ]]
then [[ -n "$out" ]] || echo "$dst"
else
trap 'rm -f "$dst"' EXIT INT
feh --force-aliasing -FZYxs --image-bg white "$dst" ||:
fi