#!/bin/bash

set -euxo pipefail

rm -rf public/

if [ -x "$(which mdbook)" ] ; then
    echo "mdBook found, running..."
    mdbook build -d public
else
    echo "mdBook not found, downloading..."
    wget https://github.com/rust-lang/mdBook/releases/download/v0.4.13/mdbook-v0.4.13-x86_64-unknown-linux-gnu.tar.gz -O mdbook.tgz
    tar zxvf mdbook.tgz
    ./mdbook build -d public
fi

cp -R static/* public/

# Work around some print.html bugs.
sed -i "s/\(href\|src\)=\"[[:alnum:]\-]*\/\//\1=\"\//g" public/print.html

./move_html_to_dir public/

# Make checkboxes editable, just in case the user wants to keep their own checklist.
find public/ -name "*.html" -type f -exec sed -i 's/input disabled=""/input/g' {} +

# Replace invalid `path_to_root`.
find public/ -name "*.html" -type f -exec sed -i 's/var path_to_root = "\/.";/var path_to_root = "\/";/g' {} +