AC_INIT([minilb],[0.0.1])
AM_INIT_AUTOMAKE([foreign subdir-objects tar-pax])
AC_PREFIX_DEFAULT(/usr/local)

PACKAGE=minilb

AC_CHECK_PROG(HAVE_GO, go, true, false)
if test "x$HAVE_GO" = "xfalse"; then
    AC_MSG_ERROR([Requested program go not found])
fi

AC_PATH_PROG([GO],[go])
AC_PATH_PROG([FIND],[find])
AC_PATH_PROG([XARGS],[xargs])
AC_PATH_PROG([SORT],[sort])
AC_PATH_PROG([UNIQ],[uniq])
AC_PATH_PROGS([SUDO],[sudo false])
AC_PATH_PROGS([CPIO],[cpio false])
AC_PATH_PROGS([BASENAME],[basename])

AC_PATH_PROGS([PROTOC],[protoc true])
AC_PATH_PROGS([PODMAN],[podman true])
AC_PATH_PROGS([HELM],[helm true])
AC_PATH_PROGS([DBUILDPACKAGE],[dpkg-buildpackage true])
AC_PATH_PROGS([DPKGSOURCE],[dpkg-source true])

AC_PATH_PROGS([CP],[gcp cp])
if test -z "$CP"; then
    AC_MSG_ERROR([Requested program cp not found])
fi

AC_PROG_INSTALL
AC_PROG_MKDIR_P
AC_CANONICAL_HOST

case $host_os in
    *freebsd* )
    AC_SUBST(ROOT_GROUP, "wheel")
    AM_CONDITIONAL(FREEBSD_OS, true)
    AM_CONDITIONAL(LINUX_OS, false)
    OSNAME=freebsd
    ROOT_GROUP=wheel
        ;;
    *linux* )
    AC_SUBST(ROOT_GROUP, "root")
    AM_CONDITIONAL(FREEBSD_OS, false)
    AM_CONDITIONAL(LINUX_OS, true)
    OSNAME=linux
    ROOT_GROUP=root
        ;;
esac

AM_CONDITIONAL(SYSTEMD, false)
if test -d /lib/systemd/system; then
    AM_CONDITIONAL(SYSTEMD, true)
fi


test "x$prefix" == "xNONE" && prefix=$ac_default_prefix
test "x$libexecdir" == "xNONE" && libexecdir=${prefix}/lib

dnl --------------------------------------------------------------------------------------

case $host_os in
    *freebsd* )
        default_user="daemon"
        default_group="daemon"
        ;;
    *linux* )
        default_user="daemon"
        default_group="daemon"
        ;;
esac

AC_ARG_WITH(user,
        AS_HELP_STRING([--with-user=${PACKAGE}],[set executing user name]),
        [ if test ! -z "$with_user" ; then
                case $with_user in
                        "")
                                AC_MSG_ERROR(You must specify user name)
                                ;;
                        *)
                                RUN_USER="$with_user"
                                ;;
                esac
        else
                RUN_USER="$default_user"
        fi ],
        [ RUN_USER="$default_user" ])

AS_IF([test "x$enable_devel_mode" = "xyes"], [
        RUN_USER="`id -un`"
])

AC_MSG_NOTICE(run_user set as ${RUN_USER})
AC_DEFINE_UNQUOTED(RUN_USER, "$RUN_USER", [effective user])
AC_SUBST(RUN_USER, "$RUN_USER")
AC_DEFINE_UNQUOTED(run_user, "$RUN_USER", [effective user])
AC_SUBST(run_user, "$RUN_USER")

dnl --------------------------------------------------------------------------------------

AC_ARG_ENABLE([devel-mode],
    AS_HELP_STRING([--enable-devel-mode], [Enable developmend mode]))


AC_DEFINE_UNQUOTED(srv_devel_mode, "false", [developmend mode])
AC_SUBST(srv_devel_mode, "false")

AS_IF([test "x$enable_devel_mode" = "xyes"], [
    SRCDIR=`pwd`
    enable_devel_mode=yes
])

dnl --------------------------------------------------------------------------------------

SRV_CONFDIR="${prefix}/etc/${PACKAGE}"

