#!/bin/sh

# This script builds and publishes pages

echo CHECKING REPO

git remote | grep gh-pages

if [ "$?" != 0 ]
then
	echo 'Remote `gh-pages` not found!'
	echo 'Create a `gh-pages` remote in this repo'
	echo 'with ssh url that has write access to `gh-pages` branch. '
fi

echo RUNNING DOCS PIPELINE
./podman/test-all.sh docs_with_coverage || exit 9

echo SAVING ARTIFACT
cp -r --reflink=auto ./podman/output/docs "$MINICYCLE_ARTIFACTS/docs" || exit 9

echo MAKING SURE GIT KNOWS WHO I AM

if [ -z "$(git config user.name)" ] || [ -z "$(git config user.email)" ]
then
	echo "git does not know me, setting user.name and user.email..."
	git config user.name "minicycle" || exit 9
	git config user.email "no@email.com" || exit 9
	git config --global user.name "minicycle" || exit 9
	git config --global user.email "no@email.com" || exit 9
fi

echo ADDING PAGES TO GIT
git reset --hard || exit 9
git branch gh-pages || echo "branch already exists, no problem..."
git checkout gh-pages || exit 9
rm -rf * || exit 9
cp -r --reflink=auto "$MINICYCLE_ARTIFACTS/docs" ./docs || exit 9
# Force needed because `docs` might be in gitignore
git add --force ./docs || exit 9
git commit --allow-empty -m "autocommit from Mitakihara on $(date)"  || exit 9
git push --force --set-upstream gh-pages gh-pages || exit 9


