#!/bin/bash

cd $(dirname $(readlink -f $0))/..

$(pwd)/xlunch --help 2>docs/usage2.txt
if [ $? -ne 0 ]; then
   rm docs/usage2.txt
   echo "Please compile xlunch first, so we can generate usage.txt by running it with --help argument"
   exit 1
fi
mv -f docs/usage2.txt docs/usage.txt

OLDMAJOR=$(cat xlunch.c | grep 'int VERSION_MAJOR' | sed -r 's/.*= *|;.*//g')
OLDMINOR=$(cat xlunch.c | grep 'int VERSION_MINOR' | sed -r 's/.*= *|;.*//g')
OLDPATCH=$(cat xlunch.c | grep 'int VERSION_PATCH' | sed -r 's/.*= *|;.*//g')

echo -ne "New MAJOR VERSION ($OLDMAJOR): "
read MAJOR
if [ "$MAJOR" = "" ]; then MAJOR=$OLDMAJOR; fi

echo -ne "New MINOR VERSION ($OLDMINOR): "
read MINOR
if [ "$MINOR" = "" ]; then MINOR=$OLDMINOR; fi

echo -ne "New PATCH VERSION ($OLDPATCH): "
read PATCH
if [ "$PATCH" = "" ]; then PATCH=$OLDPATCH; fi


sed -r -i "s/(int VERSION_MAJOR *= *)[0-9]+/\\1$MAJOR/" xlunch.c
sed -r -i "s/(int VERSION_MINOR *= *)[0-9]+/\\1$MINOR/" xlunch.c
sed -r -i "s/(int VERSION_PATCH *= *)[0-9]+/\\1$PATCH/" xlunch.c

sed -r -i "s:v[0-9]+.[0-9]+.[0-9]+:v$MAJOR.$MINOR.$PATCH:g" docs/index.html

git commit -a -m "Version update v$MAJOR.$MINOR.$PATCH"
git tag -m "Version release v$MAJOR.$MINOR.$PATCH" v$MAJOR.$MINOR.$PATCH
git push
git push --tags