AC_ARG_WITH(confdir,
    AS_HELP_STRING([--with-confdir=PATH],[set configuration dir to PATH (default: $SRV_CONFDIR)]),
    [ if test ! -z "$withval" ; then
        case $withval in
            /*)
                SRV_CONFDIR="$withval"
                ;;
            *)
                AC_MSG_ERROR(You must specify an absolute path to --with-confdir=PATH)
                ;;
        esac
    fi ])

AS_IF([test "x$enable_devel_mode" = "xyes"], [
    SRV_CONFDIR="${SRCDIR}/etc/${PACKAGE}"
    sysconfdir="${SRCDIR}/etc/${PACKAGE}"
], [
    test "x$SRV_CONFDIR" == "x/usr/etc/${PACKAGE}" && SRV_CONFDIR="/etc/${PACKAGE}"
    test "x$prefix" == "x/usr" && sysconfdir="/etc"
])

AC_DEFINE_UNQUOTED(SRV_CONFDIR, "$SRV_CONFDIR", [location of configuration files for ${PACKAGE}])
AC_SUBST(srv_confdir, "$SRV_CONFDIR")

AC_MSG_NOTICE(srv_confdir set as ${SRV_CONFDIR})

dnl --------------------------------------------------------------------------------------

SRV_LOGDIR="/var/log/${PACKAGE}"

AC_ARG_WITH(logdir,
    AS_HELP_STRING([--with-logdir=PATH],[set path for logdir (default: $SRV_LOGDIR)]),
    [ if test ! -z "$withval" ; then
        case $withval in
            /*)
                SRV_LOGDIR="$withval"
                ;;
            *)
                AC_MSG_ERROR(You must specify an absolute path to --with-logdir=PATH)
                ;;
        esac
    fi])

AS_IF([test "x$enable_devel_mode" = "xyes"], [
    SRV_LOGDIR="${SRCDIR}/tmp/log"
])

AC_DEFINE_UNQUOTED(SRV_LOGDIR, "$SRV_LOGDIR", [location of ${PACKAGE} logdir])
AC_SUBST(srv_logdir, "$SRV_LOGDIR")

AC_MSG_NOTICE(srv_logdir set as ${SRV_LOGDIR})

dnl --------------------------------------------------------------------------------------

SRV_RUNDIR="/var/run/${PACKAGE}"

AC_ARG_WITH(rundir,
    AS_HELP_STRING([--with-rundir=PATH],[set path for rundir (default: $SRV_RUNDIR)]),
    [ if test ! -z "$withval" ; then
        case $withval in
            /*)
                SRV_RUNDIR="$withval"
                ;;
            *)
                AC_MSG_ERROR(You must specify an absolute path to --with-rundir=PATH)
                ;;
        esac
    fi ])

AS_IF([test "x$enable_devel_mode" = "xyes"], [
    SRV_RUNDIR="${SRCDIR}/tmp/run"
])


AC_DEFINE_UNQUOTED(SRV_RUNDIR, "$SRV_RUNDIR", [location of rundir])
AC_SUBST(srv_rundir, "$SRV_RUNDIR")

AC_MSG_NOTICE(srv_rundir set as ${SRV_RUNDIR})

dnl --------------------------------------------------------------------------------------

SRV_SHAREDIR="${prefix}/share/$PACKAGE"

AC_ARG_WITH(sharedir,
    AS_HELP_STRING([--with-sharedir=PATH],[set share directory (default: $SRV_SHAREDIR)]),
    [ if test ! -z "$with_sharedir" ; then
        case $with_sharedir in
            /*)
                SRV_SHAREDIR="$withval"
                ;;
            *)
                AC_MSG_ERROR(You must specify an absolute path to --with-sharedir=PATH)
                ;;
        esac
    fi ])

AS_IF([test "x$enable_devel_mode" = "xyes"], [
    SRV_SHAREDIR="${SRCDIR}/share"
])


AC_DEFINE_UNQUOTED(SRV_SHAREDIR, "$SRV_SHAREDIR", [location of share dir])
AC_SUBST(srv_sharedir, "$SRV_SHAREDIR")

AC_MSG_NOTICE(srv_sharedir set as ${SRV_SHAREDIR})

dnl --------------------------------------------------------------------------------------

SRV_LIBDIR="${prefix}/lib/$PACKAGE"

AC_ARG_WITH(libdir,
    AS_HELP_STRING([--with-libdir=PATH],[set lib directory (default: $SRV_LIBDIR)]),
    [ if test ! -z "$withval" ; then
        case $withval in
            /*)
                SRV_LIBDIR="$withval"
                ;;
            *)
                AC_MSG_ERROR(You must specify an absolute path to --with-libdir=PATH)
                ;;
        esac
    fi ])

AS_IF([test "x$enable_devel_mode" = "xyes"], [
    SRV_LIBDIR="${SRCDIR}/sysagent"
])

AC_DEFINE_UNQUOTED(SRV_LIBDIR, "$SRV_LIBDIR", [location of lib dir])
AC_SUBST(srv_libdir, "$SRV_LIBDIR")

AC_MSG_NOTICE(srv_libdir set as ${SRV_LIBDIR})

dnl --------------------------------------------------------------------------------------


SRV_DATADIR="/var/lib/${PACKAGE}"

AC_ARG_WITH(datadir,
    AS_HELP_STRING([--with-datadir=PATH],[set path for datadir (default: $SRV_DATADIR)]),
    [ if test ! -z "$withval" ; then
        case $withval in
            /*)
                SRV_DATADIR="$withval"
                ;;
            *)
                AC_MSG_ERROR(You must specify an absolute path to --with-datadir=PATH)
                ;;
        esac
    fi])

AS_IF([test "x$enable_devel_mode" = "xyes"], [
    SRV_DATADIR="${SRCDIR}/tmp/data"
])

AC_DEFINE_UNQUOTED(SRV_DATADIR, "$SRV_DATADIR", [location of ${PACKAGE} datadir])
AC_SUBST(srv_datadir, "$SRV_DATADIR")

AC_MSG_NOTICE(srv_datadir set as ${SRV_DATADIR})

dnl --------------------------------------------------------------------------------------

AC_SUBST(srv_sbindir, "${prefix}/sbin")
AC_SUBST(srv_bindir, "${prefix}/bin")


AC_CONFIG_FILES([
Makefile
app/config/variant.go
initrc/minilbd.service
initrc/minilbd
debian/control
debian/changelog
])
AC_OUTPUT
