#! /bin/bash

WHAT=$1

case "$WHAT" in
(doc) echo "Pushing documentation" ;;
(coverage) echo "Pushing coverage" ;;
(*) echo "ERROR!!"
    exit 1
esac

set -e
set -x

GIT_OWNER=$(  echo $TRAVIS_REPO_SLUG | sed "s/\/.*$//" )
GIT_REPO=$(   echo $TRAVIS_REPO_SLUG | sed "s/^.*\///" )

if [ "$TRAVIS_PULL_REQUEST" != false ] ; then
  echo "Building from a pull request, nothing to push"
  exit 0
fi

if [ "$GIT_OWNER" = plumed ] ; then
  if [[ "$TRAVIS_BRANCH" =~ ^v2\.[0-9]+$ ]] ; then
    if test -f CHANGES/"$TRAVIS_BRANCH".md && grep -q plumednotmaintained CHANGES/"$TRAVIS_BRANCH".md ; then
      branchtype=unsupported
    else
      branchtype=official
    fi
  elif [ "$TRAVIS_BRANCH" = master ] ; then
    branchtype=master
  else
    branchtype=unofficial
  fi
else 
  branchtype=unofficial
fi

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

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


case "$WHAT" in
(doc)
# 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/
;;
(coverage)
  cp -R $root/developer-doc/coverage .
  cp -R $root/developer-doc/coverage-libs .
esac


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

case "$WHAT" in
(doc)
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
  ;;
(coverage)
cat > index.html << EOF
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<body>
<title> Coverage scan </title>
<h1> Coverage scan </h1>
Here you can find coverage scan reports for PLUMED:
<ul>
  <li><a href=coverage/index.html>Standard coverage (excluding libraries)</a></li>
  <li><a href=coverage-libs/index.html>Coverage for third-party libraries</a></li>
</ul>
The first one is equivalent to the one you will find on <a href="https://codecov.io/gh/$GIT_OWNER/$GIT_REPO">CodeCov</a> (if enabled).
The second one only contains third-party libraries included in PLUMED.
</body>
</html>
EOF
  ;;
esac


if test -z "${GIT_BOT}"
then
  export GIT_BOT=plumedbot
fi

if test -z "${GIT_BOT_EMAIL}"
then
  export GIT_BOT_EMAIL=giovanni.bussi+plumedbot@gmail.com
fi

git init
git config user.email "${GIT_BOT_EMAIL}"
git config user.name "${GIT_BOT}"
git checkout -b gh-pages
# this is not to show the GIT_TOKEN on Travis log
set +x
git remote add doc https://${GIT_BOT}:$GIT_TOKEN@github.com/$GIT_OWNER/${WHAT}-${TRAVIS_BRANCH}.git
set -x

case "$WHAT" in
(doc)
cat > README.md << EOF
Precompiled manual for PLUMED ${TRAVIS_BRANCH}
-----------------------------

This repository hosts a precompiled manual for [PLUMED](http://www.plumed.org) ${TRAVIS_BRANCH},
git revision [$hash](https://github.com/${GIT_OWNER}/${GIT_REPO}/commit/$hash).

To browse the manual you should go [here](http://${GIT_OWNER}.github.io/${WHAT}-${TRAVIS_BRANCH}).

You can also download a full copy of the manual for offline access
at [this link](http://github.com/${GIT_OWNER}/${WHAT}-${TRAVIS_BRANCH}/archive/gh-pages.zip).

This manual has been compiled on [travis](http://travis-ci.org/${GIT_OWNER}/${GIT_REPO}) on $(date).

EOF

case "$branchtype" in
(official)    echo "This is the manual for an official and supported PLUMED branch." >> README.md ;;
(unsupported) echo "This is the manual for an official but unsupported PLUMED branch. It will not appear on search engines." >> README.md ;;
(master)      echo "This is the manual for the development PLUMED branch." >> README.md ;;
(unofficial)  echo "This is the manual for an unofficial PLUMED branch. It will not appear on search engines." >> README.md ;;
esac
;;

(coverage)
cat > README.md << EOF
Coverage scan for PLUMED ${TRAVIS_BRANCH}
-----------------------------

This repository hosts the coverage scan for [PLUMED](http://www.plumed.org) ${TRAVIS_BRANCH},
git revision [$hash](https://github.com/${GIT_OWNER}/${GIT_REPO}/commit/$hash).

Coverage scan done on [travis](http://travis-ci.org/${GIT_OWNER}/${GIT_REPO}) on $(date).

To browse the scan you should go [here](http://${GIT_OWNER}.github.io/${WHAT}-${TRAVIS_BRANCH}).

You can also download a full copy of the scan for offline access
at [this link](http://github.com/${GIT_OWNER}/${WHAT}-${TRAVIS_BRANCH}/archive/gh-pages.zip).
EOF
esac

case "$branchtype" in
(unsupported|unofficial)
# this is to avoid a huge log file:
set +x
  for file in $(find . -name "*.html") ; do
    awk '{
      if(done){print; next;}
      print
      if(match($0,".*<head>.*")){
        print "<meta name=\"robots\" content=\"noindex\">"
        done=1;
      }
    }END{
    if(!done) print "error: head not present in " FILENAME > "/dev/stderr"
    }' $file > $$
    mv $$ $file
done
set -x
esac

git add --all .
# >/dev/null to avoid excessive noise on travis log
git commit -m "Update to ${GIT_OWNER}/${GIT_REPO}@$hash" >/dev/null
# -q and 2> is not to show the GIT_TOKEN on Travis log
git push -q -f doc gh-pages 2> /dev/null 
