#!/bin/bash

SVN_URL="http://opengnsys.es/svn"
[ -z $SVN_DIR] && SVN_DIR="/tmp/opengnsys-svn"

[ -z $PREFIX ] && PREFIX="/"

for dir in \
    "$PREFIX"\
    "$PREFIX/opt"\
    "$PREFIX/opt/opengnsys"\
    "$PREFIX/opt/opengnsys/bin"\
    "$PREFIX/opt/opengnsys/etc" \
    "$PREFIX/opt/opengnsys/etc/init.d" \
    "$PREFIX/opt/opengnsys/lib" \
    "$PREFIX/opt/opengnsys/lib/python" \
    "$PREFIX/opt/opengnsys/images" \
    "$PREFIX/opt/opengnsys/log"
do
    [ -d "$dir" ] || (mkdir -p $dir && echo "Creating dir: $dir")
done


if [ ! -d $SVN_DIR ]
then
    echo "Getting the new engine"
    svn export -q $SVN_URL/branches/version2 $SVN_DIR
else
    echo "Updating engine"
    cd $SVN_DIR
    svn update -q
    cd "$OLDPWD"
fi

echo "Copying ogr"
cp -rf $SVN_DIR/ogr/ogr $PREFIX/opt/opengnsys/bin

echo "Copying libogr"
cp -rf $SVN_DIR/ogr/libogr.py $PREFIX/opt/opengnsys/lib/python

echo "Copying runtest"
cp -rf $SVN_DIR/runtest/runtest.py $PREFIX/opt/opengnsys/bin

echo "Copying engine"
cp -rf $SVN_DIR/engine $PREFIX/opt/opengnsys

echo "Copying job_executer"
cp -rf $SVN_DIR/job_executer $PREFIX/opt/opengnsys/
cp -rf $SVN_DIR/job_executer/init.d/job_executer $PREFIX/opt/opengnsys/etc/init.d

echo "Copying load_environ"
cp -rf $SVN_DIR/live/load_environ/load_environ.py $PREFIX/opt/opengnsys/etc/init.d/load_environ
