#!/bin/sh
#
# Simple script to build a binary rpm from the src rpm.
#

# Source rpm file should be first arg to script (i.e. package-version-release.src.rpm):
SRC_RPM_FILE=$1

# Install prefix should be the second arg (i.e. /usr or /opt/usatlas):
PREFIX=$2

# Toplevel build directory:
#  - Should this be a temporary directory which will be deleted, or something that pacman will always use?
BUILD_DIR="/tmp/rpm-pacman-build-$$"

# What arch am I building on?
#  - Returns the currently defined arch in /usr/lib/rpm/macros or some user defined arch which overrides this.
#  - What about the possibility of optomized packages, like i686?
ARCH=`rpm -qp --queryformat '%{ARCH}' $SRC_RPM_FILE`

# Initial setup (a directory structure like this is needed):
mkdir -p $BUILD_DIR/SRPMS $BUILD_DIR/SPECS $BUILD_DIR/BUILD \
 $BUILD_DIR/SOURCES $BUILD_DIR/RPMS/$ARCH $BUILD_DIR/RPMS/noarch

# Verify the download (should check return code for error):
#  - Should we make gpg signed packages for enhanced security?
rpm --checksig $SRC_RPM_FILE

# Build the binary package:
#  - Might just be easier to make my own macro file that defines these options and reference that (with --macros or --rcfile ???).
#  - I added a hook in my globus packages to change their install prefix with the altprefix define, all packages which define
#    their own prefix like this would need a similar hook.
rpm --define="_topdir $BUILD_DIR" --define="_builddir $BUILD_DIR/BUILD" \
    --define="_rpmdir $BUILD_DIR/RPMS" --define="_sourcedir $BUILD_DIR/SOURCES" \
    --define="_specdir $BUILD_DIR/SPECS" --define="_srcrpmdir $BUILD_DIR/SRPMS" \
    --define="_prefix $PREFIX" --define "altprefix $PREFIX" --rebuild $SRC_RPM_FILE

# Then install the resulting package:
#  - should be the only step that might have to be done by root or should pacman do this step?
#  - NOTE: Some src rpms might create multiple binary rpms and maybe an arch independent rpm.
rpm -U $BUILD_DIR/RPMS/$ARCH/*.$ARCH.rpm
rpm -U $BUILD_DIR/RPMS/noarch/*.$ARCH.rpm

# Finally clean-up our temporary build area (or should this be a permanent location that pacman always uses?):
#  - Do this after the above rpm(s) has been installed or it will be removed too.
rm -rf $BUILD_DIR

#
# End file.
#

