#!/bin/sh

#    check: verify all the needed parts are present
#    Copyright (C) 2002-2004  Ludovic Rousseau
#
#    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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

# this code is greatly inspired by configure scripts generated by autoconf
# $Id: check,v 1.1.1.5 2004/07/24 21:57:23 rousseau Exp $

exec 5>./config.log

CHECK_CFLAGS=`pkg-config libpcsclite --cflags`

ac_ext=c
ac_cpp='cpp $CFLAGS $CHECK_CFLAGS'
ac_cc=cc
ac_compile='${CC-cc} -c $CFLAGS $CHECK_CFLAGS $CPPFLAGS conftest.$ac_ext 1>&5'
ac_link='${CC-cc} -o conftest${ac_exeext} $CFLAGS $CHECK_CFLAGS $CPPFLAGS $LDFLAGS conftest.$ac_ext $LIBS 1>&5'

# test the presence of a .h file
# the filename to check is fetched from $ac_hdr
check_h()
{
ac_safe=`echo "$ac_hdr" | sed 'y%./+-%__p_%'`
echo -n "checking for $ac_hdr""... "
echo "checking for $ac_hdr""... " >&5
cat > conftest.${ac_ext} << EOF
#include <$ac_hdr>
EOF
ac_try="$ac_cpp conftest.$ac_ext >/dev/null 2>conftest.out"
{ (eval $ac_try) 2>&5; }
ac_err=`grep -v '^ *+' conftest.out | grep -v "^conftest.${ac_ext}\$"`
if test -z "$ac_err"; then
  rm -rf conftest*
  eval "ac_cv_header_$ac_safe=yes"
else
  echo "$ac_err" >&5
  echo "configure: failed program was:" >&5
  cat conftest.$ac_ext >&5
  echo $ac_try >& 5
  cat conftest.out >& 5
  rm -rf conftest*
  eval "ac_cv_header_$ac_safe=no"
fi
rm -f conftest*
if eval "test \"`echo '$ac_cv_header_'$ac_safe`\" = yes"; then
  echo "yes"
else
  echo "no"
fi
}

# check the presence of pcsclite.h
ac_hdr="pcsclite.h"
check_h
if [ "$ac_cv_header_pcsclite_h" = "no" ]
then
cat << EOF

FILE MISSING!
You must install the pcsc-lite software before compiling this software
You can get it from http://pcsclite.alioth.debian.org/ or from a package of
your operating system distribution

If pcsc-lite is already installed but not in /usr/ or /usr/local/ you must
edit GemPC4?0/Makefile and change the definition of INCS.
EOF
		exit 1
fi

# check the version of pcsc-lite
ac_version=\"1.2.9-beta5\"
ac_hdr=pcsclite.h
ac_safe=`echo "$ac_hdr" | sed 'y%./+-%__p_%'`
echo -n "checking for pcsc-lite version >= $ac_version... "
echo "checking for pcsc-lite version >= $ac_version... " >&5
cat > conftest.${ac_ext} << EOF
#include <string.h>
#include <$ac_hdr>
int main() {
if (strcmp(PCSCLITE_VERSION_NUMBER, $ac_version) < 0)
return 1;
else
return 0;
}
EOF
if { (eval $ac_link) 2>&5; } && ./conftest; then
  eval "ac_cv_pcsc_version=yes"
else
  echo "configure: failed program was:" >&5
  cat conftest.$ac_ext >&5
  eval "ac_cv_pcsc_version=no"
fi
rm -f conftest*

if eval "test \"`echo '$ac_cv_'pcsc_version`\" = yes"; then
  echo "yes"
  :
else
  echo "no"
fi

if [ $ac_cv_pcsc_version = "no" ]
then
	cat << EOF

WRONG VERSION OF PCSC-LITE!
You can get it from http://pcsclite.alioth.debian.org/ or from a package of
your operating system distribution
EOF
	exit 1
fi

# if we test for a GemPC410 we stop here
if [ "$1" = 410 ]
then
	exit 0
fi

# check the presence of usb.h
ac_hdr=usb.h
check_h
if [ $ac_cv_header_usb_h = "no" ]
then
	cat << EOF

FILE MISSING!
You must install the libusb library before compiling this software
You can get it from http://libusb.sourceforge.net/ or from a package of
your operating system distribution
EOF
	exit 1
fi

# check the presence of usb_get_busses()
LDFLAGS="$LDFLAGS -lusb"
echo -n "checking for usb_get_busses... "
echo "checking for usb_get_busses... " >&5
cat > conftest.$ac_ext <<EOF
char usb_get_busses();

int main() {

usb_get_busses();

; return 0; }
EOF
if { (eval $ac_link) 2>&5; } && test -s conftest; then
  eval "ac_cv_func_usb_get_busses=yes"
else
  echo "configure: failed program was:" >&5
  cat conftest.$ac_ext >&5
  eval "ac_cv_func_usb_get_busses=no"
fi
rm -f conftest*

if eval "test \"`echo '$ac_cv_func_'usb_get_busses`\" = yes"; then
  echo "yes"
  :
else
  echo "no"
fi

if [ $ac_cv_func_usb_get_busses = "no" ]
then
	cat << EOF

WRONG VERSION OF LIBUSB!
Your version of libusb is not correct (too old?). You must use version
0.1.6a or superior.
You can get it from http://libusb.sourceforge.net/ or from a package of
your operating system distribution
EOF
	exit 1
fi

exit 0

