aboutsummaryrefslogtreecommitdiff
path: root/format.sh
blob: 1cdee4f6e65989d4fd6bdb8ec9368f36f5cea375 (plain) (blame)
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
#!/bin/sh
##
# Usage: format.sh TEMPLATE [SELEXP]
#
# This runs recfmt w/ template file TEMPLATE, taking input from stdin,
# and writing output to stdout.
#
# Optional arg SELEXP is an expression passed to ‘recsel -e’.  If specified,
# stdin is first processed by recsel and its output is then piped to recfmt
# for formatting.  If recsel exits failurefully (e.g., given invalid SELEXP),
# no output is written and format.sh exits failurefully as well.
##
me=$(basename $0)

version='1.3'
# 1.3  -- add support for optional arg SELEXP
# 1.2  -- add check for required arg TEMPLATE
# 1.1  -- add --help/--version support
# 1.0  -- initial release

if [ x"$1" = x--help ] ; then
    sed '/^##/,/^##/!d;/^##/d;s/^# //g;s/^#$//g' $0
    exit 0
fi

if [ x"$1" = x--version ] ; then
    echo $me '(gana)' $version
    exit 0
fi

if [ x"$1" = x ] ; then
    echo >&2 "$me: ERROR: missing arg TEMPLATE (try --help)"
    exit 1
fi

template="$1"

if [ x"$2" = x ] ; then : ; else
    selexp="$2"
fi

if [ "$selexp" ] ; then
    t=$(mktemp)
    trap "rm -f $t" EXIT
    recsel -e "$selexp" > $t &&
        recfmt -f "$template" < $t
else
    exec recfmt -f "$template"
fi

# format.sh ends here