#/bin/bash

# Note: this can only be run on a "standard" Nagios XI development VM (where standard is "whatever
#       Sebastian is using today"), and should not be included in the final deliverable.

set -e

pushd /repos/nagiosxi/subcomponents/ndo

git checkout master
git checkout update-ndo
git clone git@git.nagios.com:open-source/ndo.git
cd ndo

# Default to most recent tag
version=`git tag -l | grep 3\. | tail -1`
if [ "x$1" != "x" ]; then
    version=$1
fi
git checkout $version
commit_hash=$(git rev-parse --short $version)

autoconf

rm -rf autom4te.cache
rm -rf .git
rm -rf prep-release

cd ..
mv ndo ndo-$version
git rm -f ndo-*.tar.gz
tar cvfz ndo-$version.tar.gz ndo-$version
rm -rf ndo-$version
sed -i 's/pkgname=.*/pkgname="ndo-'$version'"/' install
sed -i 's/pkgname=.*/pkgname="ndo-'$version'"/' upgrade
git add ndo-$version.tar.gz install upgrade
git commit -m "Update NDO to $version (open-source/ndo@$commit_hash)"
git push origin update-ndo -f

popd
