#! /bin/bash

##############################################################
# LICENSE: GNU GPL 3.0
#
# Runs on trunk branch.
# Transfers generated docs from DOC to trunk.
#
# See also:
# https://chiselapp.com/user/aplsimple/repository/HOWTO_chisel
#
##############################################################

EMBEDDDOC=../doc
SOURCEDOC=$EMBEDDDOC/md
GENERADOC=$EMBEDDDOC/www
TMPWWWDOC=../.TMP-WWW

# 1st test: can the monkey jump to trunk

THEBRANCH=$( fossil stat |  grep "^tags: " |  awk '{ print $2 }' )
if [ $THEBRANCH != "trunk" ] ; then
  COCOMMAND="fossil checkout trunk"
  { COMESSAGE="$( { $COCOMMAND; } 2>&1 1>&3 3>&- )"; } 3>&1;
  
  if [ "$COMESSAGE" != "" ] ; then
    echo "------------------------------------------------------"
    echo "fossil status:"
    echo " "
    fossil status
    echo "------------------------------------------------------"
    echo "\"$COCOMMAND\" failed with error:"
    echo "  $COMESSAGE"
    echo " "
    exit
  fi
fi

# 2nd test for certain: is it the trunk

THEBRANCH=$( fossil stat |  grep "^tags: " |  awk '{ print $2 }' )
if [ $THEBRANCH != "trunk" ] ; then
  echo "------------------------------------------------------"
  echo "Check if"
  echo "  trunk is a current branch."
  echo "------------------------------------------------------"
  exit
fi

echo "Restoring docs from TMP:"
rm -r -f  $GENERADOC
cp -r  $TMPWWWDOC  $GENERADOC

echo "Adding generated docs to trunc:"
fossil add $GENERADOC
fossil commit -m "Updating generated docs"
