#!/bin/sh
export LANG=C
if [[ $# -ne 5 ]]; then
   echo Bad parameters  [fnmt,fnmtdnie,dnie,tif] [MAJOR] [MINOR] [FIX] [BUILDDIR]
   echo *BUILDDIR - Case sensitive volumen path to compile project
   exit 1
fi

CFGDIST=$1
CFGDISTUPPER=$1 | tr "[a-z]" "[A-Z]"
CFGMAJOR=$2
CFGMINOR=$3
CFGFIX=$4

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

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

SRCDIR=../../src
#BUILDDIR=/Volumes/HDD_CASESENSITIVE/BUILD		#need case sensitive volumen to compile project
BUILDDIR=$5_$CFGDIST

sudo rm -rf $BUILDDIR
mkdir $BUILDDIR
mkdir $BUILDDIR/m4
mkdir $BUILDDIR/OSX
mkdir $BUILDDIR/OSX/pkgFiles
cp -pR $SRCDIR/* $BUILDDIR
cp -pR ../common.autotools/* $BUILDDIR
cp -pR all/* $BUILDDIR/OSX
cp -pR card-$CFGDIST/* $BUILDDIR/OSX/pkgFiles

#Extras for MAC OS X systems!!
mv $BUILDDIR/OSX/reader.h $BUILDDIR/Includes		#because reader.h not exist in OSX
mv -f $BUILDDIR/OSX/m4/* $BUILDDIR/m4/.			#insert libassaun MACRO declaration to correct compilation
sudo rm -r $BUILDDIR/OSX/m4
mv $BUILDDIR/OSX/pkgFiles/components.plist $BUILDDIR/OSX/components.plist


pushd $BUILDDIR
## Change and Replace all #CFGDIST# to correct dist in code.
find . ! -path '*/DialogSign*' ! -path '*.svn*' -type f -print | xargs sed -i '' s/#CFGDIST#/$CFGDIST/g
find . ! -path '*/DialogSign*' ! -path '*.svn*' -type f -print | xargs sed -i '' s/#CFGMAJOR#/$CFGMAJOR/g
find . ! -path '*/DialogSign*' ! -path '*.svn*' -type f -print | xargs sed -i '' s/#CFGMINOR#/$CFGMINOR/g
find . ! -path '*/DialogSign*' ! -path '*.svn*' -type f -print | xargs sed -i '' s/#CFGFIX#/$CFGFIX/g

## Change and Replace by Dani
#find . ! -path '*/DialogSign*' ! -path '*.svn*' -type f -print | xargs sed -i '' "/#CFGDIST#/s//$CFGDIST/"
#find . ! -path '*/DialogSign*' ! -path '*.svn*' -type f -print | xargs sed -i '' "/#CFGMAJOR#/s//$CFGMAJOR/"
#find . ! -path '*/DialogSign*' ! -path '*.svn*' -type f -print | xargs sed -i '' "/#CFGMINOR#/s//$CFGMINOR/"
#find . ! -path '*/DialogSign*' ! -path '*.svn*' -type f -print | xargs sed -i '' "/#CFGFIX#/s//$CFGFIX/"

#exit

echo PASADO LOS FINDS

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_ -D_CFG_CACHE_PIN"
   export CPPFLAGS="-D_CFG_DNI_ -D_CFG_CACHE_PIN"
elif [ "$CFGDIST" == "fnmtdnie"  ]; then
   export CFLAGS="-D_CFG_CACHE_PIN"
   export CPPFLAGS="-D_CFG_CACHE_PIN"
elif [ "$CFGDIST" == "tif"  ]; then
   export CFLAGS="-D_CFG_TIF_REAL_"
   export CPPFLAGS="-D_CFG_TIF_REAL_"
else
   export CFLAGS=""
   export CPPFLAGS=""
fi

if [[ "$VER" == *10.10* ]]
then
    export CFLAGS="$CFLAGS -D_YOSEMITE_"
    export CPPFLAGS="$CPPFLAGS -D_YOSEMITE_"
fi

if [[ "$VER" == *10.9* ]]
then
    export CFLAGS="$CFLAGS -D_MAVERICKS_"
    export CPPFLAGS="$CPPFLAGS -D_MAVERICKS_"
fi

echo $CFLAGS

ROOT_NAME=Libpkcs11-$CFGDIST
autoreconf --verbose --install --force
./configure --prefix=/Library/$ROOT_NAME --disable-static
make
sudo make install
cd ./OSX
perl ./packit.pl

popd

if [ -e $BUILDDIR/OSX/*.pkg ] && [ -e $BUILDDIR/OSX/*.dmg ]
then
  mkdir $RELEASEDIR
  cp $BUILDDIR/OSX/*.pkg $RELEASEDIR
  cp $BUILDDIR/OSX/*.dmg $RELEASEDIR
  cp $BUILDDIR/test_DIST/libpkcs11-$CFGDIST-test-dist $RELEASEDIR
  cp $BUILDDIR/test_P11/libpkcs11-$CFGDIST-test-p11 $RELEASEDIR
fi

sudo rm -rf $BUILDDIR
