#! /bin/bash

set -e
set -x

hash=$( git log -1 --format="%h")

root="$PWD"
mkdir -p tmp/$$
cd tmp/$$

# grab doc
mkdir user-doc
cp -R $root/user-doc/{html,manual.pdf} user-doc/
test -d $root/user-doc/regtests && cp -R $root/user-doc/regtests user-doc/ || true
mkdir developer-doc
cp -R $root/developer-doc/html developer-doc/

# This file should be here, it is needed otherwise
# files beginning with _ are skipped
touch .nojekyll

cat > index.html << EOF
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Page Auto Redirect</title>
<meta http-equiv="refresh" content ="0; user-doc/html/index.html">
</head>
<body>
This is an auto redirect page.
</body>
</html>
EOF

git init
git config user.email "giovanni.bussi+plumedbot@gmail.com"
git config user.name "plumedbot"
git checkout -b gh-pages
# this is not to show the GIT_TOKEN on Travis log
set +x
git remote add doc https://plumedbot:$GIT_TOKEN@github.com/plumed/doc-${TRAVIS_BRANCH}.git
set -x
cat > README.md << EOF
Precompiled manual for PLUMED ${TRAVIS_BRANCH}
-----------------------------

This repository hosts a precompiled manual
for PLUMED ${TRAVIS_BRANCH}, git revision $hash, $(date).
This manual has been compiled on [travis](http://travis-ci.org/plumed/plumed2).
To browse the manual you should go
[here](http://plumed.github.io/doc-${TRAVIS_BRANCH}).

EOF
git add --all .
git commit -m "Update to plumed/plumed2@$hash"
# -q is not to show the GIT_TOKEN on Travis log
git push -q -f doc gh-pages
