#!/bin/bash

# build the files for tuhh-delivery locally
#
#  Structure 
#
# tuhh-build/
# ├── forschung
# │   ├── aa/forschung.html
# │   ├── cm/forschung.html
# │   ├── dm/forschung.html
# │   ├── nm/forschung.html
# │   └── st/forschung.html
# ├── home
# │   ├── ataraz.html
# │   ├── cseifert.html
# │   └── ymogge.html
# └── topics
#     ├── img
#     │   ├── BO_BDO.jpg
#     │   ├── controlimex.png
#     │   └── webpage-example.png
#     ├── topic.html
#     └── Zemke_Hessenberg.html
# 
#
#

RESEARCH_BIN=$RESEARCH_ROOT/bin
RESEARCH_STATIC=$RESEARCH_ROOT/static
RESEARCH_BUILD=$RESEARCH_ROOT/build
RESEARCH_DEPLOY=$RESEARCH_ROOT/build-tuhh

rm -rf $RESEARCH_BUILD $RESEARCH_DEPLOY
mkdir -p $RESEARCH_BUILD $RESEARCH_DEPLOY 

echo "Preproc Topics"
$RESEARCH_BIN/preproc_topics.sh "$RESEARCH_BUILD" \
                                "$RESEARCH_ROOT/topics" \
                                "$RESEARCH_BUILD/home" \
                                "$RESEARCH_BUILD/forschung" >> $RESEARCH_BUILD/build.log 2>&1

# build index file
cp $RESEARCH_BUILD/index.md  $RESEARCH_DEPLOY
$RESEARCH_BIN/create_html.sh $RESEARCH_DEPLOY \
                             $RESEARCH_DEPLOY \
                             TUHH >> $RESEARCH_BUILD/build.log 2>&1
rm $RESEARCH_DEPLOY/index.md

# build html files
echo "Creating HTML Topics with pandoc"
#topics
$RESEARCH_BIN/create_html.sh $RESEARCH_BUILD/forschung/topics \
                             $RESEARCH_BUILD \
                             TUHH >> $RESEARCH_BUILD/build.log 2>&1

# include ssi directives
echo "Inserting SSI-directives"
$RESEARCH_BIN/replace_ssi_marker.sh $RESEARCH_DEPLOY/forschung/topics \
                                    $RESEARCH_BUILD/forschung/topics >> $RESEARCH_BUILD/build.log 2>&1

echo "Creating HTML Snippets for Staff"
#staffpages
$RESEARCH_BIN/create_html.sh $RESEARCH_DEPLOY/home \
                             $RESEARCH_BUILD/home \
                             TUHH >> $RESEARCH_BUILD/build.log 2>&1
# wg pages 
echo "Creating HTML Snippets for WG"
for wg in aa cm dm nm st
do
    $RESEARCH_BIN/create_html.sh $RESEARCH_DEPLOY/forschung/$wg \
                                 $RESEARCH_BUILD/forschung/$wg \
                                 TUHH >> $RESEARCH_BUILD/build.log 2>&1
done

echo "Copying files..."
cp -r $RESEARCH_ROOT/img $RESEARCH_DEPLOY/forschung/topics
cp -r $RESEARCH_STATIC/include $RESEARCH_DEPLOY

echo "Done. Review file://$RESEARCH_DEPLOY/index.html ."
