#! /bin/bash

##############################################################
# LICENSE: GNU GPL 3.0
#
# Runs on DOC branch.
# Prepares 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: is it the DOC branch

ISDOCBRANCH=0
if ls -1qA  $GENERADOC/* | grep -q . ; then
  if ls -1qA $SOURCEDOC/* | grep -q . ; then
    ISDOCBRANCH=1
  fi
fi

THEBRANCH=$( fossil stat |  grep "^tags: " |  awk '{ print $2 }' )
if [ $THEBRANCH == "DOC" ] ; then
  ISDOCBRANCH=1
fi

if test $ISDOCBRANCH -eq 0 ; then
  echo "------------------------------------------------------"
  echo "Check if"
  echo "  DOC is a current branch."
  echo "  $SOURCEDOC and $GENERADOC both exist and are filled."
  echo "------------------------------------------------------"
  exit
fi

echo "Saving generated docs in TMP:"
rm -r -f  $TMPWWWDOC
cp -r  $GENERADOC  $TMPWWWDOC

echo "Adding sources to DOC branch:"
fossil add $SOURCEDOC
fossil commit -m "Making sources of docs"

# Last test for certain: can the monkey jump to DOC branch

COCOMMAND="fossil checkout DOC"
{ 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
