dnl Process this file with autoconf to produce a configure script.
AC_INIT(Schedule.pm)

AC_MSG_CHECKING([which Big Sister version we take care of])
. ./version
AC_MSG_RESULT([$VERSION (build $RELEASE)])
PACKAGE_RELEASE=$RELEASE
BIGSISTER_VERSION=$VERSION
AC_SUBST(BIGSISTER_VERSION)
AC_SUBST(PACKAGE_RELEASE)

AC_MSG_CHECKING([the system type we are running on])
AC_SUBST(systemtype)
systemtype=`uname -s 2>/dev/null | tr A-Z a-z`
systemtype=${systemtype-unknown}
AC_MSG_RESULT([$systemtype])

dnl Checks for programs.
AC_PREFIX_DEFAULT(/usr/local/lib/bs)
AC_PROG_MAKE_SET
AC_PATH_PROG(perl,perl,perl)
AC_PATH_PROG(speedy,speedy,no)
AC_PROG_INSTALL()

AC_SUBST(speedypath)
AC_ARG_WITH(  speedy, [  --with-speedy	     the CGI accelerators (e.g. speedy) path],speedypath=$withval,speedypath="$speedy")
AC_MSG_CHECKING([which CGI accelerator we should use])
if test "$speedypath" = "no"; then
    AC_MSG_RESULT( "none" )
    speedypath="$perl"
else
    AC_MSG_RESULT( "$speedypath" )
fi

AC_MSG_CHECKING([wether we should apply FHS standard])
AC_ARG_ENABLE(fhs, [  --enable-fhs	     use file hierarchy standard install directories],fhs=yes,fhs=no)
AC_ARG_ENABLE(FHS, [  --enable-FHS	     same as --enable-fhs],fhs=yes,)
AC_MSG_RESULT($fhs)

AC_SUBST(cgipath)
AC_MSG_CHECKING([which CGI directory we should use])
AC_ARG_WITH(  cgi, [  --with-cgi	     the CGI path we should use],cgipath=$withval,cgipath=/cgi)
AC_MSG_RESULT($cgipath)

AC_SUBST(bsuser)
AC_MSG_CHECKING([under which user account we will run Big Sister])
AC_ARG_WITH(  user, [  --with-user	     the user account Big Sister will run under],bsuser=$withval,bsuser=bs)
AC_MSG_RESULT($bsuser)

AC_SUBST(webroot)
AC_MSG_CHECKING([which URL the web pages will be visible as])
AC_ARG_WITH(  url, [  --with-url	     the URL at which we will find the web pages],webroot=$withval,webroot=/bs)
if test "$webroot" = "/"; then
    # this is the logical way - but as P. Rogan pointed out supplying "/" looks legitimate
    webroot=""
fi
AC_MSG_RESULT($webroot)

AC_SUBST(perlext)
AC_MSG_CHECKING([which file extension perl scripts (CGIs) should get])
AC_ARG_WITH(  perlext, [  --with-perlext	     the file extension perl scripts (CGIs) should get],perlext=$withval)
extres=${perlext-none}
AC_MSG_RESULT($extres)

AC_SUBST(UXMON_DIR)
AC_SUBST(BIN_DIR)
AC_SUBST(VAR_DIR)
AC_SUBST(ETC_DIR)
AC_SUBST(ADM_DIR)
AC_SUBST(WEB_DIR)
AC_SUBST(CGI_DIR)
AC_SUBST(DOC_DIR)
AC_SUBST(MAN_DIR)
AC_SUBST(HELP_DIR)
AC_SUBST(USER_SBIN_DIR)
AC_SUBST(USER_BIN_DIR)
AC_SUBST(RUN_DIR)
AC_SUBST(INIT_DIR)
AC_SUBST(LIB_ROOT)
AC_SUBST(EX_UXMON_DIR)
AC_SUBST(EX_BIN_DIR)
AC_SUBST(EX_VAR_DIR)
AC_SUBST(EX_ETC_DIR)
AC_SUBST(EX_ADM_DIR)
AC_SUBST(EX_WEB_DIR)
AC_SUBST(EX_CGI_DIR)
AC_SUBST(EX_USER_SBIN_DIR)
AC_SUBST(EX_USER_BIN_DIR)
AC_SUBST(EX_RUN_DIR)
AC_SUBST(EX_INIT_DIR)
AC_SUBST(EX_LIB_ROOT)
AC_SUBST(EX_DOC_DIR)
AC_SUBST(EX_HELP_DIR)
AC_SUBST(EX_MAN_DIR)
root=${prefix}
ex_prefix=${exec_prefix}

