
version=$1
major=`echo $version|cut -d. -f1`
minor=`echo $version|cut -d. -f2`
release=`echo $version|cut -d. -f3`
release_number=`echo $version|cut -d. -f3|sed -e 's/\([0-9]*\).*/\1/'`
revision=`echo $version|cut -d. -f4`
release_type=$2

if test "$version" = ""; then
  echo "$0 <major.minor.release.revision> [<release-type>]"
  exit 1
fi

if test "$revision" = ""; then
  echo "$0 <major.minor.release.revision> [<release-type>]"
  exit 1
fi

if test x$release_type != x; then
    dot_release_type=.$release_type
    space_release_type=" $release_type"
else
    dot_release_type=
    space_release_type=
fi

echo "Updating configure.in"
echo "AC_INIT(mysql-workbench, $major.$minor.$release$dot_release_type)" > configure.in.new

grep -v AC_INIT configure.in >> configure.in.new

mv -f configure.in.new configure.in


echo "Updating wb_context.h"
sed -i -e "s/#define APP_MAJOR_NUMBER.*/#define APP_MAJOR_NUMBER $major/" \
   -e "s/#define APP_MINOR_NUMBER.*/#define APP_MINOR_NUMBER $minor/" \
   -e "s/#define APP_RELEASE_NUMBER.*/#define APP_RELEASE_NUMBER $release_number/" \
   -e "s/#define APP_RELEASE_TYPE.*/#define APP_RELEASE_TYPE \"$release_type\"/" \
   -e "s/#define APP_REVISION_NUMBER.*/#define APP_REVISION_NUMBER $revision/" backend/wbprivate/workbench/wb_context.h 

echo "Updating AssemblyInfo.cs"
sed -i -e "s/\[assembly: AssemblyVersion(.*)\]/[assembly: AssemblyVersion(\"$version\")]/"\
        -e "s/\[assembly: AssemblyFileVersion(.*)\]/[assembly: AssemblyFileVersion(\"$version\")]/" \
        -e "s/\[assembly: AssemblyReleaseType(.*)\]/[assembly: AssemblyReleaseType(\"$release_type\")]/" \
  frontend/windows/MySQLWorkbench/Properties/AssemblyInfo.cs

echo "Updating Info.plist"
cat Info.plist | sed -e "/<key>CFBundleVersion<\/key>/,/<\/string>/c \
	<key>CFBundleVersion</key>\n	<string>$major.$minor.$release$dot_release_type</string>" > Info.plist.new
mv -f Info.plist.new Info.plist

# check if build/debian/changelog needs update
echo "Checking if build/debian/changelog needs updating"
if ! grep $major.$minor.$release- build/debian/changelog; then
	echo "build/debian/changelog needs update, adding a new entry"
	cat <<EOF > build/debian/changelog.new
mysql-workbench ($major.$minor.$release$dot_release_type-1) unstable; urgency=low

  * New release

 -- `bzr whoami`  `date -R`

EOF
	cat build/debian/changelog >> build/debian/changelog.new
	mv -f build/debian/changelog.new build/debian/changelog
	head -10 build/debian/changelog
fi
