#!/bin/sh


ARCH=""
PARCH=""

if [ "$1" = "" ]; then
    echo "Regenerate rpm packages and rpm repository."
    echo "Automatically detecting architecture."
    PARCH=$(uname -m)
        if [ "$PARCH" = "i386" ] || [ "$PARCH" = "i686" ]; then
        ARCH="i386"
        fi
        if [ "$PARCH" = "x86_64" ]; then
        ARCH="x86_64"
        fi
    echo "Architecture detected as $ARCH."
elif [ "$1" = "i386" ]; then
    echo "Regenerating i386 rpm packages and rpm repository."
    echo "Requires the reprepro tool."
elif [ "$1" = "x86_64" ]; then
    echo "Regenerating x86_64 rpm packages and rpm repository."
    echo "Requires the reprepro tool."
else
    echo "Usage: build-deb-repo i386 | x86_64"
    exit 5
fi




set -e

rm -rf rpm_repo/repodata
rm -f rpm_repo/*$ARCH.rpm
mkdir -p rpm_repo
mkdir -p ~/rpmbuild/{BUILD,RPMS,SOURCES,SPECS,SRPMS}
echo '%_topdir %(echo $HOME)/rpmbuild' > ~/.rpmmacros

for j in alpha beta charlie; do
 for k in 1 2; do
   cp ./rpm_sources/test-package-$j-1.0-$k-"$ARCH"-rpm-installed.txt ~/rpmbuild/SOURCES
 done
done


for i in alpha beta charlie; do
    rpmbuild -bb   test-package-$i-1.0-1.spec
    cp ~/rpmbuild/RPMS/"$ARCH"/test-package-$i-1.0-1."$ARCH".rpm rpm_repo
    rpmbuild -bb   test-package-$i-1.0-2.spec
    cp ~/rpmbuild/RPMS/"$ARCH"/test-package-$i-1.0-2."$ARCH".rpm rpm_repo
done

# Not all createrepo versions support the below arguments, but if they do
# then they must be included.
createrepo --no-database --simple-md-filenames rpm_repo || createrepo rpm_repo
