#!/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' name and version information
##

DOGTAG_PKI_THEME="dogtag-pki-theme"
PKI_SPECS_FILE="${PKI_DIR}/specs/${DOGTAG_PKI_THEME}.spec.in"
DOGTAG_PKI_THEME_VERSION="`rpmspec -P "${PKI_SPECS_FILE}" | grep '^Version:' | awk '{print $2;}'`"


##
## Establish the TARGET files/directories of the 'dogtag-pki' source/spec files
##

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

DOGTAG_PKI_THEME_BUILD_DIR="${PKI_PACKAGES}/BUILD"
DOGTAG_PKI_THEME_RPMS_DIR="${PKI_PACKAGES}/RPMS"
DOGTAG_PKI_THEME_SOURCES_DIR="${PKI_PACKAGES}/SOURCES"
DOGTAG_PKI_THEME_SPECS_DIR="${PKI_PACKAGES}/SPECS"
DOGTAG_PKI_THEME_SRPMS_DIR="${PKI_PACKAGES}/SRPMS"

DOGTAG_PKI_THEME_TARBALL="pki-${DOGTAG_PKI_THEME_VERSION}.tar.gz"
DOGTAG_PKI_THEME_SPEC_FILE="${DOGTAG_PKI_THEME_SPECS_DIR}/${DOGTAG_PKI_THEME}.spec"

DOGTAG_PKI_THEME_STAGING_DIR="${PKI_PACKAGES}/staging"
DOGTAG_PKI_THEME_DIR="${DOGTAG_PKI_THEME_STAGING_DIR}/${DOGTAG_PKI_THEME}-${DOGTAG_PKI_THEME_VERSION}"
DOGTAG_PKI_THEME_DOGTAG_DIR="${DOGTAG_PKI_THEME_DIR}/dogtag"


##
## Always create a top-level 'packages' directory
##

mkdir -p ${PKI_PACKAGES}



##
## Always create 'dogtag-pki' package directories
##

mkdir -p ${DOGTAG_PKI_THEME_BUILD_DIR}
mkdir -p ${DOGTAG_PKI_THEME_RPMS_DIR}
mkdir -p ${DOGTAG_PKI_THEME_SOURCES_DIR}
mkdir -p ${DOGTAG_PKI_THEME_SPECS_DIR}
mkdir -p ${DOGTAG_PKI_THEME_SRPMS_DIR}


##
## Always start with new 'dogtag-pki' package files
##

rm -rf ${DOGTAG_PKI_THEME_BUILD_DIR}/${DOGTAG_PKI_THEME}-${DOGTAG_PKI_THEME_VERSION}
rm -f  ${DOGTAG_PKI_THEME_RPMS_DIR}/${DOGTAG_PKI_THEME}-${DOGTAG_PKI_THEME_VERSION}*.rpm
rm -f  ${DOGTAG_PKI_THEME_SOURCES_DIR}/${DOGTAG_PKI_THEME_TARBALL}
rm -f  ${DOGTAG_PKI_THEME_SPEC_FILE}
rm -f  ${DOGTAG_PKI_THEME_SRPMS_DIR}/${DOGTAG_PKI_THEME}-${DOGTAG_PKI_THEME_VERSION}*.rpm


##
## Compute build options
##

compute_build_options


##
## Insert timestamp and commit ID while copying spec template into actual spec file.
##

sed "s/%{?_timestamp}/${_TIMESTAMP}/g; s/%{?_commit}/${_COMMIT}/g" \
    ${PKI_SPECS_FILE} > ${DOGTAG_PKI_THEME_SPEC_FILE}


##
## Create the 'dogtag-pki' tarball
##

tar czf \
 ${DOGTAG_PKI_THEME_SOURCES_DIR}/${DOGTAG_PKI_THEME_TARBALL} \
 --transform "s,^./,pki-${DOGTAG_PKI_THEME_VERSION}/," \
 --exclude .git \
 --exclude .svn \
 --exclude .swp \
 --exclude .metadata \
 --exclude build \
 --exclude .tox \
 --exclude dist \
 --exclude MANIFEST \
 --exclude *.pyc \
 --exclude __pycache__ \
 -C ${PKI_DIR} \
 .


##
## Automatically invoke RPM/SRPM creation
##

cd ${PKI_PACKAGES}
rpmbuild \
  ${RPMBUILD_OP} \
  --define "_topdir ${PKI_PACKAGES}" \
  "${OPTIONS[@]}" \
  ${DOGTAG_PKI_THEME_SPEC_FILE} \
  | tee package_${DOGTAG_PKI_THEME}.log 2>&1

