#! /bin/bash

#    Copyright (C) 2007-2009  Ludovic Rousseau  <ludovic.rousseau@free.fr>
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
#    02110-1301 USA.

# to use
# ./MacOSX/configure
# make
# make install
# the driver is installed in /usr/libexec/SmartCardServices/drivers

# Colors
RED="\033[31m"
NORMAL="\033[0m"

# run this script as ./MacOSX/configure to configure for Mac OS X
if [ ! -d MacOSX ]
then
	echo -e $RED
	echo "ERROR!"
	echo "run ./MacOSX/configure from the source top directory"
	echo -e $NORMAL
	exit;
fi

./MacOSX/convert_reader_h.pl MacOSX/reader.h.in > MacOSX/reader.h

if [[ ("$*" =~ "--no-configure") ]]; then
       exit 0
fi

# find pcsc-lite header files in MacOSX/
# use ${varname:-word} to return word only if varname is not already defined
PCSC_CFLAGS=${PCSC_CFLAGS:--I$(pwd)/MacOSX}
PCSC_LIBS=${PCSC_LIBS:--framework PCSC}

libusb-config --version &> /dev/null
if [ $? -eq 0 ]
then
    echo "found libusb-config, using it"
    LIBUSB_CFLAGS=$(libusb-config --cflags)
    LIBUSB_LIBS=$(libusb-config --libs)
	# if possible replace "-L/usr/local/lib -lusb"
	# by "/usr/local/lib/libusb.a" to use a static link
	LIBUSB_LIBS=${LIBUSB_LIBS/-L\/usr\/local\/lib -lusb//usr/local/lib/libusb.a}
	echo $LIBUSB_LIBS
else
    echo "no libusb-config found"
    # use libusb from OpenSC (universal binary)
    LIBUSB_CFLAGS=${LIBUSB_CFLAGS:--I/Library/OpenSC/include}
    #LIBUSB_LIBS=${LIBUSB_LIBS:--L/Library/OpenSC/lib -lusb}
    LIBUSB_LIBS=${LIBUSB_LIBS:-/Library/OpenSC/lib/libusb.a -Wl,-framework -Wl,IOKit -Wl,-framework -Wl,CoreFoundation}
fi

# RESPONSECODE is already defined by PCSC/wintypes.h
# define needed here to compile examples/scardcontrol.c since config.h is
# not included
CFLAGS="$CFLAGS -DRESPONSECODE_DEFINED_IN_WINTYPES_H"

# Build a Universal Binary
CFLAGS="$CFLAGS -isysroot /Developer/SDKs/MacOSX10.5.sdk -arch i386 -arch ppc"
LDFLAGS="-arch i386 -arch ppc"
CONFIGURE_ARGS="--disable-dependency-tracking"

# do not build a static driver
# (building fails when linking statically with libusb)
CONFIGURE_ARGS="$CONFIGURE_ARGS --disable-static"

# do not use pcscd debug feature
CONFIGURE_ARGS="$CONFIGURE_ARGS --disable-pcsclite"

# simulate a composite device as multi slots
CONFIGURE_ARGS="$CONFIGURE_ARGS --enable-composite-as-multislot"

# use a specific bundle name to NOT overwrite the official CCID driver
#CONFIGURE_ARGS="$CONFIGURE_ARGS --enable-bundle=ifd-ccid-foobar.bundle"

set -x
./configure \
	CFLAGS="$CFLAGS" \
	PCSC_CFLAGS="$PCSC_CFLAGS" \
	PCSC_LIBS="$PCSC_LIBS" \
	LIBUSB_CFLAGS="$LIBUSB_CFLAGS" \
	LIBUSB_LIBS="$LIBUSB_LIBS" \
	LDFLAGS="$LDFLAGS" \
	--enable-usbdropdir=/usr/libexec/SmartCardServices/drivers \
	$CONFIGURE_ARGS \
	"$@"

# force a regeneration of Info.plist
rm -f src/Info.plist

