-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathprogress
More file actions
executable file
·191 lines (165 loc) · 4.64 KB
/
progress
File metadata and controls
executable file
·191 lines (165 loc) · 4.64 KB
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#!/usr/bin/env bash
set -euo pipefail
if [[ ! "$BASH_VERSION" > "5.2.0" ]]; then
>&2 echo "Requires bash 5.2 or above. Sorry."
exit 1
fi
TREASURE=TREASURE
here=$(pwd)
puzzle_dir="$here/puzzle"
secrets_dir="$here/.found"
hints_dir="$here/.hints"
verbose="no"
show_hint="no"
while [[ $# -gt 0 ]]; do
case "$1" in
-c | --clean)
rm -rf "$secrets_dir"
shift;
;;
-v | --verbose)
verbose=yes
shift;
;;
-h | --hint)
show_hint=yes
shift
;;
*)
>&2 echo "Unknown option: $1"
exit 1
;;
esac
done
script_name() {
printf "%s/clue-%03d.sh" "$here" "$1"
}
answer_name() {
printf "%s/secret-%03d.txt" "$secrets_dir" "$1"
}
hint_name() {
printf "%s/hint-%03d.txt" "$hints_dir" "$1"
}
# Get the clue that the current script is based on. Assumes that we've already
# run the previous script so the answer file exists.
get_clue() {
i="$1"
if [[ "$i" -eq 0 ]]; then
cat "$puzzle_dir/.first-clue"
else
secret=$(< "$(answer_name $((i - 1)))")
echo "${secret#*: }"
fi
}
# Get the secret for step i by running the script or loading the cached answer.
get_secret() {
script=$(script_name "$1")
answer=$(answer_name "$1")
# Run the script if needed (no answer or out of date answer)
if [[ ! -e "$answer" || "$script" -nt "$answer" ]]; then
if [[ "$verbose" = "yes" ]]; then
>&2 echo "Running $(basename "$script")"
fi
if ! "$script" > "$answer"; then
rm -f "$answer"
return 1
fi
else
if [[ "$verbose" = "yes" ]]; then
>&2 echo "$(basename "$answer") up to date"
fi
fi
# Should be a file by now.
if [[ -e "$answer" ]]; then
cat "$answer"
return 0
else
>&2 echo "Weird. No answer file."
return 1
fi
}
decrypt_treasure() {
local keygen_opts
read -ra keygen_opts < "$puzzle_dir/.keygen-opts"
find "$secrets_dir" -type f | sort | xargs cut -d : -f 1 | \
openssl dgst -sha256 -binary | \
openssl enc -d "${keygen_opts[@]}" \
-in "$puzzle_dir/.treasure.enc" \
-out "$TREASURE" \
-pass stdin 2> /dev/null
}
complete=0
if [[ -d "$puzzle_dir" ]]; then
next_clue=$(get_clue "0")
readarray -t hashes < <(tac "$puzzle_dir/.hashes")
mkdir -p "$secrets_dir"
i=0
while [[ "$i" -lt "${#hashes[@]}" ]]; do
script=$(script_name "$i")
if [[ -e "$script" ]]; then
if secret=$(get_secret "$i"); then
check=$(sha1sum <<< "$secret" | cut -c -40)
if [[ "${hashes[$i]}" = "$check" ]]; then
printf "✅ %s (%s)\n" "$(get_clue "$i")" "$(basename "$script")"
complete=$((complete + 1))
next_clue="${secret#*: }"
else
printf "❌ %s (%s)\n" "$(get_clue "$i")" "$(basename "$script")"
break
fi
else
printf "💣 Problem running %s\n" "$script"
next_clue="$(get_clue "$i")"
break
fi
else
break
fi
((i += 1))
done
# i is now at the index of the next puzzle or the length of hashes if all
# puzzles have been solved.
echo
echo "$complete of ${#hashes[@]} complete"
if [[ "$complete" -lt "${#hashes[@]}" ]]; then
if [[ -n "$next_clue" ]]; then
echo
echo "Next clue: $next_clue"
echo
if [[ "$show_hint" = "yes" ]]; then
hint=$(hint_name "$i")
if [[ -e "$hint" ]]; then
cat "$hint"
else
echo "Sorry, no hint available."
fi
echo
fi
next_script="$(script_name "$complete")"
if [[ ! -e "$next_script" ]]; then
cat <<EOF > "$next_script"
#!/usr/bin/env bash
set -euo pipefail
EOF
chmod +x "$next_script"
fi
echo "Write your code to extract this secret in ${next_script#"$here/"}"
echo
fi
else
if [[ -e "$TREASURE" ]] || decrypt_treasure; then
echo
echo "You found the treasure!"
echo
cat "$TREASURE"
else
rm -f "$TREASURE"
>&2 echo
>&2 echo "Uh oh. Your treasure seems to be damaged."
fi
echo
fi
else
echo "No puzzle directory!"
exit 1
fi