if test "$fhs" = "yes"; then
    if test "$root" = "NONE"; then
	root=""
    fi
    if test "$ex_prefix" = "NONE"; then
	ex_prefix="$root"
    fi
    prefix=/usr/share/bigsister
    UXMON_DIR=${root}/usr/share/bigsister/uxmon
    BIN_DIR=${root}/usr/share/bigsister/bin
    VAR_DIR=${root}/var/lib/bigsister
    ETC_DIR=${root}/usr/share/bigsister/etc
    ADM_DIR=${root}/etc/bigsister
    WEB_DIR=${root}/var/lib/bigsister/www
    CGI_DIR=${root}/usr/share/bigsister/cgi
    USER_SBIN_DIR=${root}/usr/sbin
    USER_BIN_DIR=${root}/usr/bin
    RUN_DIR=/var/run
    LIB_ROOT=/usr/share/bigsister
    EX_UXMON_DIR=${ex_prefix}/usr/share/bigsister/uxmon
    EX_BIN_DIR=${ex_prefix}/usr/share/bigsister/bin
    EX_VAR_DIR=${ex_prefix}/var/lib/bigsister
    EX_ETC_DIR=${ex_prefix}/usr/share/bigsister/etc
    EX_ADM_DIR=${ex_prefix}/etc/bigsister
    EX_WEB_DIR=${ex_prefix}/var/lib/bigsister/www
    EX_CGI_DIR=${ex_prefix}/usr/share/bigsister/cgi
    EX_USER_SBIN_DIR=${ex_prefix}/usr/sbin
    EX_USER_BIN_DIR=${ex_prefix}/usr/bin
    EX_RUN_DIR=/var/run
    EX_MAN_DIR=${ex_prefix}/usr/share/man
    MAN_DIR=${root}/usr/share/man
    for dir in /usr/share/doc/packages /usr/doc/packages /usr/share/doc; do
	if test -d "$dir"; then
	    DOC_DIR=${root}$dir/bigsister
	    EX_DOC_DIR=${ex_prefix}$dir/bigsister
	    break
	fi
    done
    if test "$DOC_DIR" = ""; then
        DOC_DIR=${root}/usr/share/bigsister/doc
        EX_DOC_DIR=${ex_prefix}/usr/share/bigsister/doc
    fi
else
    if test "$root" = "NONE"; then
	root="$ac_default_prefix"
    fi
    if test "$ex_prefix" = "NONE"; then
	ex_prefix="$root"
    fi
    LIB_ROOT=$ex_prefix
    UXMON_DIR=${root}/uxmon
    BIN_DIR=${root}/bin
    VAR_DIR=${root}/var
    ETC_DIR=${root}/etc
    ADM_DIR=${root}/adm
    WEB_DIR=${root}/www
    CGI_DIR=${root}/cgi
    DOC_DIR=${root}/doc
    MAN_DIR=${root}/man
    USER_SBIN_DIR=${BIN_DIR}
    USER_BIN_DIR=${BIN_DIR}
    RUN_DIR=${root}/adm
    EX_UXMON_DIR=${ex_prefix}/uxmon
    EX_BIN_DIR=${ex_prefix}/bin
    EX_VAR_DIR=${ex_prefix}/var
    EX_ETC_DIR=${ex_prefix}/etc
    EX_ADM_DIR=${ex_prefix}/adm
    EX_WEB_DIR=${ex_prefix}/www
    EX_CGI_DIR=${ex_prefix}/cgi
    EX_DOC_DIR=${ex_prefix}/doc
    EX_USER_SBIN_DIR=${EX_BIN_DIR}
    EX_USER_BIN_DIR=${EX_BIN_DIR}
    EX_RUN_DIR=${ex_prefix}/adm
    EX_MAN_DIR=${ex_prefix}/man
fi


