export PATH=/opt/csw/bin:/opt/csw/gcc4/bin:/opt/css/bin:/usr/sfw/bin:$PATH:/usr/ccs/bin
if [[ $# -ne 4 ]]; then
   echo Bad parameters  [fnmt,fnmtdnie,dnie,tif] [MAJOR] [MINOR] [FIX]
   exit 1
fi

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

CFGDIST=$1
CFGMAJOR=$2
CFGMINOR=$3
CFGFIX=$4
CFGARCH=`uname -p`
TEMPORALPATH=FNMTpkcs11$CFGDIST
DISTNAME=libpkcs11-$CFGDIST-$CFGMAJOR.$CFGMINOR.$CFGFIX-`uname`-`uname -r`-`uname -p`
PKGDIR=./card-$CFGDIST
SRCDIR=../../src
BUILDDIR=/tmp/build
rm -rf $BUILDDIR
mkdir $BUILDDIR
cp -R $SRCDIR/* $BUILDDIR
cp -R ../common.autotools/* $BUILDDIR
cp -R all/* $BUILDDIR
cp -R card-$CFGDIST/* $BUILDDIR
pushd $BUILDDIR
find ./ -type f | xargs gsed -i s/#CFGDIST#/$CFGDIST/g
find ./ -type f | xargs gsed -i s/#CFGMAJOR#/$CFGMAJOR/g
find ./ -type f | xargs gsed -i s/#CFGMINOR#/$CFGMINOR/g
find ./ -type f | xargs gsed -i s/#CFGFIX#/$CFGFIX/g
find ./ -type f | xargs gsed -i s/#CFGARCH#/$CFGARCH/g
autoreconf -ifv

if [ "$CFGDIST" == "fnmt"  ]; then
   export CFLAGS="-D_CFG_FNMT_CERES_"
   export CPPFLAGS="-D_CFG_FNMT_CERES_"
elif [ "$CFGDIST" == "dnie"  ]; then
   export CFLAGS="-D_CFG_DNI_"
   export CPPFLAGS="-D_CFG_DNI_"
elif [ "$CFGDIST" == "tif"  ]; then
   export CFLAGS="-D_CFG_TIF_REAL_"
   export CPPFLAGS="-D_CFG_TIF_REAL_"
else
   export CFLAGS=""
   export CPPFLAGS=""
fi

export PCSC_CFLAGS=-I/usr/include/smartcard
export PCSC_LIBS="-L/usr/lib -lpcsclite"
export CFLAGS="$CFLAGS -m64"
export CPPFLAGS="$CPPFLAGS -DCRYPTOPP_DISABLE_ASM -m64"
export LDFLAGS="$LDFLAGS -m64"

./configure --disable-static
make

mkdir $TEMPORALPATH
pkgmk -o -r . -d $TEMPORALPATH -f Prototype
pkgtrans -s $TEMPORALPATH ../$DISTNAME.pkg $TEMPORALPATH

popd

RELEASEDIR=../RELEASE-$OS-$VER-$ARCH-bits
if [ -e $BUILDDIR/*.pkg ]
then
  mkdir $RELEASEDIR
  cp $BUILDDIR/*.pkg $RELEASEDIR
  cp $BUILDDIR/test_DIST/libpkcs11-$CFGDIST-test-dist $RELEASEDIR
  cp $BUILDDIR/test_P11/libpkcs11-$CFGDIST-test-p11 $RELEASEDIR
fi

#rm -rf $BUILDDIR


