#!/bin/sh

usage='Usage: sprec_type [-c] [-d DEVICE] [-o] [-t STRING]

sprec_type types speech recognition results in realtime.

-c         Capitalize the first letter of each sentence.
-d DEVICE  Specify the input audio device (like arecord -D).
-o         Terminate after the first sentence.
-t STRING  Specify how to end sentences. default: ". "

sprec_type supports the environment variables of sprec
and dotool for specifying the speech recognition model,
keyboard layout, etc.
'

! command -v arecord >/dev/null && echo 'you need arecord' && exit 1
! command -v dotool >/dev/null && echo 'you need dotool' && exit 1

cap=0 device=sysdefault:CARD=Microphone once=0 term='. '
while [ $# != 0 ]; do
	case "$1" in
	-h|--help) printf %s "$usage"; exit;;
	-c) cap=1; shift;;
	-d) device="$2"; shift 2;;
	-o) once=1; shift;;
	-t) term="$2"; shift 2;;
	*) printf %s\\n "unknown option: $1" >&2; exit 1;;
	esac
done

if awk -Wv 2>&1 | grep -q '^mawk '; then
	alias awk='mawk -Winteractive'
fi

arecord -q -fS16_LE -c1 -r16000 -D "$device" | sprec --partial |
	awk -v cap="$cap" -v once="$once" -v term="$term" '
{
	final = /^final/
	sub(/.*: /, "")

	if (cap) {
		$0 = toupper(substr($0, 1, 1)) substr($0, 2)
		s = toupper(substr(s, 1, 1)) substr(s, 2)
	}

	for (i = 1; i <= length($0); i++) {
		if (substr($0, i, 1) != substr(s, i, 1)) {
			break
		}
	}

	if (s != "") {
		bs = "key"
		for (j = 0; j <= length(s)-i; j++) {
			bs = bs " backspace"
		}
		print bs
	}
	print "type " substr($0, i)

	if (final) {
		if ($0 != "") {
			print "type "term
		}

		if (once) {
			exit
		}

		s = ""
	} else {
		s = $0
	}

	fflush()
}' | dotool
