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

PKI_CONSOLE="pki-console"
PKI_SPECS_FILE="${PKI_DIR}/specs/${PKI_CONSOLE}.spec.in"
PKI_CONSOLE_VERSION="`rpmspec -P "${PKI_SPECS_FILE}" | grep '^Version:' | awk '{print $2;}'`"


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

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

PKI_CONSOLE_BUILD_DIR="${PKI_PACKAGES}/BUILD"
PKI_CONSOLE_RPMS_DIR="${PKI_PACKAGES}/RPMS"
PKI_CONSOLE_SOURCES_DIR="${PKI_PACKAGES}/SOURCES"
PKI_CONSOLE_SPECS_DIR="${PKI_PACKAGES}/SPECS"
PKI_CONSOLE_SRPMS_DIR="${PKI_PACKAGES}/SRPMS"

PKI_CONSOLE_TARBALL="pki-${PKI_CONSOLE_VERSION}.tar.gz"
PKI_CONSOLE_SPEC_FILE="${PKI_CONSOLE_SPECS_DIR}/${PKI_CONSOLE}.spec"

PKI_CONSOLE_STAGING_DIR="${PKI_PACKAGES}/staging"
PKI_CONSOLE_DIR="${PKI_CONSOLE_STAGING_DIR}/${PKI_CONSOLE}-${PKI_CONSOLE_VERSION}"
PKI_CONSOLE_BASE_DIR="${PKI_CONSOLE_DIR}/base"


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

mkdir -p ${PKI_PACKAGES}


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

mkdir -p ${PKI_CONSOLE_BUILD_DIR}
mkdir -p ${PKI_CONSOLE_RPMS_DIR}
mkdir -p ${PKI_CONSOLE_SOURCES_DIR}
mkdir -p ${PKI_CONSOLE_SPECS_DIR}
mkdir -p ${PKI_CONSOLE_SRPMS_DIR}


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

rm -rf ${PKI_CONSOLE_BUILD_DIR}/${PKI_CONSOLE}-${PKI_CONSOLE_VERSION}
rm -f  ${PKI_CONSOLE_RPMS_DIR}/${PKI_CONSOLE}-${PKI_CONSOLE_VERSION}*.rpm
rm -f  ${PKI_CONSOLE_SOURCES_DIR}/${PKI_CONSOLE_TARBALL}
rm -f  ${PKI_CONSOLE_SPEC_FILE}
rm -f  ${PKI_CONSOLE_SRPMS_DIR}/${PKI_CONSOLE}-${PKI_CONSOLE_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} > ${PKI_CONSOLE_SPEC_FILE}


##
## Create the 'pki-console' tarball
##


tar czf \
 ${PKI_CONSOLE_SOURCES_DIR}/${PKI_CONSOLE_TARBALL} \
 --transform "s,^./,pki-${PKI_CONSOLE_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[@]}" \
  ${PKI_CONSOLE_SPEC_FILE} \
  | tee package_${PKI_CONSOLE}.log 2>&1

