#!/bin/bash

echo "Preprocessing..."

# include a date stamp with the latest commit
sed -i -e "s/\*Last updated:.*/\*Last updated: $(git log --format="%cd (commit %h)" --date=short -1)\*  /" src/index.md 

echo "Building documentation..."

julia builddocs.jl

# Disabling the pretty-url feature of `makedocs` doesn't work, so we have to
# revert it manually

echo "Postprocessing..."

sed -i -e "s/io\//io\/index.html/g" build/index.html build/search_index.js
sed -i -e "s/developing\//developing\/index.html/g" build/index.html build/search_index.js
sed -i -e "s/architecture\//architecture\/index.html/g" build/index.html build/search_index.js
sed -i -e "s/species-dsl\//species-dsl\/index.html/g" build/index.html build/search_index.js
sed -i -e "s/populations\//populations\/index.html/g" build/index.html build/search_index.js
sed -i -e "s/species\//species\/index.html/g" build/index.html build/search_index.js
sed -i -e "s/crops\//crops\/index.html/g" build/index.html build/search_index.js
sed -i -e "s/farm\//farm\/index.html/g" build/index.html build/search_index.js
sed -i -e "s/simulation\//simulation\/index.html/g" build/index.html build/search_index.js
sed -i -e "s/search\//search\/index.html/g" build/index.html build/search_index.js

sed -i -e "s/href=\"..\/\"/href=\"..\/index.html\"/g" build/*/index.html
sed -i -e "s/..\/io\//..\/io\/index.html/g" build/*/index.html
sed -i -e "s/..\/developing\//..\/developing\/index.html/g" build/*/index.html
sed -i -e "s/..\/architecture\//..\/architecture\/index.html/g" build/*/index.html
sed -i -e "s/..\/species-dsl\//..\/species-dsl\/index.html/g" build/*/index.html
sed -i -e "s/..\/nature\//..\/nature\/index.html/g" build/*/index.html
sed -i -e "s/..\/species\//..\/species\/index.html/g" build/*/index.html
sed -i -e "s/..\/crops\//..\/crops\/index.html/g" build/*/index.html
sed -i -e "s/..\/farm\//..\/farm\/index.html/g" build/*/index.html
sed -i -e "s/..\/simulation\//..\/simulation\/index.html/g" build/*/index.html
sed -i -e "s/..\/search\//..\/search\/index.html/g" build/*/index.html

echo "Done."