AC_INIT(src/rateup.c)

dnl Check for programs.
AC_PROG_CC
AC_PREFIX_DEFAULT( /usr/local/mrtg-2 )

AC_PROG_CPP
AC_PROG_MAKE_SET
AC_PROG_INSTALL
AC_PATH_PROG(PERL, perl, no, $PATH:/usr/bin:/usr/local/bin)
if test $PERL = no; then 
   echo
   echo "** No Perl found in the PATH. A recent copy of Perl"
   echo "   is required for mrtg to work. Check www.perl.com"
   echo 
   exit 1
fi

AC_PATH_PROGS(NROFF, groff nroff)

dnl Check for long long type (64 bit rateup)
AC_CHECK_HEADERS(inttypes.h)
AC_CHECK_TYPE(unsigned long long)
AC_CHECK_TYPE(long long)
AC_CHECK_FUNCS(strtoll)

AH_BOTTOM(
#ifndef HAVE_STRTOLL     
long long int strtoll(const char *str, char **ptr, int base)
{
        long long int ll;
        sscanf(str, LLD, &ll);
        return ll;
}
#endif
)

dnl Checks for libraries.
AC_CHECK_LIB(m, pow, [ MATHLIBS="-lm" ])

dnl AC_ARG_WITH(RRDs,[  --with-RRDs=DIR         location RRDtool perl module],
dnl 		[PERL5LIB="${withval}";export PERL5LIB])
dnl AC_MSG_CHECKING(availability of RRDtool / RRDs.pm)
dnl if $PERL -e 'use RRDs' >/dev/null 2>&1; then
dnl   AC_MSG_RESULT(found)
dnl  RRD_PERL=$PERL5LIB
dnl else
dnl  AC_MSG_RESULT(no)
dnl fi

AC_CACHE_CHECK([if long long works here], long_long_check,
[ AC_TRY_RUN([

#include <stdlib.h>
#define val "9223372036854775807"

int main(void){
   char x[100];
   long long i;
   if (sscanf(val,"%lld",&i) < 0) {
         printf ("sscanf error\n");
                return 1;
   }
   if (sprintf(x,"%lld",i) < 0) {
	printf ("sprintf error\n");
		return 1;
   }
   if (strcmp(x,val) != 0) {
         printf ("error '%s' != '%s'\n", x,val);
                return 1;
   }
   return 0;
}

],
            [long_long_check=yes],[long_long_check=nope],:)                    
])


AC_CACHE_CHECK([if quad works here], quad_check,
[ AC_TRY_RUN([

#include <stdlib.h>
#define val "9223372036854775807"

int main(void){
   char x[100];
   long long i;
   if (sscanf(val,"%qd",&i) < 0) {
         printf ("sscanf error\n");
                return 1;
   }
   if (sprintf(x,"%qd",i) < 0) {
	printf ("sprintf error\n");
		return 1;
   }
   if (strcmp(x,val) != 0) {
         printf ("error '%s' != '%s'\n", x,val);
                return 1;
   }
   return 0;
}

],
            [quad_check=yes],[quad_check=nope],:)                    
])

if test x"$long_long_check" = xnope -a x"$quad_check" = xnope ; then
        echo
	echo "** Ooops, your compiler seems to have issues with long long:"
	echo ""
	echo "   figure out how to get long long, printf with %lld or %qd and"
	echo "   strtoll working on your platform."
	echo "   probably the  config.log file can give you a hint."
        exit 1
fi

AH_TEMPLATE([LLD],[printf format for long long])

if test x"$long_long_check" = xyes; then
        AC_DEFINE([LLD],["%lld"])
else if test x"$quad_check" = xyes; then
        AC_DEFINE([LLD],["%qd"])
fi
fi
            

AC_ARG_WITH(gd,[  --with-gd=DIR           location of the gd lib/inc],
		[LDFLAGS="${LDFLAGS} -L${withval}"
		CPPFLAGS="${CPPFLAGS} -I${withval}"
		LD_RUN_PATH="${withval}${LD_RUN_PATH:+:}${LD_RUN_PATH}"])

AC_ARG_WITH(gd-lib,[  --with-gd-lib=DIR       location of the gd library],
		[LDFLAGS="${LDFLAGS} -L${withval}"
		LD_RUN_PATH="${withval}${LD_RUN_PATH:+:}${LD_RUN_PATH}"])

AC_ARG_WITH(gd-inc,[  --with-gd-inc=DIR       location of the gd include files],
		[CPPFLAGS="${CPPFLAGS} -I${withval}"])

AC_ARG_WITH(z,[  --with-z=DIR            location of zlib lib/inc],
		[LDFLAGS="${LDFLAGS} -L${withval}"
		CPPFLAGS="${CPPFLAGS} -I${withval}"
		LD_RUN_PATH="${withval}${LD_RUN_PATH:+:}${LD_RUN_PATH}"])

AC_ARG_WITH(z-lib,[  --with-z-lib=DIR        location of zlib library],
		[LDFLAGS="${LDFLAGS} -L${withval}"
		LD_RUN_PATH="${withval}${LD_RUN_PATH:+:}${LD_RUN_PATH}"])

AC_ARG_WITH(z-inc,[  --with-z-inc=DIR        location of the zlib include files],
		[CPPFLAGS="${CPPFLAGS} -I${withval}"])

AC_ARG_WITH(png,[  --with-png=DIR          location of png lib/inc],
		[LDFLAGS="${LDFLAGS} -L${withval}"
		CPPFLAGS="${CPPFLAGS} -I${withval}"
		LD_RUN_PATH="${withval}${LD_RUN_PATH:+:}${LD_RUN_PATH}"])

