dnl Process this file with autoconf to produce a configure script.

AC_INIT(backup.h)

osname=`./prosname.sh`

ARCH="`./prosspec.sh` $*"
ARCH="`echo $ARCH`"		# for some stupid shells
if test -r config.setup ; then
  CONFARCH=`cat config.setup`
  if test "$CONFARCH" = "$ARCH" ; then
    echo "Already configured for this setup."
    exit 0
  fi
fi
rm -f config.setup

AC_CONFIG_HEADER(conf.h)

dnl Checks for programs.

dnl Checks for libraries.

dnl Checks for header files.
AC_HEADER_DIRENT
AC_HEADER_STDC
dnl AC_HEADER_SYS_WAIT
AC_CHECK_HEADERS(arpa/inet.h fcntl.h fnmatch.h limits.h malloc.h netinet/ip.h netinet/in_systm.h regex.h string.h strings.h sys/ioctl.h sys/mode.h sys/mount.h sys/param.h sys/resource.h sys/select.h sys/statfs.h sys/time.h sys/timers.h sys/vfs.h sys/wait.h termios.h termio.h time.h unistd.h values.h)

dnl Checks for typedefs, structures, and compiler characteristics.
AC_STRUCT_ST_RDEV
AC_HEADER_TIME
AC_STRUCT_TM

dnl Checks for library functions.
AC_FUNC_ALLOCA
AC_FUNC_FNMATCH
AC_PROG_GCC_TRADITIONAL
AC_FUNC_MEMCMP
AC_TYPE_SIGNAL
AC_FUNC_UTIME_NULL
AC_FUNC_VPRINTF

OPTIMIZE=-O2
if test $ac_cv_prog_gcc = no -a `echo $osname|grep 'HPUX'|wc -l` -lt 1; then
  EXTRACFLAGS="+Olibcalls -Ae -Dhpux -Dunix -Dnotdef"
  OPTIMIZE=-O
fi

AC_CHECK_TYPE(timer_t, int)
AC_CHECK_TYPE(signal_t, int)

checkBoth=0
AC_CHECK_FUNC(connect, checkSocket=0, checkSocket=1)
if test "$checkSocket" = 1; then
    AC_CHECK_LIB(socket, main, LIBS="$LIBS -lsocket", checkBoth=1)
fi
if test "$checkBoth" = 1; then
    oldLibs=$LIBS
    LIBS="$LIBS -lsocket -lnsl"
    AC_CHECK_FUNC(accept, checkNsl=0, [LIBS=$oldLibs])
fi
AC_CHECK_FUNC(gethostbyname, , AC_CHECK_LIB(nsl, main, [LIBS="$LIBS -lnsl"]))

AC_CHECK_FUNCS(bsearch drand48 getcwd gettimeofday inet_ntoa isnan lchown lfind lsearch memmove mkdir mkfifo mktime qsort rand re_comp regcomp re_compile_pattern rint select setitimer sighold sigrelse socket strdup strerror strrstr strstr timer_create uname)

if test "$ac_cv_func_re_comp" != yes ; then
    AC_CHECK_LIB(compat, re_comp, [LIBS="$LIBS -lcompat"])
    unset ac_cv_func_re_comp
    AC_CHECK_FUNCS(re_comp)
fi

prefixext=yes
AC_ARG_WITH(prefixext,
    [   --with-prefixext=YESNO extend prefix with /backup [yes]],
    [
        if test "$withval"; then
            prefixext="$withval"
        else
            AC_MSG_WARN([*** You must supply an argument to the --with-prefixext
 option.])
        fi
    ]
)
subdir="/backup"
if test $prefixext != "yes" ; then
  subdir=""
fi

