#!/bin/bash

# $Id: mkdeb,v 1.6 2004/01/09 20:29:28 type2 Exp $

#  
#  Prospect: a developer's system profiler.
#  Debian (deb) file distribution generator.
#
#  COPYRIGHT (C) 2001-2004 Hewlett-Packard Company
# 
#  Author: Al Stone, HP
#
#  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.
# 
#  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.
# 
#  You should have received a copy of the GNU General Public License along with
#  this program; if not, write to the Free Software Foundation, Inc., 59 Temple
#  Place - Suite 330, Boston, MA 02111-1307, USA.
#

# This is run as a result of a "make deb" in the prospect main directory.
# This script should live in "scripts".  If this script is given a -i option,
# it will increment the Debian package revision number.

# Extract the revision, create a proper debian/changelog, then invoke
# dpkg-buildpackage.  NB: *always* use -us and -uc options when running
# dpkg-buildpackage -- if a package is to be signed -- which it must be
# to upload into the official Debian repository -- do it by hand so it
# be signed properly.

#######################################################################
#
#	Idiot checks

set -u

if [ `whoami` != root ]; then
	echo "You must be root to do this" >&2
	exit 1
fi

if [ ! -e prospect/version.h ]; then
    echo "Run this while in the top-level prospect directory" >&2
    exit 1
fi

#######################################################################
#
#	See if we need to increment the Debian package revision or not
INCR=n
[ "$#" -gt 1 ] && [ $1 = "-i" ] || INCR=y

#######################################################################
#
#	Set up dpkg-buildpackages options
DBLDOPTS="-us -uc -ICVS"

#######################################################################
#
#	Extract the revision

target=`awk '/^#define[ ]+cREV/ {printf "%s=%s", $2,$3;}' prospect/version.h`
if [ -z "$target" ]; then
    echo "Cannot extract version info from prospect/version.h" >&2
    exit 2
fi
eval "$target"
if [ -z "$cREV" ]; then
    echo "Cannot create version variable from prospect/version.h" >&2
    exit 2
fi
set -- `echo $cREV | tr '.' ' ' `
if [ "$1.$2.$3" != "$cREV" ]; then
    echo "Version variable from prospect/version.h is not format x.y.z" >&2
    exit 2
fi
# debian doesn't allow underscores in the version number
NAMEREV=$(echo "$cREV" | sed 's/_/-/g')

#######################################################################
#
#	Figure out what the debian package version number is
DEBVER=$( head -1 debian/changelog | cut -d'(' -f2 | cut -d')' -f1 )
DEBEPOCH=$( echo $DEBVER | cut -d: -f1 )
DEBREV=$( echo $DEBVER | cut -d- -f3 )
[ "$DEBREV"X != "X" ] || DEBREV=$( echo $DEBVER | cut -d- -f2 )

if [ "$INCR" = "y" ]; then
   DEBREV=$( expr $DEBREV + 1 )
fi

#echo debver $DEBVER epoc $DEBEPOCH rev $DEBREV

#######################################################################
#
#	Figure out what the debian package version number should be
if [ "$DEBVER" = "$NAMEREV" ]; then
   DEBSTR="${DEBEPOCH}:${DEBVER}-${DEBREV}"
else
   DEBSTR="${DEBEPOCH}:${NAMEREV}-1"
fi

#echo debstr $DEBSTR

#######################################################################
#
#	Attach entry to beginning of debian/changelog
cp debian/changelog /tmp/DCLOG$$

echo "prospect ($DEBSTR) unstable; urgency=low

  * Automatic build

 --  <$(whoami)@$(hostname)>  $(date -R)
" > debian/changelog

cat /tmp/DCLOG$$ >> debian/changelog

#######################################################################
#
#	Build the package
dpkg-buildpackage $DBLDOPTS

#######################################################################
#
#       Cleanup and then reset debian/changelog to original text
debian/rules clean
mv /tmp/DCLOG$$ debian/changelog

