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

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


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

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

PKI_CORE_BUILD_DIR="${PKI_PACKAGES}/BUILD"
PKI_CORE_RPMS_DIR="${PKI_PACKAGES}/RPMS"
PKI_CORE_SOURCES_DIR="${PKI_PACKAGES}/SOURCES"
PKI_CORE_SPECS_DIR="${PKI_PACKAGES}/SPECS"
PKI_CORE_SRPMS_DIR="${PKI_PACKAGES}/SRPMS"

PKI_CORE_TARBALL="pki-${PKI_CORE_VERSION}.tar.gz"
PKI_CORE_SPEC_FILE="${PKI_CORE_SPECS_DIR}/${PKI_CORE}.spec"

PKI_CORE_STAGING_DIR="${PKI_PACKAGES}/staging"
PKI_CORE_DIR="${PKI_CORE_STAGING_DIR}/${PKI_CORE}-${PKI_CORE_VERSION}"
PKI_CORE_BASE_DIR="${PKI_CORE_DIR}/base"


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

mkdir -p ${PKI_PACKAGES}


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

mkdir -p ${PKI_CORE_BUILD_DIR}
mkdir -p ${PKI_CORE_RPMS_DIR}
mkdir -p ${PKI_CORE_SOURCES_DIR}
mkdir -p ${PKI_CORE_SPECS_DIR}
mkdir -p ${PKI_CORE_SRPMS_DIR}


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

rm -rf ${PKI_CORE_BUILD_DIR}/${PKI_CORE}-${PKI_CORE_VERSION}
rm -f  ${PKI_CORE_RPMS_DIR}/${PKI_CORE}-${PKI_CORE_VERSION}*.rpm
rm -f  ${PKI_CORE_SOURCES_DIR}/${PKI_CORE_TARBALL}
rm -f  ${PKI_CORE_SPEC_FILE}
rm -f  ${PKI_CORE_SRPMS_DIR}/${PKI_CORE}-${PKI_CORE_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_CORE_SPEC_FILE}


##
## Create the 'pki-core' tarball
##

tar czf \
 ${PKI_CORE_SOURCES_DIR}/${PKI_CORE_TARBALL} \
 --transform "s,^./,pki-${PKI_CORE_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_CORE_SPEC_FILE} \
  | tee package_${PKI_CORE}.log 2>&1

if [ "$CREATEREPO" == "Y" ] ; then
   echo "Creating a repository of the built rpms"
   cp -r $PKI_PWD/$PKI_DIR/scripts/scp_the_repo.exp .
   Create_repo_after_build $REPO_CFG_FILE
fi