AC_ARG_WITH(serverbindir,
    [   --with-serverbindir=DIR   server binaries in DIR [prefix/subdir/server/bin]],
    [
        if test "$withval"; then
            serverbindir="$withval"
        else
            AC_MSG_WARN([*** You must supply an argument to the --with-serverbindir
 option.])
        fi
    ]
)
: ${serverbindir:='$prefix$subdir/server/bin'}
serverbindir=`(
    test "x$prefix" = xNONE && prefix=$ac_default_prefix
    test "x$exec_prefix" = xNONE && exec_prefix=${prefix}
    eval echo "$serverbindir"
)`
AC_SUBST(serverbindir)
AC_ARG_WITH(serverlibdir,
    [   --with-serverlibdir=DIR   server configuration files in DIR [prefix/subdir/server/lib]],
    [
        if test "$withval"; then
            serverlibdir="$withval"
        else
            AC_MSG_WARN([*** You must supply an argument to the --with-serverlibdir
 option.])
        fi
    ]
)
: ${serverlibdir:='$prefix$subdir/server/lib'}
serverlibdir=`(
    test "x$prefix" = xNONE && prefix=$ac_default_prefix
    test "x$exec_prefix" = xNONE && exec_prefix=${prefix}
    eval echo "$serverlibdir"
)`
AC_SUBST(serverlibdir)
AC_ARG_WITH(servervardir,
    [   --with-servervardir=DIR   variable server files in DIR [prefix/subdir/server/var]],
    [
        if test "$withval"; then
            servervardir="$withval"
        else
            AC_MSG_WARN([*** You must supply an argument to the --with-servervardir
 option.])
        fi
    ]
)
: ${servervardir:='$prefix$subdir/server/var'}
servervardir=`(
    test "x$prefix" = xNONE && prefix=$ac_default_prefix
    test "x$exec_prefix" = xNONE && exec_prefix=${prefix}
    eval echo "$servervardir"
)`
AC_SUBST(servervardir)
AC_ARG_WITH(servermandir,
    [   --with-servermandir=DIR   variable server files in DIR [prefix/subdir/server/var]],
    [
        if test "$withval"; then
            servermandir="$withval"
        else
            AC_MSG_WARN([*** You must supply an argument to the --with-servermandir
 option.])
        fi
    ]
)
: ${servermandir:='$prefix$subdir/server/man'}
servermandir=`(
    test "x$prefix" = xNONE && prefix=$ac_default_prefix
    test "x$exec_prefix" = xNONE && exec_prefix=${prefix}
    eval echo "$servermandir"
)`
AC_SUBST(servermandir)
AC_ARG_WITH(serverlogdir,
    [   --with-serverlogdir=DIR   server log files in DIR [prefix/subdir/server/var]],
    [
        if test "$withval"; then
            serverlogdir="$withval"
        else
            AC_MSG_WARN([*** You must supply an argument to the --with-serverlogdir
 option.])
        fi
    ]
)
: ${serverlogdir:='$prefix$subdir/server/var'}
serverlogdir=`(
    test "x$prefix" = xNONE && prefix=$ac_default_prefix
    test "x$exec_prefix" = xNONE && exec_prefix=${prefix}
    eval echo "$serverlogdir"
)`
AC_SUBST(serverlogdir)
AC_ARG_WITH(clientbindir,
    [   --with-clientbindir=DIR   client binaries in DIR [prefix/subdir/client/bin]],
    [
        if test "$withval"; then
            clientbindir="$withval"
        else
            AC_MSG_WARN([*** You must supply an argument to the --with-clientbindir
 option.])
        fi
    ]
)
: ${clientbindir:='$prefix$subdir/client/bin'}
clientbindir=`(
    test "x$prefix" = xNONE && prefix=$ac_default_prefix
    test "x$exec_prefix" = xNONE && exec_prefix=${prefix}
    eval echo "$clientbindir"
)`
AC_SUBST(clientbindir)
AC_ARG_WITH(clientlibdir,
    [   --with-clientlibdir=DIR   client configuration files in DIR [prefix/subdir/client/lib]],
    [
        if test "$withval"; then
            clientlibdir="$withval"
        else
            AC_MSG_WARN([*** You must supply an argument to the --with-clientlibdir
 option.])
        fi
    ]
)
: ${clientlibdir:='$prefix$subdir/client/lib'}
clientlibdir=`(
    test "x$prefix" = xNONE && prefix=$ac_default_prefix
    test "x$exec_prefix" = xNONE && exec_prefix=${prefix}
    eval echo "$clientlibdir"
)`
AC_SUBST(clientlibdir)
AC_ARG_WITH(clientvardir,
    [   --with-clientvardir=DIR   variable client files in DIR [prefix/subdir/client/var]],
    [
        if test "$withval"; then
            clientvardir="$withval"
        else
            AC_MSG_WARN([*** You must supply an argument to the --with-clientvardir
 option.])
        fi
    ]
)
: ${clientvardir:='$prefix$subdir/client/var'}
clientvardir=`(
    test "x$prefix" = xNONE && prefix=$ac_default_prefix
    test "x$exec_prefix" = xNONE && exec_prefix=${prefix}
    eval echo "$clientvardir"
)`
AC_SUBST(clientvardir)
AC_ARG_WITH(clientmandir,
    [   --with-clientmandir=DIR   variable client files in DIR [prefix/subdir/client/var]],
    [
        if test "$withval"; then
            clientmandir="$withval"
        else
            AC_MSG_WARN([*** You must supply an argument to the --with-clientmandir
 option.])
        fi
    ]
)
: ${clientmandir:='$prefix$subdir/client/man'}
clientmandir=`(
    test "x$prefix" = xNONE && prefix=$ac_default_prefix
    test "x$exec_prefix" = xNONE && exec_prefix=${prefix}
    eval echo "$clientmandir"
)`
AC_SUBST(clientmandir)
AC_ARG_WITH(clientlogdir,
    [   --with-clientlogdir=DIR   client log files in DIR [prefix/subdir/client/var]],
    [
        if test "$withval"; then
            clientlogdir="$withval"
        else
            AC_MSG_WARN([*** You must supply an argument to the --with-clientlogdir
 option.])
        fi
    ]
)
: ${clientlogdir:='$prefix$subdir/client/var'}
clientlogdir=`(
    test "x$prefix" = xNONE && prefix=$ac_default_prefix
    test "x$exec_prefix" = xNONE && exec_prefix=${prefix}
    eval echo "$clientlogdir"
)`
AC_SUBST(clientlogdir)
AC_ARG_WITH(rexecdir,
    [   --with-rexecdir=DIR   binaries for remote execution in DIR [prefix/rexec]],
    [
        if test "$withval"; then
            REXECDIR="$withval"
        else
            AC_MSG_WARN([*** You must supply an argument to the --with-rexecdir
 option.])
        fi
    ]
)
: ${REXECDIR:='$prefix$subdir/rexec'}
REXECDIR=`(
    test "x$prefix" = xNONE && prefix=$ac_default_prefix
    test "x$exec_prefix" = xNONE && exec_prefix=${prefix}
    eval echo "$REXECDIR"
)`
AC_SUBST(REXECDIR)
AC_ARG_WITH(clientconf,
    [   --with-clientconf=NAME   NAME for client configuration [backup.conf]],
    [
        if test "$withval"; then
            clientconf="$withval"
        else
            AC_MSG_WARN([*** You must supply an argument to the --with-clientconf
 option.])
        fi
    ]
)
: ${clientconf:=backup.conf}
clientconf=`(
    test "x$prefix" = xNONE && prefix=$ac_default_prefix
    test "x$exec_prefix" = xNONE && exec_prefix=${prefix}
    eval echo "$clientconf"
)`
AC_SUBST(clientconf)
AC_ARG_WITH(serverconf,
    [   --with-serverconf=NAME   NAME for server configuration [backup.conf]],
    [
        if test "$withval"; then
            serverconf="$withval"
        else
            AC_MSG_WARN([*** You must supply an argument to the --with-serverconf
 option.])
        fi
    ]
)
: ${serverconf:=backup.conf}
serverconf=`(
    test "x$prefix" = xNONE && prefix=$ac_default_prefix
    test "x$exec_prefix" = xNONE && exec_prefix=${prefix}
    eval echo "$serverconf"
)`
AC_SUBST(serverconf)


