#!/bin/sh
set -e
if [ "$1" = "clean" ] ; then
    echo Cleaning docs...
    rm -rf "$DISTDIR/ximc/doc-en"
    rm -rf "$DISTDIR/ximc/doc-ru"
    exit 0
fi

[ -z "$BASEDIR" ] && export BASEDIR="$(pwd)"
[ -z "$DISTDIR" ] && export DISTDIR="$BASEDIR/dist"
[ -z "$MAKE" ] && export MAKE=make

export LIBXIMC_SRC_DIR="$BASEDIR/libximc/src"
export LIBXIMC_INC_DIR="$BASEDIR/libximc/include"

echo '----------------------------------------------------'
echo '                                                    '
echo '                    BUILDING DOCS                   '
echo '                                                    '
echo '----------------------------------------------------'

echo '----------------------------------------------------'
echo '              Checking dependencies...              '
echo '----------------------------------------------------'
set +e

echo Checking libximc sources that had to be generated by xigen...
set MISSING_FILES_DETECTED=false
FILE_LIST="$LIBXIMC_INC_DIR/ximc.h
    $LIBXIMC_SRC_DIR/ximc-gen.h
    $LIBXIMC_SRC_DIR/ximc-gen.c
    $LIBXIMC_SRC_DIR/libximc.def
    $LIBXIMC_SRC_DIR/fwprotocol.h
    $LIBXIMC_SRC_DIR/fwprotocol.c"
for file in $FILE_LIST ; do
    if [ ! -e "$file" ] ; then
        echo "$file cannot be found. ERROR!"
        MISSING_FILES_DETECTED=true
    else
        echo "$file is found."
    fi
done
[ -n "$MISSING_FILES_DETECTED" ] && exit 1
echo '***** Done!'

set -e
echo '------- Dependencies checking has been done --------'

cd "$BASEDIR/libximc/docs"
$MAKE
cd - 1>/dev/null
mkdir -p "$DISTDIR/ximc/doc-en"
mkdir -p "$DISTDIR/ximc/doc-ru"
cp -f "$BASEDIR/libximc/docs/doc-en/docs/libximc7-en.pdf" "$DISTDIR/ximc/doc-en"
cp -f "$BASEDIR/libximc/docs/doc-ru/docs/libximc7-ru.pdf" "$DISTDIR/ximc/doc-ru"
cp -rf "$BASEDIR/libximc/docs/doc-en/docs/html" "$DISTDIR/ximc/doc-en"
cp -rf "$BASEDIR/libximc/docs/doc-ru/docs/html" "$DISTDIR/ximc/doc-ru"
echo '----------------------------------------------------'
echo '              BUILDING DOCS IS COMPLETED            '
echo '----------------------------------------------------'
echo 'Output:                                             '
echo "* $DISTDIR/ximc/doc-en                              "
echo "* $DISTDIR/ximc/doc-ru                              "
echo '----------------------------------------------------'
