#!/bin/bash
# script running on the wesnoth server to update units.wesnoth.org
set -e -u

# set them in the environment
# VERSION=1.10
# PORT=1.10.x

# must run from the correct directory
DIR="$(dirname $0)"

SOURCES=/usr/src/wesnoth
ADDONS=$DIR/addons
EXE=$DIR/build/wesnoth
CONFIG=$DIR/config
SRC=$SOURCES/$VERSION
DATA=$SRC
LOG=$DIR/logs
TMPOUT=$DIR/output
OUT="elias@192.168.1.2:/srv/www/html/units/$VERSION"
TRANS=$DIR/translations
BUILD=$DIR/build
# always using the master version
TOOLS=$SOURCES/master/data/tools
export TEMP=$DIR/temp

mkdir -p $CONFIG/data/add-ons
mkdir -p $ADDONS
mkdir -p $LOG
mkdir -p $TMPOUT
mkdir -p $TEMP
mkdir -p translations

rm -rf $TEMP/*

# we only need the parser, but have to build the game executable for that
echo BUILDING
make -C $BUILD wesnoth > /dev/null 2>&1 || true
make -C $BUILD mo-update > /dev/null 2>&1

# later can just rebuild updated campaigns, but for now rebuild everything
rm -f $DIR/overview.txt

echo DOWNLOADING
$TOOLS/wesnoth_addon_manager -p $PORT -d '.*' -c $ADDONS > $LOG/wesnoth_addon_manager.txt 2>&1 &&
echo WORKING
python -u $TOOLS/wmlunits -t $TRANS -D $DATA -o $TMPOUT -w $EXE -C $CONFIG -a $ADDONS -L $DIR/overview.txt -B $DIR/overview.txt > $LOG/wmlunits.txt 2>&1 &&
rsync -vaz --delete $TMPOUT/ $OUT > $LOG/rsync.txt 2>&1 &&
rm -rf $TEMP/* &&
rm -rf $TMPOUT

