#!/usr/bin/env bash
set -e

LOCALE=$1
# Lookup language code
LANGUAGE_CODE=`language-code "$LOCALE"`

# The directory where we store the built files
dist_dir="dist/$VERSION/$LANGUAGE_CODE"
mkdir -p "$dist_dir"

# Register path to HTML ZIP download (we'll build it later)
export HTML_DOWNLOAD="readonlydocs-$VERSION-$LANGUAGE_CODE.zip"

# Build HTML docs
echo "Building page for $LANGUAGE_CODE..."
sphinx-build -q -D "language=$LOCALE" -d "build/$LOCALE/doctrees" source "$dist_dir" &> "$dist_dir/build-log.txt"

# Replace working directory in log file
sed -i "s@$PWD/source/@@g" "$dist_dir/build-log.txt"

echo "Building ZIP for $LANGUAGE_CODE..."
cd "$dist_dir"
zip -9 -q -r "$HTML_DOWNLOAD" .
