#!/bin/sh
set -eu

usage() {
	cat <<-'EOF'
		Usage:
		  qr [-s PIXEL_SIZE]
		  qr -h
	EOF
}

help() {
	cat <<-'EOF'


		Options:
		  -s PIXEL_SIZE    size of the pixel (default 10)
		  -h, --help       show this help message


		Read data from STDIN and present a QR image with said data.


		Examples:

		  Link to my homepage:

		    $ printf 'https://euandre.org' | qr


		  Numbers with a smaller pixel size:

		    $ seq 99 | qr -s 5
	EOF
}

for flag in "$@"; do
	case "$flag" in
		(--)
			break
			;;
		(--help)
			usage
			help
			exit
			;;
		(*)
			;;
	esac
done

PIXEL_SIZE=10
while getopts 's:h' flag; do
	case "$flag" in
		(s)
			PIXEL_SIZE="$OPTARG"
			;;
		(h)
			usage
			help
			exit
			;;
		(*)
			usage >&2
			exit 2
			;;
	esac
done
shift $((OPTIND - 1))


cat | qrencode -s "$PIXEL_SIZE" -o- | feh -
