56 lines
1.4 KiB
Bash
Executable File

#!/usr/bin/env bash
# vim:sw=4:ts=4:et
set -e
target_file_dir=/app/templates
target_files=(base.tmpl chorus-collection.tmpl chorus-collection-post.tmpl collection.tmpl collection-post.tmpl collection-tags.tmpl password-collection.tmpl post.tmpl read.tmpl)
custom_head_file=/app/data/custom-head.html
if [[ ! -f "$custom_head_file" ]]; then
echo "Error: $custom_head_file does not exist."
exit 1
fi
custom_content=$(<"$custom_head_file")
replace() {
if grep -q "<!-- custom head tags start -->" "$1"; then
awk -v content="$custom_content" '{
if ($0 ~ /<!-- custom head tags start -->/) {
in_block = 1;
print;
printf("%s\n", content);
next;
}
if ($0 ~ /<!-- custom head tags end -->/) {
in_block = 0;
}
if (!in_block) {
print;
}
}' "$1" > "$1.tmp"
else
awk -v content="$custom_content" '{
if ($0 ~ /<\/head>/) {
print "<!-- custom head tags start -->";
printf("%s\n", content);
print "<!-- custom head tags end -->";
print;
} else {
print;
}
}' "$1" > "$1.tmp"
fi
mv "$1.tmp" "$1"
}
for target_file in "${target_files[@]}"; do
if [[ ! -f "$target_file_dir/$target_file" ]]; then
echo "Error: $target_file_dir/$target_file does not exist."
exit 1
fi
replace "$target_file_dir/$target_file"
done