Remove link to GitHub sponsors
[openssl-web.git] / bin / from-tt
1 #! /bin/bash
2
3 HERE=$(cd $(dirname $0); pwd)
4 THIS=$(basename $0)
5
6 dir=
7 input=
8 output=
9
10 shortopts='d:i:o:h'
11 longopts='dir:,input:,output:,help'
12 usage="\
13 Usage 1: $THIS [ options ] [ key=value ... ] < file.tt > file
14 Usage 2: $THIS [ options ] [ key=value ... ] file.tt ...
15
16 Options:
17     -d, --dir=DIR               Directory of the output file
18     -i, --input=FILE            Input file (usage 1 only)
19     -o, --output=FILE           Output file (usage 1 only)
20     -h, --help                  Output this usage and do nothing else
21
22 In usage 1, the template is read from standard input or the file given
23 with --input and the processing result is output to standard output or
24 the file given with --output.  When the output goes to stdout, the --dir
25 option is mandatory.
26
27 In usage 2, the templates are read from the files given as argument, and
28 the processing result for each of them is written to a corresponding file
29 without the '.tt' suffix.  All given file names must have the '.tt' suffix.
30 In this usage, --input, --output, standard input and standard output are
31 ignored.
32
33 In both usages, one can also set template variables with with the form
34 key=value.  They must come before any file name."
35
36 # Standard getopt calling sequence
37 if ! TEMP=$(getopt -o "$shortopts" --long "$longopts" -n $THIS -- "$@"); then
38     echo >&2 "$usage"
39     exit 1
40 fi
41 eval set -- "$TEMP"
42 unset TEMP
43
44 # Check the parsed options
45 while true; do
46     case "$1" in
47         '-d' | '--dir' )
48             dir="$2"
49             shift 2
50             continue
51             ;;
52         '-i' | '--input' )
53             input="$2"
54             shift 2
55             continue
56             ;;
57         '-o' | '--output' )
58             output="$2"
59             shift 2
60             continue
61             ;;
62         '-h' | '--help' )
63             echo >&2 "$usage"
64             exit 0
65             ;;
66         '--' )
67             shift
68             break
69             ;;
70         * )
71             echo >&2 'Internal error!'
72             echo >&2 "$usage"
73             exit 1
74             ;;
75     esac
76 done
77
78 tpagecmd="tpage"
79 while true; do
80     case "$1" in
81         *=* )
82             tpagecmd="$tpagecmd --define '$1'"
83             shift
84             ;;
85         * )
86             break
87             ;;
88     esac
89 done    
90
91 # If there are no other arguments, read from stdin, write to stdout.
92 # Otherwise, read from the input files and write to corresponding output files.
93 if [ $# -eq 0 ]; then
94     if [ -z "$dir" ]; then
95         echo >&2 'Directory must be set with -d / --dir in this mode'
96         exit 1
97     fi
98     (
99         cd $dir
100         ( cat $HERE/../inc/common.tt;
101           if [ -n "$input" ]; then cat "$HERE/../$input"; else cat; fi ) \
102         | eval "$tpagecmd --define 'dir=$HERE/../${dir:-filedir}'" \
103         | ( if [ -n "$output" ]; then cat > "$HERE/../$output"; else cat; fi )
104     )
105 else
106     errfiles=
107     nofiles=
108     for f in "$@"; do
109         base=$(basename "$f" .tt)
110
111         if [ "$base" = "$f" ]; then
112             errfiles="$errfiles '$f'"
113         elif [ ! -f "$f" ]; then
114             nofiles="$nofiles '$f'"
115         fi
116     done
117     if [ -n "$errfiles" ]; then
118         echo >&2 "Files not ending with .tt:$errfiles"
119     fi
120     if [ -n "$nofiles" ]; then
121         echo >&2 "Files no present:$nofiles"
122     fi
123     if [ -n "$errfiles" -o -n "$nofiles" ]; then
124         exit 1
125     fi
126
127     for f in "$@"; do
128         base=$(basename "$f" .tt)
129         filedir=$(dirname "$f")
130
131         if [ "$f" != "$base" ]; then
132             if ! (
133                     cd $filedir
134                     ( cat $HERE/../inc/common.tt; cat $base.tt ) | \
135                         eval "$tpagecmd --define 'dir=${dir:-filedir}'" \
136                              > $base
137                 ); then
138                 exit $?
139             fi
140         fi
141     done
142 fi