#!/bin/sh
#
# tools/ant-txt/pick - Run ant-txt on a message selected by number
#
# Written 2012 by Werner Almesberger
# Copyright 2012 Werner Almesberger
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#

#
# File format (on stdin):
#
# - message blocks are separated by one or more empty lines
# - lines beginning with # are ignored before or after a message block
#   (or between empty lines)
#
# Message blocks are selected by number. The first block is 1.
#
#

opt=
if [ "${1#-}" != "$1" ]; then
	opt=$1
	shift
fi

sel=$1
skip=true
n=0
while read s; do
	[ "$s" = "${s#\#}" ] || continue
        if [ ! "$s" ]; then
		skip=true
		continue
	fi
	$skip && n=`expr $n + 1`
	skip=false
	[ "$sel" ] || continue
	[ $sel = $n ] && break
done

if [ ! "$sel" ]; then
	echo $n
	exit
fi

msg=
while true; do
	msg="$msg '$s'"
	read t || break
	[ -z "$t" -o "$t" != "${t#\#}" ] && break
	s=$t
done

dir=${0%/*}
eval $dir/ant-txt $opt -F $dir/../fonts $msg