#! /bin/sh
#
# mkdoc
#
# Create reference documentation for a POCO release.
# Reads the current version from $POCO_BASE/VERSION and requires
# a release specification (loaded from $POCO_BASE/release/spec/*.release)
# as argument.
#
# usage: mkdoc [<specfile>]
#

set -e

if [ "$POCO_BASE" = "" ] ; then
  echo "Error: POCO_BASE not set."
  exit 1
fi

spec=""
docConfig=""
while [ "$1" != "" ] ;
do
	if [ "$1" = "-C" ] ; then
		shift
		docConfig="$docConfig -C $1"
		shift
	elif [ "$1" = "-v" ] ; then
		shift
		version=$1
		shift
	else
		spec=$1
		shift
	fi
done

if [ -z "$docConfig" ] ; then
	docConfig="-C $POCO_BASE/PocoDoc/cfg/mkdoc-poco.xml"
fi

if [ "$spec" != "" ] ; then
  relspec="-f release/spec/${spec}.release"
  tag="-$spec"
  reltag="-t $spec"
else
  relspec=""
  reltag=""
  tag=""
fi

cd $POCO_BASE

if [ ! -f VERSION ] ; then
  echo "Error: No VERSION file found."
  exit 2
fi

if [ "$version" = "" ] ; then
	read version <$POCO_BASE/VERSION
fi
release=$version$tag

#
# Build PocoDoc tool using CMake
#

echo "Building tools"

tools=$POCO_BASE/stage/tools
rm -rf $tools
mkdir -p $tools
mkrelease -o $tools $version Data Data/SQLite CppParser PocoDoc

cmake_build=$tools/cmake-build
cmake -S$tools -B$cmake_build \
    -DCMAKE_BUILD_TYPE=Release \
    -DPOCO_MINIMAL_BUILD=ON \
    -DENABLE_POCODOC=ON

cmake --build $cmake_build --target PocoDoc --parallel 8

export PATH=$cmake_build/bin:$PATH
if [ -n "$LD_LIBRARY_PATH" ] ; then
  export LD_LIBRARY_PATH="$cmake_build/lib:$LD_LIBRARY_PATH"
else
  export LD_LIBRARY_PATH="$cmake_build/lib"
fi
echo PATH=$PATH

echo "Building documentation $release (using $docConfig)"
mkdocumentation $reltag $relspec $docConfig -v $version
