#!/bin/bash

if [ "x$GSDL3SRCHOME" = "x" ] ; then
  full_setup="${PWD}/${BASH_SOURCE}"
  fulldir="${full_setup%/*}"
  fulldir="${fulldir%/.}"
  cd ../../../
  . ./gs3-setup.sh
  cd "$fulldir"
fi

export FUSEKI_HOME=$GEXT_JENA/packages/apache-jena-fuseki-3.17.0

echo ""

if [ ! -d run3 ] ; then
    echo "Creating directory:"
    echo "  run3"
    mkdir run3
fi

if [ ! -f run3/config.ttl ] || [ run3.in/config.ttl -nt run3/config.ttl ] ; then
    echo "Copying config.ttl"
    echo "  run3.in/config.ttl -> run3/config.ttl"
    /bin/cp run3.in/config.ttl run3/config.ttl
fi

if [ ! -d run3/templates ] ; then
    echo "Copying templates dir (customised setting unionDefaultGraph to true):"
    echo "    run3.in/templates/* run3/templates/."
    if [ ! -d run3/templates ] ; then
	mkdir run3/templates
    fi
    /bin/cp run3.in/templates/* run3/templates/.
fi


for full_f in run3.in/templates/* ; do
    f=${full_f##*/}

    if [ run3.in/templates/$f -nt run3/templates/$f ] ; then
	echo "Updating tempaltes config file"
	echo "    run3.in/templates/$f run3/templates/$f"
	/bin/cp run3.in/templates/$f run3/templates/$f
    fi
done

if [ ! -f run3/shiro.ini ] || [ run3.in/shiro.ini -nt run3/shiro.ini ] ; then
    echo "Copying customized runtime shiro authentication file:"
    echo "  run3.in/shiro.ini -> run3/shiro.ini"
    /bin/cp run3.in/shiro.ini run3/shiro.ini
fi


if [ ! -f "$FUSEKI_HOME/fuseki.war" ] ; then
    echo "" >&2
    echo "Error: Failed to find $FUSEKI_HOME/fuseki.war" >&2
    echo "Have you run ./CASCASE-MAKE.sh?"  >&2
    echo "" >&2
    exit 1
fi

if [ ! -f "$GSDL3SRCHOME/packages/tomcat/webapps/fuseki3.war" ] \
       || [ "$FUSEKI_HOME/fuseki.war" -nt "$GSDL3SRCHOME/packages/tomcat/webapps/fuseki3.war" ] ; then

    echo ""
    echo "Copying: "
    echo "    $FUSEKI_HOME/fuseki.war"
    echo "    -> $GSDL3SRCHOME/packages/tomcat/webapps/fuseki3.war"

    /bin/cp "$FUSEKI_HOME/fuseki.war" "$GSDL3SRCHOME/packages/tomcat/webapps/fuseki3.war"
else
    echo ""
    echo "Installed war file is up to date"
fi


echo ""

echo "****"
echo "* If this is the first time you've install fuseki3.war, now run:"
echo "*     ./CREATE-GREENSTONE-DATASET.sh"
echo "****"
echo ""

