notes/gollum/index.sh

89 lines
2.1 KiB
Bash
Executable File

#! /bin/bash
function main() {
set -e
fill_missing_files
format_files
init_git
}
function fill_missing_files() {
pushd "${1:-/wiki}" > /dev/null 2>&1
fill_if_missing ./Home.md
fill_if_missing ./_Header.md
find ./* -type d | while read -r i; do
i="${i%/}"
fill_if_missing "$i.md"
fill_if_missing "$i/_Header.md"
done
rm -f .git/hooks/pre-commit || true
for i in .git/hooks/pre-commit custom.css custom.js; do
fill_if_missing ./$i "$(cat /opt/${i##*/})"
done
mkdir -p ./uploads-ftp
cp /opt/favicon.ico . || true
popd > /dev/null 2>&1
}
function format_files() {
pushd ${1:-/wiki} > /dev/null 2>&1
local patterns=('^[0-9][0-9]*\. *' ' 1. ')
local f="/tmp/switch"
find . -type f -name "*.md" | grep -v '\/_[^\/]*.md' | while read -r path; do
for ((i=0; i<${#patterns[@]}; i+=2)); do
local from="${patterns[i]}"
local to="${patterns[i+1]}"
if [ -n "$(grep "${from// /.}" "$path")" ]; then
cat "$path" \
| sed "s/$from/$to/g" \
> "$f"
cp "$f" "$path"
fi
done
done
rm -f "$f"
popd > /dev/null 2>&1
}
function init_git() {
pushd ${1:-/wiki} > /dev/null 2>&1
git config user.email bel@bel.bel
git config user.name bel
git add -A :/
if [ "${JUST_ADD:-""}" != "true" ]; then
JUST_ADD=true git commit --author="auto <>" -m "routine" || true
fi
popd > /dev/null 2>&1
}
function fill_if_missing() {
local relative_path="$1"
shift
mkdir -p "$(dirname "$relative_path")"
local full_path="$(printf "%s/%s" \
"$(realpath \
"$(dirname "$relative_path")" \
)" \
"$(basename "$relative_path")" \
)"
if [ ! -e "$full_path" ]; then
if [ "$#" -gt 0 ]; then
echo "$@" > "$full_path"
elif [[ "$relative_path" == *"_Header.md" ]]; then
echo "<<PathSegments()>>" > "$full_path"
else
echo "<<ImmediatePages()>>" > "$full_path"
fi
fi
chmod +x "$full_path"
}
if [ "$0" == "${BASH_SOURCE[0]}" ]; then
main "$@"
fi