#!/bin/bash

SPECDIR=`rpm --eval %_specdir`
SRCDIR=`rpm --eval %_sourcedir`
USERID=`id -u`
GRPID=`id -g`
SPECFILE="likewise-open.spec"

if [ ! -n "$1" ]; then
	echo "Usage: $0 <redhat | suse>"
	exit 1
fi
case "$1" in
	redhat|suse)
		DISTRO=$1
		;;
	*)
		echo "Valid distro names are \"redhat\" or \"suse\""
		exit 1
		;;
esac

## get utility functions
. `dirname $0`/functions
VERSION=$(get_lwopen_version)
if [ "x${VERSION}" = "x" ]; then
	echo "Failed to get Likewise Open Version."
	exit 1
fi
. `dirname $0`/../${DISTRO}/release-info

## create the %_topdir heirarchy
create_rpmbuild_directory || exit 1

cd `dirname $0`

(cd ../${DISTRO} && tar cvf - setup | gzip -9 > ${SRCDIR}/setup.tar.gz) || exit 1
cp -p ../../../likewise-open-${VERSION}.tar.gz ${SRCDIR}/ || exit 1
cat ../${DISTRO}/${SPECFILE} |\
	sed -e s/__RPM_VER/${VERSION}/g \
		-e s/__RPM_RELEASE/${RELEASE}/g > ${SPECDIR}/${SPECFILE} || exit 1

RPM_ARGS=--define='enablegui 1'
GTK_MINOR_VER=`rpm -q --qf "%{VERSION}\n"  gtk2-devel | cut -d. -f2`
if [ "x$GTK_MINOR_VER" = "x" -o $GTK_MINOR_VER -lt 6 ]; then
	echo "likewise-open GUI requires GTK 2.6 or later.  Disabling GTK join tool."
	RPM_ARGS=--define='enablegui 0'
fi

rpmbuild "${RPM_ARGS}" -ba ${SPECDIR}/${SPECFILE} 



