#!/bin/sh
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 2 of the License, or (at your
# option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
# 
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.

cd "`dirname $0`/../.."
STRING="3.8"

# To build the debs and skip the signature part
BUILD_CMD="debuild -i -us -uc -b"

# Test if version replace can occur
if ! grep -m1 -q $STRING debian/changelog ; then
    echo "Unable to find the string \"$STRING\" used for "
    echo "automatic version number replacing."
    echo
    echo "To use this script, copy the file debian/changelog.in"
    echo "to debian/changelog and run this script again."
    echo
    echo "OR"
    echo
    echo "Manually edit debian/changelog with the appropriate value and run"
    echo "\"$BUILD_CMD\""
    exit 1
fi

# Use the latest version in CHANGES and append the date
VERSION="`grep -m1 "^v" CHANGES | cut -dv -f2 | cut -d" " -f1`"

# Update debian/changelog to make debuild/dpkg-buildpackage happy
sed -i -e "s/$STRING/$VERSION/g" -e "/^libreswan/ s/-.*xelerance.*)/~1git`date +%Y%m%d`)/" debian/changelog debian/NEWS

echo "Building Libreswan version $(dpkg-parsechangelog | grep -m1 '^Version:' | cut -d' ' -f2 | cut -d: -f2)"
$BUILD_CMD
