#!/bin/bash

BASE="master"

DATENUM="`date +%Y%m%d`"

VERSTR="$1"
if [ "${VERSTR}" = "today" ]
then
	VERSTR="${DATENUM}"
fi

FULLVERSTR="${BASE}_${VERSTR}"
if [[ "${VERSTR}" =~ ^[0-9.]{1,7}(_.*)$ ]]
then
	FULLVERSTR="${BASE}.${VERSTR}"
fi

GITBRANCH="todo/${FULLVERSTR}"

A=$'\033[1m'
B=$'\033[0m'

ERROR=0

if [ "${VERSTR}" = "" ]
then
	echo -ne "${A}Usage${B}:\n\n\tTo create a todo/${BASE}.3_beta15 branch:\n\t\t$0 3_beta15\n\n\tTo create a todo/${BASE}_${DATENUM} branch\n\t\t$0 today\n\n"
	ERROR=1
fi

if ! git diff --quiet HEAD 2> /dev/null
then
	echo -ne "${A}Error${B}:\n\tYour working copy is dirty.  Afraid of messing\n\tsomething up, so commit anything that needs to\n\tbe committed,  and revert anything  that needs\n\tto be reverted, to make your working copy nice\n\tand clean.\n\n"
	ERROR=1
fi

if [ ! -f po/Makefile ]
then
	echo -ne "${A}Error${B}:\n\tThis seems not to be a build directory,  which\n\tis needed  for "\""make -Cpo"\"" update-po  to work.\n\tChange  to your favourite  build directory and\n\trun this script from there.\n\n"
	ERROR=1
fi

if ! GITROOT="`git rev-parse --show-cdup 2> /dev/null`"
then
	echo -ne "${A}Error${B}:\n\tNot in a git repository.\n\n"
	ERROR=1
fi

if [ "${VERSTR}" = "bunny" ]
then
	echo -ne "${A}Error${B}:\n\t       ${A}(\\_/)${B}\n\tSorry, ${A}(O.o)${B} is not a valid version number.\n\t       ${A}(> <)${B}\n\n"
fi

if [ ${ERROR} -ne 0 ]
then
	exit 1
fi

echo "${A}Creating ${GITBRANCH} branch.${B}"
if ! git checkout -b "${GITBRANCH}"
then
	echo -ne "Error:\n\tCould not create branch ${GITBRANCH}.\n\n"
	exit 1
fi


echo "${A}make -Cpo update-po${B}"
make -Cpo update-po
git commit -am "make -Cpo update-po"


echo "${A}Bumping version strings to ${FULLVERSTR} and netcode version to ${DATENUM}.${B}"

sed -i 's/\(AC_INIT(\[Warzone 2100\],\[\)'"${BASE}"'\(\],\[http:\/\/wz2100.net\/\],\[warzone2100\])\)/\1'"${FULLVERSTR}"'\2/;
        s/\(\[ enable_debug=${enableval} \], \[ enable_debug=\)yes\( \])\)/\1no\2/
       ' "${GITROOT}"configure.ac
sed -i 's/\(static char const \*versionString = \).*\(;\)/\1"'"${FULLVERSTR}"'"\2/;
        s/\(static int NETCODE_VERSION_MINOR = \)[0-9]*\(;\)/\1'"${DATENUM}"'\2/
       ' "${GITROOT}"lib/netplay/netplay.c*
sed -i 's/\(VALUE "FileVersion", "\)'"${BASE}"'\("\)/\1'"${FULLVERSTR}"'\2/;
        s/\(VALUE "ProductVersion", "\)'"${BASE}"'\("\)/\1'"${FULLVERSTR}"'\2/;
       ' "${GITROOT}"win32/warzone2100.rc "${GITROOT}"win32/warzone2100.vs2k5.rc "${GITROOT}"win32/warzone2100.vs2k8.rc

git commit -am "Bumping version strings to ${FULLVERSTR} and netcode version to ${DATENUM}."


echo "${A}All done.${B}"

echo "${A}If you liked the result, you may also like:${B}"

if [ ! -d "${GITROOT}"build ]
then
	echo $'\t'mkdir -p "${GITROOT}"build
fi
echo $'\t'cd "${GITROOT}"build
echo $'\t'../autogen.sh
echo $'\t'../configure --with-distributor="wz2100.net"
echo $'\t'make -j4
echo $'\t'make -j4 dist
echo $'\t'tar -xzf [whateverTheFileIsCalled].tar.gz
echo $'\t'cd [whateverTheDirectoryIsCalled]/win32
echo $'\t'./__BUILD_SCRIPT --enable-installer --with-distributor="wz2100.net"
