#!/bin/sh
# BEGIN COPYRIGHT BLOCK
# (C) 2010 Red Hat, Inc.
# All rights reserved.
# END COPYRIGHT BLOCK

##
## Include common 'compose' functions
##

COMPOSE_PWD=`dirname $0`
. ${COMPOSE_PWD}/compose_functions


## Always switch into the base directory three levels
## above this shell script prior to executing it so
## that all of its output is written to this directory

cd `dirname $0`/../..


##
## Retrieve the name of this base directory
##

PKI_PWD=`pwd`

##
## Establish the 'dogtag-pki' spec file
##

DOGTAG_PKI_META="dogtag-pki"
PKI_SPECS_FILE="${PKI_DIR}/specs/${DOGTAG_PKI_META}.spec.in"

##
## Establish packages directory for 'dogtag-pki' META packages
##

if [ $WORK_DIR ]; then
    PKI_PACKAGES="`cd $WORK_DIR ; pwd`"
else
    PKI_PACKAGES="${PKI_PWD}/packages"
fi


##
## Specify 'dogtag-pki' META functions
##

create_packages_directory()
{
	printf "Creating top-level 'packages' directory . . . "
	mkdir -p ${PKI_PACKAGES}
	printf "done.\n"
}

create_META_package_directories()
{
	printf "Creating 'dogtag-pki' META package directories . . . "
	mkdir -p ${PKI_PACKAGES}
	mkdir -p ${PKI_PACKAGES}/BUILD
	mkdir -p ${PKI_PACKAGES}/BUILDROOT
	mkdir -p ${PKI_PACKAGES}/RPMS
	mkdir -p ${PKI_PACKAGES}/SOURCES
	mkdir -p ${PKI_PACKAGES}/SPECS
	mkdir -p ${PKI_PACKAGES}/SRPMS
	printf "done.\n"
}

build_specified_META_package()
{
	package=$1
	spec_file_path="${PKI_PWD}/pki/specs"
	spec_file="${package}.spec.in"

	printf "BEGIN: Building '${package}' META package . . .\n"
	compute_build_options
	cp ${spec_file_path}/${spec_file} ${PKI_PACKAGES}/SPECS/${package}.spec
	rpmbuild \
	    -ba \
	    --define "_topdir ${PKI_PACKAGES}" \
	    --clean \
	    ${PKI_PACKAGES}/SPECS/${package}.spec \
	    2>&1
	printf "END:   Building '${package}' META package.\n"
}


##
## Build the 'dogtag-pki' META package
##

create_packages_directory
create_META_package_directories
build_specified_META_package dogtag-pki

