-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathtemplating.sh
executable file
·90 lines (76 loc) · 2.88 KB
/
templating.sh
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/usr/bin/env bash
# Very simple template language
if [ "$1" = "" ]; then
echo "USAGE: $0 <template-file>"
echo
echo "Template syntax:"
echo
echo 'Lines can start with @INCLUDE, @EVAL or @EXEC for special processing:'
echo '@INCLUDE foo.txt'
echo '@EVAL My home dir is $HOME'
echo '@EXEC uname -a'
echo '@EXEC [ "$foo" = "bar" ] && include bar.txt'
echo '@IF [ "$foo" = "bar" ]'
echo 'This line is only printed if $foo = "bar"'
echo '@ENDIF'
echo 'Other lines are printed unchanged.'
echo
echo "Environment variables:"
echo " tmpl_debug: If set, markers are printed around included files"
echo " tmpl_comment: Characters to use to start the marker comments (default: #)"
echo " tmpl_prefix: Regexp to match processing directive prefixes (default: @)"
exit 1
fi
tmpl_comment=${tmpl_comment:-#}
tmpl_prefix=${tmpl_prefix:-@}
include() {
[ "$tmpl_debug" != "" ] && echo "$tmpl_comment $1"
# Current level of @IF we are in
local iflevel=0
# Set to the @IF level that disabled the current block, if any
local ifdisablelevel=0
local line
local condition
( cat "$1" && echo ) | while IFS= read -r line; do
if [[ $line =~ ^${tmpl_prefix}\ *IF\ (.*) ]]; then
[ "$tmpl_debug" != "" ] && echo "$tmpl_comment $line"
iflevel=$((iflevel+1))
if ! [ $ifdisablelevel -gt 0 ]; then
# Only if not already in a disabled IF statement
condition="${BASH_REMATCH[1]}"
if ! eval "$condition" ; then
# Disabled at the current IF level
ifdisablelevel=$iflevel
fi
fi
elif [[ $line =~ ^${tmpl_prefix}\ *ENDIF ]]; then
[ "$tmpl_debug" != "" ] && echo "$tmpl_comment $line"
if [ $iflevel = 0 ] ; then
echo "ERROR: @ENDIF without matching @IF in file $1" > /dev/stderr
exit 30
fi
iflevel=$((iflevel-1))
if [ $ifdisablelevel -gt $iflevel ]; then
# We left the IF block level that was disabled
ifdisablelevel=0
fi
elif [ $ifdisablelevel -gt 0 ]; then
# nothing, in IF that evaluated to false
[ "$tmpl_debug" != "" ] && echo "$tmpl_comment $line"
elif [[ $line =~ ^${tmpl_prefix}\ *INCLUDE\ +([^ ]*) ]]; then
include="${BASH_REMATCH[1]}"
include $include
elif [[ $line =~ ^${tmpl_prefix}\ *EVAL\ (.*) ]]; then
line="${BASH_REMATCH[1]}"
eval echo "\"$line\""
elif [[ $line =~ ^${tmpl_prefix}\ *EXEC\ (.*) ]]; then
line="${BASH_REMATCH[1]}"
eval "$line"
else
echo "$line"
fi
done
[ "$tmpl_debug" != "" ] && echo "$tmpl_comment /$1"
}
include "$1"
exit 0