dnl ------------------------------------------------------------------------
dnl Try to find the pc/sc headers and libraries.
dnl $(PCSC_LDFLAGS) will be -Lpcscliblocation (if needed)
dnl and $(PCSC_INCLUDES) will be -Ipcschdrlocation (if needed)
dnl ------------------------------------------------------------------------
dnl
AC_DEFUN(AC_PATH_PCSC,
[
LIBPCSC="-lpcsclite"
AC_MSG_CHECKING([for PC/SC])

ac_pcsc_includes=NO ac_pcsc_libraries=NO
pcsc_libraries=""
pcsc_includes=""
AC_ARG_WITH(pcsc-dir,
    [  --with-pcsc-dir=DIR     where the root of PC/SC is installed],
    [  ac_pcsc_includes="$withval"/include
       ac_pcsc_libraries="$withval"/lib
    ])

want_pcsc=yes
AC_ARG_WITH(pcsc,
    [  --without-pcsc          disable PC/SC checks],
    [want_pcsc=$withval])

if test $want_pcsc = yes; then

AC_CACHE_VAL(ac_cv_have_pcsc,
[#try to guess PC/SC locations

  pcsc_incdirs="/usr/include /usr/local/include /usr/local/pcsc/include /opt/pcsc/include $prefix/include $kde_extra_includes"
  pcsc_incdirs="$ac_pcsc_includes $pcsc_incdirs"
  AC_FIND_FILE(winscard.h, $pcsc_incdirs, pcsc_incdir)
  ac_pcsc_includes="$pcsc_incdir"

  pcsc_libdirs="/usr/lib /usr/local/lib /usr/local/pcsc/lib $prefix/lib $exec_prefix/lib $kde_extra_libs"
  if test ! "$ac_pcsc_libraries" = "NO"; then
    pcsc_libdirs="$ac_pcsc_libraries $pcsc_libdirs"
  fi

  test=NONE
  pcsc_libdir=NONE
  for dir in $pcsc_libdirs; do
    try="ls -1 $dir/libpcsclite*"
    if test=`eval $try 2> /dev/null`; then pcsc_libdir=$dir; break; else echo "tried $dir" >&AC_FD_CC ; fi
  done

  ac_pcsc_libraries="$pcsc_libdir"

  if test "$ac_pcsc_includes" = NO || test "$ac_pcsc_libraries" = NO; then
    have_pcsc=no
  else
    PCSC_INCLUDES="-I$ac_pcsc_includes ";
    PCSC_LDFLAGS="-L$ac_pcsc_libraries -lpcsclite";
    have_pcsc=yes;
  fi

  ])

  eval "$ac_cv_have_pcsc"
else
  have_pcsc=no
fi

AC_SUBST(PCSC_INCLUDES)
AC_SUBST(PCSC_LDFLAGS)
AC_SUBST(LIBPCSC)
])


AC_PATH_PCSC

if test "$ac_pcsc_includes" = NO || test "$ac_pcsc_libraries" = NO; then
  SMARTCARDDIR=
  AC_SUBST(SMARTCARDDIR)
  AC_MSG_RESULT([not found.  Smartcard support disabled.])
else
  SMARTCARDDIR="kardlib kardsvc kardimpl"
  AC_SUBST(SMARTCARDDIR)
  AC_MSG_RESULT([found.  Smartcard support enabled.])
fi

