#DAs ist ein Test
AC_DEFUN([AC_SEARCH_CFITSIO],
[
     ac_with_cfitsio=no
     if test "${build_cpu}" = "x86_64"; then
       ac_search_lib_path="$CFITSIODIR/lib64 $CFITSIODIR/lib /usr/lib64 /usr/local/lib64 /opt/lib64 /opt/local/lib64 /usr/lib"
     else
       ac_search_lib_path="$CFITSIODIR/lib /usr/lib /usr/local/lib /opt/opencv/lib /opt/local/lib /usr/lib/i386-linux-gnu"
     fi
     ac_search_include_path="$CFITSIODIR/include /usr/include/cfitsio/ /usr/include/libcfitsio0 /usr/include/fitsio /usr/include  /usr/local/include /opt/include"
     
     
     AC_ARG_WITH(cfitsio,
          AC_HELP_STRING([--with-cfitsio=DIR], [root directory of cfitsio installation]),
          ac_with_cfitsio=$withval
          if test "x${ac_with_cfitsio}" != xyes && test "x${ac_with_cfitsio}" != xno; then
               ac_cfitsio_include="$withval/include"
               ac_cfitsio_libdir="$withval/lib"
          fi
     )

     AC_ARG_WITH([cfitsio-include-dir],
          AC_HELP_STRING([--with-cfitsio-include-dir=DIR], [cfitsio header files are in DIR]),
          ac_with_cfitsio=$withval
          if test "x${ac_with_cfitsio}" != xyes && test "x${ac_with_cfitsio}" != xno; then
               ac_cfitsio_include="$withval"
          fi
     )

     AC_ARG_WITH([cfitsio-lib-dir],
          AC_HELP_STRING([--with-cfitsio-lib-dir=DIR], [The cfitsio library is in DIR]),
          ac_with_cfitsio=$withval
          if test "x${ac_with_cfitsio}" != xyes && test "x${ac_with_cfitsio}" != xno; then
               ac_cfitsio_libdir="$withval"
          fi
     )

  if test "x${ac_cfitsio_libdir}" == "x"; then
    for ac_search_lib_test in $ac_search_lib_path; do
      if test -r "$ac_search_lib_test"; then
         ls $ac_search_lib_test/libcfitsio*  >/dev/null 2>&1
         if test $? -eq 0; then
            ac_cfitsio_libdir="$ac_search_lib_test/"
            break;
         fi
      fi
    done
  fi

   if test "x${ac_cfitsio_include}" == "x"; then
    for ac_search_include_test in $ac_search_include_path ; do
      if test -r "$ac_search_include_test/fitsio.h"; then
         ac_cfitsio_include="$ac_search_include_test"
         break;
      fi
    done
   fi

    AC_SEARCH_CFITSIO_LOCAL($ac_cfitsio_libdir, $ac_cfitsio_include)
])


# subfunction for easier reuse
AC_DEFUN([AC_SEARCH_CFITSIO_LOCAL],
[
     OLD_LDFLAGS=$LDFLAGS
     OLD_CXXFLAGS=$CXXFLAGS

     AC_MSG_NOTICE(Check $1 $2)

     if test "$1"; then
          LDFLAGS="$LDFLAGS -L$1 -lcfitsio -pthread"
     fi
     if test "$2"; then
          CXXFLAGS="$CXXFLAGS -I$2"
     fi

     AC_LANG_PUSH(C++)
     AC_MSG_CHECKING(for cfitsio in $1)
     AC_TRY_LINK([
          #include <fitsio.h>
     ],[
          ;
     ],[
     success=yes
     ],[
     success=no
     ])
     AC_MSG_RESULT($success)
     AC_LANG_POP()

     LDFLAGS=$OLD_LDFLAGS
     CXXFLAGS=$OLD_CXXFLAGS

     if test "x$success" != xyes; then
          AC_MSG_ERROR(cfitsio not (properly) installed. Get a (free) recent version from: http://heasarc.nasa.gov/fitsio/fitsio.html)
     else
          if test "$1"; then
               AC_SUBST(CFITSIO_LDFLAGS, "-L$1")
               AC_SUBST(CFITSIO_LIBS, "-lcfitsio")
          fi
          if test "$2"; then
               AC_SUBST(CFITSIO_CFLAGS, "-I$2")
          fi
     fi
])