AC_MSG_CHECKING([where to store doc files])
AC_MSG_RESULT($DOC_DIR)
AC_MSG_CHECKING([where to store uxmon files])
AC_MSG_RESULT($UXMON_DIR)
AC_MSG_CHECKING([where to store executable files])
AC_MSG_RESULT($BIN_DIR)
AC_MSG_CHECKING([where to store variable files])
AC_MSG_RESULT($VAR_DIR)
AC_MSG_CHECKING([where to store shareable config files])
AC_MSG_RESULT($ETC_DIR)
AC_MSG_CHECKING([where to store local config files])
AC_MSG_RESULT($ADM_DIR)
AC_MSG_CHECKING([where to store web root])
AC_MSG_RESULT($WEB_DIR)
AC_MSG_CHECKING([where to store system administrator commands])
AC_MSG_RESULT($USER_SBIN_DIR)
AC_MSG_CHECKING([where to store user commands])
AC_MSG_RESULT($USER_BIN_DIR)

AC_MSG_CHECKING([where to store help documents])
if test -d docu/help; then
    HELP_DIR=${WEB_DIR}/help
    EX_HELP_DIR=${EX_WEB_DIR}/help
    AC_MSG_RESULT($HELP_DIR)
else
    HELP_DIR=""
    EX_HELP_DIR=""
    AC_MSG_RESULT([docu not present])
fi

AC_MSG_CHECKING([where to store man pages])
if test -d docu/man; then
    AC_MSG_RESULT($MAN_DIR)
else
    MAN_DIR=""
    EX_MAN_DIR=""
    AC_MSG_RESULT([docu not present])
fi

AC_MSG_CHECKING([where to put Big Sister boot skript to])
for dir in /etc/init.d /etc/rc.d/init.d /sbin/init.d; do
    if test -d $dir; then
        INIT_DIR=${dir}
		break
    fi
done
if test "$INIT_DIR" = ""; then
    AC_MSG_WARN(there seems to be no System V init dir - won't install boot script)
else
    AC_MSG_RESULT($INIT_DIR)
fi

AC_SUBST(chkconfig)
AC_PATH_PROG(chkconfig,chkconfig,"",${PATH}:/sbin:/usr/sbin)
AC_MSG_CHECKING([if and where we find the RPM builder directories])
RPM_DIRECTORY=""
for dir in /usr/src/*; do
    if test -d "$dir/SPECS"; then
        RPM_DIRECTORY="$dir"
        break
    fi
done
if test "$RPM_DIRECTORY" = ""; then
    AC_MSG_RESULT(none)
else
    AC_MSG_RESULT($RPM_DIRECTORY)
fi
AC_SUBST(RPM_DIRECTORY)

AC_SUBST(crondir)
AC_MSG_CHECKING([where to store weekly cronjobs])
for dir in /etc/cron.weekly; do
    if test -d $dir; then
	    crondir=$dir
    fi
done 

if test "$crondir" = ""; then
    AC_MSG_RESULT(none)
else
    AC_MSG_RESULT($crondir)
fi


# ===============test_perl()=======================================

test_perl() {
    cat >/tmp/$$.perl
    result=`perl -Itools -Iuxmon -Iplatforms /tmp/$$.perl 2>&1`
    rm /tmp/$$.perl
    case "$result" in
        *SUCCESS*)
	    return 0
    esac
    return 1
}


# ==================gettimeofday==================================

AC_SUBST(avoid_gettimeofday_syscall)
AC_MSG_CHECKING([wether gettimeofday() works])
test_perl <<END
use MicroTime;

if( MicroTime::gettimeofday() ) {
    print "SUCCESS\n";
}
END
if test $? -gt 0; then
    avoid_gettimeofday_syscall=1
    test_perl <<END
\$BigSister::common::resources{"avoid_gettimeofday_syscall"} = 1;
require MicroTime;
if( MicroTime::gettimeofday() ) {
    print "SUCCESS\n";
}
END
    if test $? -gt 0; then
        AC_MSG_RESULT([completely failed])
	exit 1
    else
        AC_MSG_RESULT([ok, but we suppress syscall])
    fi
else
    avoid_gettimeofday_syscall=0
    test_perl <<END
use MicroTime;
for( my \$i=0; \$i<2; \$i++ ) {
    my \$time = MicroTime::gettimeofday();
    if( \$time != int(\$time) ) {
        print "SUCCESS\n";
	exit(0);
    }
}
END
    if test $? -gt 0; then
        AC_MSG_RESULT([ok, but we won't measure time intervals < 1s])
    else
        AC_MSG_RESULT([ok])
    fi
fi
        

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

dnl Checks for libraries.
dnl Checks for header files.
dnl Checks for typedefs, structures, and compiler characteristics.
dnl Checks for library functions.
AC_OUTPUT(Makefile etc/bigsister.spec etc/httpd.conf etc/etc_resources)
