#!/bin/bash

# $Id: mkrpm,v 1.4 2004/01/09 20:29:28 type2 Exp $

#  
#  Prospect: a developer's system profiler.
#  RPM file distribution generator.
#
#  COPYRIGHT (C) 2001-2004 Hewlett-Packard Company
# 
#  Author: Rocky Craig, 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 rpm" in the prospect main directory.
# This script should live in "scripts".

# Extract the revision, create a fresh tarball, emit a spec file and go.

#######################################################################
#
#	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

#######################################################################
#
#	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
NAMEREV="prospect-$cREV"

#######################################################################
#
#	Establish the build area.  The first test might be too harsh on 
#	non-RedHat systems...

if [ ! rpm -q rpm-build >/dev/null 2>&1 ]; then
	echo "rpm-build package is not installed on this system" >&2
	exit 3
fi
if [ -f /etc/redhat-release ]; then
	TOPDIR=/usr/src/redhat
else
	TOPDIR=/usr/src/rpm
fi
RPMSOURCES=$TOPDIR/SOURCES		# These are referenced elsewhere...
RPMSPECS=$TOPDIR/SPECS
if [ ! -d $TOPDIR ]; then
	mkdir -p $RPMSPECS
	mkdir -p $RPMSOURCES
	mkdir -p $TOPDIR/BUILD		# ...while these are not
	mkdir -p $TOPDIR/SRPMS
	for a in noarch i386 ia64; do mkdir -p $TOPDIR/RPMS/$a; done
fi

#######################################################################
#
#	To be RPM-pure, the top level project directory needs the full name.
#	Assume this script was run from makefile, and PWD is still the main
#	project directory.

echo "Executing 'make distclean'..."
make distclean > /dev/null 2>&1

echo "Creating fresh tarball..."
TLD=$RPMSOURCES/$NAMEREV
TARBALL=$NAMEREV.tar.gz
rm -rf $TLD >/dev/null 2>&1
mkdir -p $TLD
cp -dpR * $TLD
pushd $RPMSOURCES > /dev/null
tar -zcf /tmp/$TARBALL --exclude CVS --exclude .cvsignore --exclude RPMS $NAMEREV
rm -r $NAMEREV						# Currently in $RPMSOURCES
mv /tmp/$TARBALL .
popd > /dev/null					# Back in prospect main

#######################################################################
#
#	Emit the spec file, starting with the preamble.  Before that,
#   set up the share directory

SPECFILE=$RPMSPECS/$NAMEREV.spec
echo "Creating RPM spec file $SPECFILE ..."

cat <<EOPREAMBLE > $SPECFILE
#
# RPM spec file for Prospect System Analysis Tool
#
%define name prospect
%define version $cREV
%define release 1

Name: %{name}
Version: %{version}
Release: %{release}
Summary: A system-wide, sampling, profiling tool
Copyright: GPL
Group: Development/Tools
Source: %{name}-%{version}.tar.gz
URL: http://sourceforge.net/projects/prospect
Distribution: Any
Vendor: The New Hewlett-Packard
Packager: Rocky Craig <coloroco@sourceforge.net>

%description
Prospect is an instruction pointer sampling flat profiler for obtaining 
code profiles in a non-intrusive way.  One can obtain profiles (both 
symbol-level and assembly-level) without undue requirements on the target 
application.  For example, there is no need to specially instrument the 
application and there is no need to rebuild or relink. In fact, the only 
requirement is that the application be not stripped. Shared libraries 
escape even this requirement for the most part.

Prospect accomplishes this through using the oprofile sampling module 
released under GPL by John Levon while at the Victoria University of 
Manchester, UK.  See http://oprofile.sourceforge.net/ for details.

If you are a Prospect developer, you will also need the libelf library.
Use at least revision 0.7.0.  Start looking at http://rpmfind.net .

EOPREAMBLE

#######################################################################
#
#	Building from source.  The install section is lifted from the
#   prospect/Makefile section.  For development, Alex wants it in
#	/usr/local/bin.  For deployment (binary RPM) he wants it in /usr/bin/.
# 	Until the makefile is changed, track the install target operations
#	in here.  Remember at each scriptlet, rpm changes PWD appropriately.

cat <<EOBUILD >> $SPECFILE

%prep
%setup

%build
make

%install
# Do not do "make install"!
cp prospect/prospect /usr/bin
chown root:root /usr/bin/prospect
chmod 4555 /usr/bin/prospect
mkdir -p /usr/share/man/man1
cp docs/prospect.1 /usr/share/man/man1

EOBUILD

#######################################################################
#
#	Close out the spec file.  Until the %post script has run, and the
#	tail end of THIS script, the files list is lies.

DOCDIR=/usr/share/doc/$NAMEREV

cat <<EOFILES >> $SPECFILE

%files
/usr/bin/prospect
/usr/share/man/man1/prospect.1
$DOCDIR 

%changelog
* `date +"%a %b %d %Y"` `whoami` <`whoami`@`hostname`>
- Automated build

* Wed Jun 25 2003 Alex Tsariounov <type2@users.sf.net>
- Change to shared build (from static) on RPM systems.

* Wed Apr 2 2003 Alex Tsariounov <type2@users.sf.net>
- Update for new package contents

* Wed Jun 12 2002 Rocky Craig <coloroco@sourceforge.net>
- Original packaging

EOFILES

#######################################################################
#
#	Populate the share directory (so the %files list doesn't lie :-)
#	and do the deed.  PWD is still the main project directory

echo "Building prospect from the tarball and creating RPMs..."
rm -rf $DOCDIR
mkdir -p $DOCDIR
for i in README* NEWS DISCLAIMER COPYING AUTHORS 
do
	cp $i $DOCDIR >/dev/null		# but keep stderr
done
# copy over kern_supp things
mkdir $DOCDIR/kernel_support
for i in kern_supp/* 
do
    cp -r $i $DOCDIR/kernel_support >/dev/null
done
# remove CVS dirs
find $DOCDIR/kernel_support -name CVS | xargs rm -rf >/dev/null

#outfiles=`rpm -ba $SPECFILE 2>/dev/null | grep 'Wrote:'`
outfiles=`rpm -ba $SPECFILE | grep 'Wrote:'`
if [ -n "$outfiles" ]; then
	echo $outfiles | tr ' ' '\n'
	exit 0
fi
echo rpm failed to build the RPMs
exit 4


