#!/bin/bash

#
# ET:Legacy Omni-Bot updater - Update archives on mirror.etlegacy.com/omnibot
#
SVN_USERNAME=${SVN_USERNAME:-}
SVN_PASSWORD=${SVN_PASSWORD:-}

SVN_AUTH='--non-interactive'

if [ ! -z "$SVN_USERNAME" ]; then
    SVN_AUTH="$SVN_AUTH --username $SVN_USERNAME"
fi

if [ ! -z "$SVN_PASSWORD" ]; then
    SVN_AUTH="$SVN_AUTH --password $SVN_PASSWORD"
fi

function getRevision {
    svn info $SVN_AUTH $1 | sed -ne 's/^Revision: //p'
}

REPO_URL=https://subversion.assembla.com/svn/omnibot/Enemy-Territory/0.8
REVISION_REMOTE=$(getRevision ${REPO_URL})

echo "Latest Omni-bot revision is" ${REVISION_REMOTE}

if [ -d omni-bot ]; then
    REVISION_LOCAL=$(getRevision omni-bot)
    echo "Local Omni-bot revision is" ${REVISION_LOCAL} 

    if [ ${REVISION_REMOTE} -le ${REVISION_LOCAL} ]; then
	echo "Nothing to do."
	exit 
    fi
fi

svn checkout ${SVN_AUTH} --depth empty ${REPO_URL} omni-bot
svn update ${SVN_AUTH} --set-depth infinity omni-bot/et
#svn update ${SVN_AUTH} -q --set-depth=exclude omni-bot/et/incomplete_navs
svn update ${SVN_AUTH} --set-depth infinity omni-bot/global_scripts
svn update ${SVN_AUTH} omni-bot/omnibot_et.so
svn update ${SVN_AUTH} omni-bot/omnibot_et.x86_64.so
svn update ${SVN_AUTH} omni-bot/omnibot_et.dll
svn update ${SVN_AUTH} omni-bot/omnibot_et_x64.dll

rm omnibot-linux-latest.tar.gz
tar --exclude '.svn/*' --exclude '.svn' -zcvf omnibot-linux-latest.tar.gz omni-bot

rm omnibot-windows-latest.zip
zip -r omnibot-windows-latest.zip omni-bot -x *.svn*

rm omnibot-macos-latest.tar.gz
tar --exclude '.svn/*' --exclude '.svn' --exclude '*.dll' --exclude '*.so' -cf omnibot-macos-latest.tar omni-bot
tar -rf omnibot-macos-latest.tar omni-bot/omnibot_et_mac.so
gzip omnibot-macos-latest.tar
