#!/bin/sh
# =======================================================================
#
# chronyd/chronyc - Programs for keeping computer clocks accurate.
#
# Copyright (C) Richard P. Curnow  1997-2003
#
# =======================================================================

# This configure script determines the operating system type and version

if [ "x${CC}" = "x" ]; then
  MYCC="gcc"
else
  MYCC="${CC}"
fi

if [ "x${CFLAGS}" = "x" ]; then
  MYCFLAGS="-O2 -g"
else
  MYCFLAGS="${CFLAGS}"
fi

MYCPPFLAGS="${CPPFLAGS}"

if [ "x${MYCC}" = "xgcc" ]; then
  CCWARNFLAGS="-Wmissing-prototypes -Wall"
else
  CCWARNFLAGS=""
fi

# ======================================================================
# FUNCTIONS

#{{{ test_code
test_code () {
  name=$1
  headers=$2
  cflags=$3
  ldflags=$4
  code=$5

  printf "Checking for $name : "

  (
    for h in $headers; do
      echo "#include <$h>"
    done
    echo "int main(int argc, char **argv) {"
    echo "$code"
    echo "return 0; }"
  ) > docheck.c

  ${MYCC} ${MYCFLAGS} ${MYCPPFLAGS} $cflags -o docheck docheck.c $ldflags >/dev/null 2>&1
  if [ $? -eq 0 ]
  then
    printf "Yes\n"
    result=0
  else
    printf "No\n"
    result=1
  fi
  rm -f docheck.c docheck
  return $result
}
#}}}
#{{{ usage
usage () {
  cat <<EOF;
\`configure' configures tdl to adapt to many kinds of systems.

Usage: ./configure [OPTION]...

Defaults for the options are specified in brackets.

Configuration:
  -h, --help              display this help and exit

Installation directories:
  --prefix=PREFIX         install architecture-independent files in PREFIX
                          [/usr/local]
  --exec-prefix=EPREFIX   install architecture-dependent files in EPREFIX
                          [PREFIX]

By default, \`make install' will install all the files in
\`/usr/local/bin', \`/usr/local/lib' etc.  You can specify
an installation prefix other than \`/usr/local' using \`--prefix',
for instance \`--prefix=$HOME'.

For better control, use the options below.
  --disable-readline     Disable line editing support
  --without-readline     Don't use GNU readline even if it is available
  --without-editline     Don't use editline even if it is available
  --readline-dir=DIR     Specify parent of readline include and lib directories
  --readline-inc-dir=DIR Specify where readline include directory is
  --readline-lib-dir=DIR Specify where readline lib directory is
  --with-ncurses-library=DIR Specify where ncurses lib directory is
  --disable-ipv6         Disable IPv6 support
  --disable-pps          Disable PPS API support
  --disable-rtc          Don't include RTC even on Linux
  --disable-linuxcaps    Disable Linux capabilities support

Fine tuning of the installation directories:
  --sysconfdir=DIR       chrony.conf location [/etc]
  --bindir=DIR           user executables [EPREFIX/bin]
  --sbindir=DIR          system admin executables [EPREFIX/sbin]
  --datarootdir=DIR      data root [PREFIX/share]
  --infodir=DIR          info documentation [DATAROOTDIR/info]
  --mandir=DIR           man documentation [DATAROOTDIR/man]
  --docdir=DIR           documentation root [DATAROOTDIR/doc/chrony]

Overriding system detection when cross-compiling:
  --host-system=OS       Specify system name (uname -s)
  --host-release=REL     Specify system release (uname -r)
  --host-machine=CPU     Specify machine (uname -m)

Some influential environment variables:
  CC          C compiler command
  CFLAGS      C compiler flags
  CPPFLAGS    C preprocessor flags, e.g. -I<include dir> if you have
              headers in a nonstandard directory <include dir>
  LDFLAGS     linker flags, e.g. -L<lib dir> if you have libraries in a
              nonstandard directory <lib dir>

Use these variables to override the choices made by \`configure' or to help
it to find libraries and programs with nonstandard names/locations.

EOF

}
#}}}

# ======================================================================



OPERATINGSYSTEM=`uname -s`
VERSION=`uname -r`
MACHINE=`uname -m`

EXTRA_LIBS=""
EXTRA_CLI_LIBS=""
EXTRA_OBJECTS=""
EXTRA_DEFS=""
SYSDEFS=""

# Support for readline (on by default)
feat_readline=1
try_readline=1
try_editline=1
feat_rtc=1
feat_linuxcaps=1
try_linuxcaps=0
readline_lib=""
readline_inc=""
ncurses_lib=""
feat_ipv6=1
feat_pps=1
try_setsched=0
try_lockmem=0

for option
do
    case "$option" in
    --trace )
      EXTRA_DEFS="-DTRACEON"
    ;;
    --disable-readline )
      feat_readline=0
    ;;
    --without-readline )
      try_readline=0
    ;;
    --without-editline )
      try_editline=0
    ;;
    --with-readline-library=* )
      readline_lib=-L`echo $option | sed -e 's/^.*=//;'`
    ;;
    --with-readline-includes=* )
      readline_inc=-I`echo $option | sed -e 's/^.*=//;'`
    ;;
    --with-ncurses-library=* )
      ncurses_lib=-L`echo $option | sed -e 's/^.*=//;'`
    ;;
    --prefix=* | --install_prefix=* )
      SETPREFIX=`echo $option | sed -e 's/[^=]*=//;'`
    ;;
    --exec-prefix=* )
      SETEPREFIX=`echo $option | sed -e 's/[^=]*=//;'`
    ;;
    --sysconfdir=* )
      SETSYSCONFDIR=`echo $option | sed -e 's/^.*=//;'`
    ;;
    --bindir=* )
      SETBINDIR=`echo $option | sed -e 's/^.*=//;'`
    ;;
    --sbindir=* )
      SETSBINDIR=`echo $option | sed -e 's/^.*=//;'`
    ;;
    --datarootdir=* )
      SETDATAROOTDIR=`echo $option | sed -e 's/^.*=//;'`
    ;;
    --infodir=* )
      SETINFODIR=`echo $option | sed -e 's/^.*=//;'`
    ;;
    --mandir=* )
      SETMANDIR=`echo $option | sed -e 's/^.*=//;'`
    ;;
    --docdir=* )
      SETDOCDIR=`echo $option | sed -e 's/^.*=//;'`
    ;;
    --disable-rtc)
      feat_rtc=0
    ;;
    --disable-ipv6)
      feat_ipv6=0
    ;;
    --disable-pps)
      feat_pps=0
    ;;
    --disable-linuxcaps)
      feat_linuxcaps=0
    ;;
    --host-system=* )
      OPERATINGSYSTEM=`echo $option | sed -e 's/^.*=//;'`
    ;;
    --host-release=* )
      VERSION=`echo $option | sed -e 's/^.*=//;'`
    ;;
    --host-machine=* )
      MACHINE=`echo $option | sed -e 's/^.*=//;'`
    ;;
    --help | -h )
      usage
      exit 0
    ;;
    * )
    echo "Unrecognized option : " $option
    esac
done

SYSTEM=${OPERATINGSYSTEM}-${MACHINE}

case $SYSTEM in
    SunOS-sun4* )
    case $VERSION in
        4.* )
            EXTRA_OBJECTS="sys_sunos.o strerror.o"
            EXTRA_LIBS="-lkvm"
            SYSDEFS="-DSUNOS"
            echo "Configuring for SunOS (" $SYSTEM "version" $VERSION ")"
        ;;
        5.* )
            EXTRA_OBJECTS="sys_solaris.o"
            EXTRA_LIBS="-lsocket -lnsl -lkvm -lelf"
            EXTRA_CLI_LIBS="-lsocket -lnsl"
            SYSDEFS="-DSOLARIS"
            echo "Configuring for Solaris (" $SYSTEM "SunOS version" $VERSION ")"
            if [ $VERSION = "5.3" ]; then
                SYSDEFS="$SYSDEFS -DHAS_NO_BZERO"
                echo "Using memset() instead of bzero()"
            fi
        ;;
    esac
    ;;
    Linux* )
        EXTRA_OBJECTS="sys_linux.o wrap_adjtimex.o"
        if [ $feat_rtc -eq 1 ] ; then
            EXTRA_OBJECTS="$EXTRA_OBJECTS rtc_linux.o"
            EXTRA_DEFS="$EXTRA_DEFS -DFEAT_RTC=1"
        fi
        try_linuxcaps=1
        try_setsched=1
        try_lockmem=1
        SYSDEFS="-DLINUX"
        echo "Configuring for " $SYSTEM
        if [ "${MACHINE}" = "alpha" ]; then
           echo "Enabling -mieee"
           # FIXME: Should really test for GCC
           SYSDEFS="$SYSDEFS -mieee -DALPHA"
        fi
    ;;

    BSD/386-i[3456]86|FreeBSD-i386|FreeBSD-amd64 )
        # Antti Jrvinen <costello@iki.fi> reported that this system can
        # be supported with the SunOS 4.x driver files.
        EXTRA_OBJECTS="sys_sunos.o strerror.o"
        EXTRA_LIBS="-lkvm"
        SYSDEFS="-DSUNOS"
        echo "Configuring for $SYSTEM (using SunOS driver)"
    ;;
    NetBSD-* )
        EXTRA_OBJECTS="sys_netbsd.o"
        EXTRA_LIBS="-lkvm"
        SYSDEFS=""
        echo "Configuring for $SYSTEM"
    ;;
    SunOS-i86pc* )                                          
        # Doug Woodward <dougw@whistler.com> reported that this configuration
        # works for Solaris 2.8 / SunOS 5.8 on x86 platforms
        EXTRA_OBJECTS="sys_solaris.o"                
        EXTRA_LIBS="-lsocket -lnsl -lkvm -lelf"      
        EXTRA_CLI_LIBS="-lsocket -lnsl"                              
        SYSDEFS="-DSOLARIS"                               
        echo "Configuring for Solaris (" $SYSTEM "SunOS version" $VERSION ")" 
    ;;                                                                        
    CYGWIN32_NT-i[3456]86 )
        EXTRA_OBJECTS="sys_winnt.o"
        EXTRA_LIBS=""
        SYSDEFS="-DWINNT"
        echo "Configuring for Windows NT (Cygwin32)"
    ;;
    * )
        echo "Sorry, I don't know how to build this software on your system."
        exit 1
    ;;
esac

MATHCODE='return (int) pow(2.0, log(sqrt((double)argc)));'
if test_code 'math' 'math.h' '' '' "$MATHCODE"; then
  LIBS=""
else
  if test_code 'math in -lm' 'math.h' '' '-lm' "$MATHCODE"; then
    LIBS="-lm"
  else
    printf "Can't compile/link a program which uses sqrt(), log(), pow(), bailing out\n"
    exit 1
  fi
fi
  
if test_code '<stdint.h>' 'stdint.h' '' '' ''; then
  SYSDEFS="${SYSDEFS} -DHAS_STDINT_H"
fi

if test_code '<inttypes.h>' 'inttypes.h' '' '' ''; then
  SYSDEFS="${SYSDEFS} -DHAS_INTTYPES_H"
fi

if [ $feat_ipv6 = "1" ] && \
  test_code 'IPv6 support' 'arpa/inet.h sys/socket.h netinet/in.h' '' '' '
    struct sockaddr_in6 n;
    char p[100];
    n.sin6_addr = in6addr_any;
    return !inet_ntop(AF_INET6, &n.sin6_addr.s6_addr, p, sizeof(p));'
then
  SYSDEFS="${SYSDEFS} -DHAVE_IPV6"
fi

if [ $feat_pps = "1" ] && \
  test_code 'PPS API' 'timepps.h' '' '' '
    pps_handle_t h;
    pps_info_t i;
    struct timespec ts;
    return time_pps_fetch(&h, PPS_TSFMT_TSPEC, &i, &ts);'
then
  SYSDEFS="${SYSDEFS} -DHAVE_PPSAPI"
fi

if [ $feat_linuxcaps = "1" ] && [ $try_linuxcaps = "1" ] && \
  test_code \
    linuxcaps \
    'sys/types.h pwd.h sys/prctl.h sys/capability.h grp.h' \
    '' '-lcap' \
    'prctl(PR_SET_KEEPCAPS, 1);cap_set_proc(cap_from_text("cap_sys_time=ep"));'
then
    EXTRA_DEFS="${EXTRA_DEFS} -DFEAT_LINUXCAPS=1"
    EXTRA_LIBS="-lcap"
fi

if [ $try_setsched = "1" ] && \
  test_code \
    'sched_setscheduler()' \
    'sched.h' '' '' '
     struct sched_param sched;
     sched_get_priority_max(SCHED_FIFO);
     sched_setscheduler(0, SCHED_FIFO, &sched);'
then
  SYSDEFS="${SYSDEFS} -DHAVE_SCHED_SETSCHEDULER"
fi

if [ $try_lockmem = "1" ] && \
  test_code \
    'mlockall()' \
    'sys/mman.h sys/resource.h' '' '' '
     struct rlimit rlim;
     setrlimit(RLIMIT_MEMLOCK, &rlim);
     mlockall(MCL_CURRENT|MCL_FUTURE);'
then
  SYSDEFS="${SYSDEFS} -DHAVE_MLOCKALL"
fi

READLINE_COMPILE=""
READLINE_LINK=""
if [ $feat_readline = "1" ]; then
  if [ $try_editline = "1" ]; then
    if test_code editline 'stdio.h editline/readline.h' \
      "$readline_inc" "$readline_lib -ledit" \
      'add_history(readline("prompt"));'
    then
      READLINE_COMPILE="-DFEAT_READLINE=1 -DUSE_EDITLINE=1 $readline_inc"
      READLINE_LINK="$readline_lib -ledit"
    fi
  fi

  if [ "x$READLINE_LINK" = "x" ] && [ $try_readline = "1" ]; then
    if test_code readline 'stdio.h readline/readline.h readline/history.h' \
      "$readline_inc" "$readline_lib $ncurses_lib -lreadline -lncurses" \
      'add_history(readline("prompt"));'
    then
      READLINE_COMPILE="-DFEAT_READLINE=1 $readline_inc"
      READLINE_LINK="$readline_lib $ncurses_lib -lreadline -lncurses"
    fi
  fi
fi

SYSCONFDIR=/etc
if [ "x$SETSYSCONFDIR" != "x" ]; then
  SYSCONFDIR=$SETSYSCONFDIR
fi

PREFIX=/usr/local
if [ "x$SETPREFIX" != "x" ]; then
  PREFIX=$SETPREFIX
fi

EPREFIX=${PREFIX}
if [ "x$SETEPREFIX" != "x" ]; then
  EPREFIX=$SETEPREFIX
fi

BINDIR=${EPREFIX}/bin
if [ "x$SETBINDIR" != "x" ]; then
  BINDIR=$SETBINDIR
fi

SBINDIR=${EPREFIX}/sbin
if [ "x$SETSBINDIR" != "x" ]; then
  SBINDIR=$SETSBINDIR
fi

DATAROOTDIR=${PREFIX}/share
if [ "x$SETDATAROOTDIR" != "x" ]; then
  DATAROOTDIR=$SETDATAROOTDIR
fi

INFODIR=${DATAROOTDIR}/info
if [ "x$SETINFODIR" != "x" ]; then
  INFODIR=$SETINFODIR
fi

MANDIR=${DATAROOTDIR}/man
if [ "x$SETMANDIR" != "x" ]; then
  MANDIR=$SETMANDIR
fi

DOCDIR=${DATAROOTDIR}/doc/chrony
if [ "x$SETDOCDIR" != "x" ]; then
  DOCDIR=$SETDOCDIR
fi

sed -e "s%@EXTRA_OBJECTS@%${EXTRA_OBJECTS}%;\
	s%@CC@%${MYCC}%;\
	s%@CFLAGS@%${MYCFLAGS}%;\
	s%@CCWARNFLAGS@%${CCWARNFLAGS}%;\
	s%@CPPFLAGS@%${CPPFLAGS}%;\
	s%@LIBS@%${LIBS}%;\
	s%@LDFLAGS@%${LDFLAGS}%;\
	s%@EXTRA_LIBS@%${EXTRA_LIBS}%;\
	s%@SYSDEFS@%${SYSDEFS}%;\
	s%@EXTRA_DEFS@%${EXTRA_DEFS}%;\
	s%@EXTRA_CLI_LIBS@%${EXTRA_CLI_LIBS}%;\
	s%@READLINE_COMPILE@%${READLINE_COMPILE}%;\
	s%@READLINE_LINK@%${READLINE_LINK}%;\
	s%@SYSCONFDIR@%${SYSCONFDIR}%;\
	s%@BINDIR@%${BINDIR}%;\
	s%@SBINDIR@%${SBINDIR}%;\
	s%@DOCDIR@%${DOCDIR}%;\
	s%@MANDIR@%${MANDIR}%;\
	s%@INFODIR@%${INFODIR}%;"\
	< Makefile.in > Makefile

# =======================================================================
# vim:et:sw=2:ht=2:sts=2:fdm=marker:cms=#%s