dnl DES stuff

usedes=no
AC_ARG_WITH(des,
    [   --with-des   for client/server authentication [no]],
    [
        usedes=yes
	des_include=../libdes
	des_libdir=`echo $des_include|sed 's#include$#lib#g'`
	des_header=des.h
	des_ldflag=-ldes
    ]
)

AC_ARG_WITH(des-include,
    [   --with-des-include=DIR   des header file in DIR [../libdes]],
    [
        if test "$withval"; then
            des_include="$withval"
	    des_libdir=$des_include
        else
            AC_MSG_WARN([*** You must supply an argument to the --with-des-include
 option.])
        fi
    ]
)

AC_ARG_WITH(des-header,
    [   --with-des-header=NAME   NAME is des header file [des.h]],
    [
        if test "$withval"; then
            des_header="$withval"
        else
            AC_MSG_WARN([*** You must supply an argument to the --with-des-header
 option.])
        fi
    ]
)

AC_ARG_WITH(des-libdir,
    [   --with-des-libdir=DIR   des library is in DIR [../libdes]],
    [
        if test "$withval"; then
            des_libdir="$withval"
        else
            AC_MSG_WARN([*** You must supply an argument to the --with-des-libdir
 option.])
        fi
    ]
)

AC_ARG_WITH(des-ldflag,
    [   --with-des-ldflag=LIB   des library is specified by LIB [-ldes]],
    [
        if test "$withval"; then
            des_ldflag="$withval"
        else
            AC_MSG_WARN([*** You must supply an argument to the --with-des-ldflag
 option.])
        fi
    ]
)

if test $usedes = "no" ; then
  DESINCLUDEPATH=""
  DESDEFINES=""
  DESLIB=""
  DESLIBPATH=""
  DESHEADER=""
else
  if test `echo $des_ldflag|grep '^lib'|wc -l` -gt 0 ; then
    des_ldflag=`echo $des_ldflag|sed 's/^lib/-l/g;s/[.].*//g'`
  fi

  DESINCLUDEPATH="-I$des_include"
  DESDEFINES="-DUSE_DES_ENCRYPTION"
  DESHEADER="$des_header"
  DESLIBPATH="-L$des_libdir"
  DESLIB="$des_ldflag"
fi

AC_SUBST(DESDEFINES)
AC_SUBST(DESINCLUDEPATH)
AC_SUBST(DESLIBPATH)
AC_SUBST(DESLIB)
AC_SUBST(DESHEADER)

AC_SUBST(EXTRACFLAGS)
AC_SUBST(OPTIMIZE)

AC_SUBST(osname)
AC_SUBST(CC)
AC_OUTPUT(budefs.h des_aux.h Makefile server.conf client.conf xcc xsc clientconfig serverconfig afclient.8 cartis.8 cartready.8 afserver.8 label_tape.8 full_backup.8 incr_backup.8 afrestore.8 afverify.8 print_errors.8 afclient.conf.8 afserver.conf.8 CONFIG)

echo "$ARCH" > config.setup