AC_ARG_WITH(png-lib,[  --with-png-lib=DIR      location of png library],
		[LDFLAGS="${LDFLAGS} -L${withval}"
		LD_RUN_PATH="${withval}${LD_RUN_PATH:+:}${LD_RUN_PATH}"])

AC_ARG_WITH(png-inc,[  --with-png-inc=DIR      location of the libpng include files],
		[CPPFLAGS="${CPPFLAGS} -I${withval}"])


LDFLAGS="${LDFLAGS} ${MATHLIBS}"

GDEXTRALIB=""
dnl which version of gd do we have here ?
AC_CHECK_LIB(gd,gdImageGif,[
  GDFORM_EXT=gif
  GLIBS="-lgd"
                           ],:,[-lgd ${MATHLIBS}])

AC_CHECK_LIB(gd,gdImagePng,[
   GDFORM_EXT=png
   GLIBS="-lgd -lpng -lz"
        ],:,[-lgd -lpng -lz ${MATHLIBS}])

AC_CHECK_LIB(gd,gdImagePng_jpg,[
   GDFORM_EXT=png
   GLIBS="-lgd -lpng -lz -ljpeg"
        ],:,[-lgd -lpng -lz -ljpeg ${MATHLIBS}])

AC_CHECK_LIB(gd,gdImagePng_jpg_ft,[
   GDFORM_EXT=png
   GLIBS="-lgd -lpng -lz -ljpeg -lfreetype"
        ],:,[-lgd -lpng -lz -ljpeg -lfreetype ${MATHLIBS}])

if test x$GDFORM_EXT = xgif; then
  CPPFLAGS="$CPPFLAGS -DGFORM_GD=gdImageGif"
fi

AC_CHECK_LIB(gd,gdImageGd,[
   GLIBS="-Wl,-Bstatic ${GLIBS} -Wl,-Bdynamic"
        ],:,[-Wl,-Bstatic ${GLIBS} -Wl,-Bdynamic])

if test x$GDFORM_EXT = xpng; then
  CPPFLAGS="$CPPFLAGS -DGFORM_GD=gdImagePng"
fi

LIBS="${GLIBS} ${LIBS}" 


dnl Make sure the header is here
AC_CHECK_HEADER( gd.h,:,[ GDFORM_EXT="" ])

if test x$GDFORM_EXT = x; then
echo
	echo "** Ooops, one of many bad things happened:"
	echo ""
	echo "   a)  You don't have the GD library installed."
	echo "       Get it from http://www.boutell.com, compile it and"
	echo "       use either --with-gd-lib=DIR and --with-gd-inc=DIR to specify"
	echo "       its location. You might also have to use --with-z-inc,"
        echo "	     --with-z-lib and --with-png-inc, --with-png-lib for gd"
        echo "	     versions 1.6 and higher.  Check config.log for more"
	echo "       information on the problem."
	echo ""
	echo "   b)  You have the GD library installed, but not the gd.h"
	echo "       header file.  Download the source (see above) and use"
	echo "       --with-gd-inc=DIR to specify where the file can be found."
	echo ""
	echo "   c)  You have the library and the header file installed, but"
	echo "       you also have a shared GD library in the same directory. "
        echo "       Remove the shared library files and/or links (e.g. "
	echo "       libgd.so.2.0.0, libgd.so and libgd.so.2).  This is especially"
        echo "	     likely if you're using a recent (post 1.8.4) version of GD"
	echo "       and didn't configure it with --disable-shared."
	echo ""
        echo "   Consider following the instructions in doc/unix-guide.txt"
	exit 1
fi

AC_MSG_CHECKING(the weather)
AC_MSG_RESULT((cached) it's fine)

dnl Does the compiler like -Wall and
if test "x$GCC" = "xyes"; then
  oCFLAGS=$CFLAGS
  CFLAGS="$CFLAGS -Wall -Wpointer-arith -Wcast-align -Wmissing-declarations -Wnested-externs -Winline -W"
  AC_CACHE_CHECK(if we can use GCC-specific compiler options, rd_cv_gcc_opt,
                [AC_TRY_COMPILE( , return 0 ,
                    rd_cv_gcc_opt=yes,
                    rd_cv_gcc_opt=no )
               ]
        )
  if test $rd_cv_gcc_opt = no; then
         CFLAGS=$oCFLAGS
  fi
fi

AC_SUBST(PERL)
AC_SUBST(RRD_PERL)
AC_SUBST(GDFORM_EXT)
AC_SUBST(LD_RUN_PATH)
AC_SUBST(LIBS)
AC_SUBST(CFLAGS)
AC_CONFIG_HEADERS(config.h)
AC_OUTPUT(Makefile)


echo $ECHO_N "ordering CD from http://people.ee.ethz.ch/~oetiker/wish $ECHO_C" 1>&6
sleep 1
echo $ECHO_N ".$ECHO_C" 1>&6
sleep 1
echo $ECHO_N ".$ECHO_C" 1>&6
sleep 1
echo $ECHO_N ".$ECHO_C" 1>&6
sleep 1
echo $ECHO_N ".$ECHO_C" 1>&6
sleep 1
AC_MSG_RESULT([ just kidding ;-)])
sleep 2
echo
echo "----------------------------------------------------------------"
echo "Config is DONE!"
echo
echo "Type 'make' to compile the software"
echo 
echo "       ... that wishlist mentioned above does really exist. So if"
echo "you feel like showing your appreciation for MRTG, this is the"
echo "place to go. I just love CDs and DVDs"
echo 
echo "                            -- Tobi Oetiker <oetiker@ee.ethz.ch>"
echo "----------------------------------------------------------------"
