forked from elliptic/qw
-
Notifications
You must be signed in to change notification settings - Fork 4
/
make-qw.sh
executable file
·68 lines (58 loc) · 1.8 KB
/
make-qw.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
#!/bin/bash
# A bash script to combine qu lua from the qw git repo.
rc_file=
first_lua_file="variables.lua"
lua_dir="source"
version=$(git describe)
lua_out_file="qw.lua"
rc_out_file="qw-final.rc"
out_file=
rc_marker='\# include = qw.lua'
read -d '' usage_string <<EOF
$(basename $0) [-d LUA-DIR] [-r RC-FILE [-m MARKER]] [-o OUT-FILE]
Combine lua files from a directory into a single lua file, optionally combining
this with a crawl rc file based on a marker string.
Default lua directory: $lua_dir
Default output lua file: $lua_out_file
Default rc output file (with -r): $rc_out_file
Default rc file lua marker string (with -r): $rc_marker
EOF
while getopts "h?m:o:r:" opt; do
case "$opt" in
h|\?)
echo -e "$usage_string"
exit 0
;;
o) out_file="$OPTARG"
;;
m) rc_marker="$OPTARG"
;;
r) rc_file="$OPTARG"
;;
esac
done
shift $(($OPTIND - 1))
set -e
lua_text=$(cat ${lua_dir}/${first_lua_file})
lua_text+=$'\n'
lua_text+=$(find source -iname '*.lua' -not -name "$first_lua_file" | sort | xargs cat)
lua_text="${lua_text/\%VERSION\%/$version}"
if [ -n "$rc_file" ]; then
if [ -z "$out_file" ]; then
out_file="$rc_out_file"
fi
rc_text=$(cat "$rc_file")
lua_text=$(printf "<\n%s\n>" "$lua_text")
# Quotes needed around $lua_text to prevent expansion of & characters for
# bash 5.2 and later.
printf "%s\n" "${rc_text/$rc_marker/"$lua_text"}" > "$out_file"
rc_nlines=$(echo "$rc_text" | wc -l)
echo Added $rc_nlines lines from $rc_file to $out_file
else
if [ -z "$out_file" ]; then
out_file="$lua_out_file"
fi
printf "<\n%s\n>\n" "$lua_text" > "$out_file"
fi
lua_nlines=$(echo "$lua_text" | wc -l)
echo Added $lua_nlines lines from $lua_dir to $out_file