if [[ $# -ne 4 ]]; then
   echo Bad parameters  [fnmt,fnmtdnie,dnie,tif] [MAJOR] [MINOR] [FIX]
   exit 1
fi

CFGDIST=$1
CFGDISTUPPER=${1^^}
CFGMAJOR=$2
CFGMINOR=$3
CFGFIX=$4

OS=$(lsb_release -si)
ARCH=$(uname -m | sed 's/x86_//;s/i[3-6]86/32/')
VER=$(lsb_release -sr)

RELEASEDIR=./../RELEASE-OpenSUSE-$VER-$ARCH-bits

BUILD_NAME=libpkcs11-$CFGDIST-$CFGMAJOR.$CFGMINOR.$CFGFIX
SRCDIR=../../src
BUILDDIR=/tmp/$BUILD_NAME
RPMBUILD_PATH=/usr/src/packages

rm -rf $BUILDDIR
mkdir $BUILDDIR
cp -R $SRCDIR/* $BUILDDIR
cp -R ../common.autotools/* $BUILDDIR
cp -R all $BUILDDIR/.
cp -R card-$CFGDIST $BUILDDIR/.
mkdir $RPMBUILD_PATH
mkdir $RPMBUILD_PATH/SOURCES
mkdir $RELEASEDIR
pushd $BUILDDIR
find ./ -type f | xargs sed -i s/#CFGDIST#/$CFGDIST/g
find ./ -type f | xargs sed -i s/#CFGMAJOR#/$CFGMAJOR/g
find ./ -type f | xargs sed -i s/#CFGMINOR#/$CFGMINOR/g
find ./ -type f | xargs sed -i s/#CFGFIX#/$CFGFIX/g
cd ..
tar czf $RPMBUILD_PATH/SOURCES/libpkcs11-$CFGDIST.tar.gz $BUILD_NAME
popd

export CFGDIST=$CFGDIST
export CFGMAJOR=$CFGMAJOR
export CFGMINOR=$CFGMINOR
export CFGFIX=$CFGFIX
rpmbuild -bb all/libpkcs11.spec

if [[ $ARCH = 32 ]]
then
cp -R $RPMBUILD_PATH/RPMS/i586/ $RELEASEDIR/.
else
cp -R $RPMBUILD_PATH/RPMS/x86_64/ $RELEASEDIR/.
fi

