-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate
executable file
·57 lines (51 loc) · 1.53 KB
/
generate
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
#!/bin/bash
if test $# -ne 1; then
echo "Usage: $0 <session-name>" 1>&2
echo
echo "Available sessions:"
IFS=$'\n'
for f in templates/*; do
echo -e " - $(basename "$f")"
done
exit 1
fi
session="$1"
tpl_session_path=templates/"$1"
if test ! -d "$tpl_session_path"; then
echo "No such session: $session" 1>&2
exit 1
fi
# Cleanup session folder if it exists.
if test -d "$session"; then
echo -en "Session folder ($session) already exists. It will be erased and recreated. Do you want to continue? [y/N] "
read answer
if test ! "$answer" == "y"; then
exit 0
fi
rm -fr "$session"
fi
mkdir "$session"
# Copy everything except for config file.
cp -r "$tpl_session_path"/* "$session"
# Generate actual files by replacing template statements using sed.
IFS=$'\n'
for task in "$session"/challenges/*; do
if test ! -d "$task"; then
continue
fi
rm -f "$task"/.config
config="$tpl_session_path"/challenges/"$(basename "$task")"/.config
find "$task" -type f | while IFS=$'\n' read f; do
echo "file: $f, config: $config"
if test -f "$config"; then
cat "$config" | while IFS=$'\n' read line; do
key=$(sed 's/^\([^= \t]\+\)[ \t]*=.*$/\1/' <<< "$line")
value=$(sed 's/^[^=]\+=[ \t]*\(.*\)$/\1/' <<< "$line")
key=${key//\//\\/}
value=${value//\//\\/}
#echo "key: $key, value: $value"
sed -i "s/$key/$value/g" "$f"
done
fi
done
done