#!/bin/sh

euroklavdb="/home/david/share/euroklav"
dbpath="${euroklavdb}"

if [ $# -gt 0 ];
then
	infile="$1"
	case "${infile}" in
	/dev/rfcomm*)
		stty -F "${infile}" raw -echo -echoe -echok
		;;
	esac
else
	infile="/dev/stdin"
fi

cd "${dbpath}"
datetime="$(date +%Y-%m-%d-%H-%M-%S)"
outfile="euroklav-${datetime}.txt"
cat "${infile}" \
| while read -r line;
do
	case "${line}" in
	$'\003'*)
		datetime="$(date +%Y-%m-%d-%H-%M-%S)"
		outfile="euroklav-${datetime}.txt"
		;;
	*" 4.07 4.08"*)
		printf "%s\n" "${line}" >> ${outfile}
		cat ${outfile} \
			| tr -d '\002\003' \
			| tr '\370' ' ' \
			| tr '\346' ' ' \
			| lpr
		;;
	esac
	printf "%s\n" "${line}" >> ${outfile}
done

