#! /usr/bin/env sh

# Usage: ./ci-build.sh VERSION
# 
# This script builds the book to HTML with mdbook 
# commits and pushes the contents to the repo in the "gh-pages" branch.
#
# It is only inteded for use on the CI!

# Enable strict error checking.
set -exo pipefail

DIR=$(dirname $(readlink -f $0))
MDBOOK="mdbook"

cd $DIR

# Verify version argument.

if [[ -z "$1" ]]; then
    echo "Missing required argument 'version': cargo make build-book VERSION"
    exit
fi
VERSION="$1"

# Download mdbook if not found.
$MDBOOK -h 2>&1 > /dev/null
if [ $? != 0]; then
    echo "mdbook not found. Downloading..."
    curl -L https://github.com/rust-lang-nursery/mdBook/releases/download/v0.2.0/mdbook-v0.2.0-x86_64-unknown-linux-gnu.tar.gz | tar xzf -    
    mv ./mdbook /tmp/mdbook
    set MDBOOK="/tmp/mdbook"
fi

$MDBOOK build
echo $VERSION > ./_rendered/VERSION
rm -rf /tmp/book-content
mv ./_rendered /tmp/book-content

cd $DIR/../..
git clean -fd
git checkout gh-pages
rm -rf $VERSION
mv /tmp/book-content ./$VERSION
git add -A $VERSION
git commit -m "Updated book for $VERSION"
git push origin gh-pages