#!/bin/sh

# This script may ONLY be run on Linux!
PKI_OS=`uname`
if [ "${PKI_OS}" != "Linux" ]; then
    printf "The '$0' script is ONLY executable\n"
    printf "on a 'Linux' machine!\n"
    exit 255
fi

# 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 a list of 'packages' directories
PKI_PACKAGE_DIRS_LIST="packages packages.theme packages.dogtag_theme packages.core packages.console packages.meta"

# This script is only executable from the directory
# containing at the "./pki" directory!
if [ ! -d ./pki ]; then
    printf "The '$0' script is ONLY executable\n"
    printf "from the directory containing './pki'!\n"
    exit 255
fi

printf "Removing any 'RPMS/' and 'SRPMS/' directories . . . "
if [ -d RPMS ]; then
    rm -rf RPMS
fi
if [ -d SRPMS ]; then
    rm -rf SRPMS
fi
printf "done.\n\n"

printf "Creating new 'RPMS/' and 'SRPMS/' directories . . . "
mkdir RPMS SRPMS
printf "done.\n\n"

printf "Copying all 'RPMS' and 'SRPMS' into the 'RPMS/' directory . . . "
for package_dir in ${PKI_PACKAGE_DIRS_LIST} ;
do
	if [ -d ${package_dir} ] ; then
		cd ${package_dir}
		find . -name "*.rpm" | xargs -n1 -I{} cp -p {} ../RPMS
		cd ..
	fi
done
printf "done.\n\n"

printf "Moving all 'SRPMS' into the 'SRPMS/' directory . . . "
mv ./RPMS/*.src.rpm ./SRPMS
printf "done.\n\n"

if [ -x /usr/bin/createrepo ]; then
	printf "Attempting to create a yum repo in the 'RPMS/' directory . . .\n"
	/usr/bin/createrepo ./RPMS
	printf "done.\n\n"

	printf "Attempting to create a yum repo in the 'SRPMS/' directory . . .\n"
	/usr/bin/createrepo ./SRPMS
	printf "done.\n\n"
fi

printf "Counting all 'RPMS' . . . "
RPM_COUNT=`ls -1 ./RPMS/*.rpm | wc -l`
printf "done.\n\n"

printf "Counting all 'SRPMS' . . . "
SRPM_COUNT=`ls -1 ./SRPMS/*.rpm | wc -l`
printf "done.\n\n"

printf "TOTAL:  RPMS  = ${RPM_COUNT}\n"
printf "        SRPMS = ${SRPM_COUNT}\n\n"

