AC_INIT([engine],[0.1.0])
AM_INIT_AUTOMAKE
AC_PREFIX_DEFAULT(/usr/local)

PACKAGE=engine

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([GO],[go])

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

AC_PATH_PROGS([YARN],[yarn npm])
if test -z "$YARN"; then
    AC_MSG_ERROR([Requested program yarn not found])
fi

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


dnl AC_PATH_PROGS([M4],[m4 gm4 true])
dnl if test -z "$M4"; then
dnl     AC_MSG_ERROR([Requested program m4 not found])
dnl fi


dnl AC_PATH_PROGS([DOCKER],[podman docker true])
dnl if test -z "$CP"; then
dnl     AC_MSG_ERROR([Requested program docker or podman not found])
dnl fi

dnl AC_PATH_PROGS([HELM],[helm true])

AC_PROG_INSTALL
AC_PROG_MKDIR_P
AC_CANONICAL_HOST

dnl --------------------------------------------------------------------------------------
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

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

dnl AC_ARG_ENABLE([docker-mode],
dnl     AS_HELP_STRING([--enable-docker-mode], [Enable docker mode]))


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

AS_IF([test "x$enable_devel_mode" = "xyes"], [
    AC_DEFINE_UNQUOTED(srv_devel_mode, "true", [developmend mode])
    AC_SUBST(srv_devel_mode, "true")
    SRCDIR=`pwd`
    enable_devel_mode=yes
])


dnl AC_DEFINE_UNQUOTED(srv_docker_mode, "false", [dockeropmend mode])
dnl AC_SUBST(srv_docker_mode, "false")

dnl AS_IF([test "x$enable_docker_mode" = "xyes"], [
dnl     AC_DEFINE_UNQUOTED(srv_docker_mode, "true", [dockeropmend mode])
dnl     AC_SUBST(srv_docker_mode, "true")
dnl     SRCDIR=`pwd`
dnl     enable_docker_mode=yes
dnl ])


test "x$prefix" == "xNONE" && prefix=$ac_default_prefix
PREFIX=$prefix

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

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

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_MSG_NOTICE(srv_confdir set as ${SRV_CONFDIR})

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

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

AC_ARG_WITH(logdir,
    AS_HELP_STRING([--with-logdir=PATH],[set file path for source logdir (default: /var/log/${PACKAGE}/${PACKAGE}.log)]),
    [ if test ! -z "$with_logdir" ; then
        case $with_logdir in
            /*)
                SRV_LOGDIR="$with_logdir"
                ;;
            *)
                                AC_MSG_ERROR(You must specify an absolute path to --with-logdir=PATH)
                ;;
        esac
    else
        SRV_LOGDIR="/var/log/${PACKAGE}"
    fi ],
    [
    SRV_LOGDIR="/var/log/${PACKAGE}"
    ])

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

dnl AS_IF([test "x$enable_docker_mode" = "xyes"], [
dnl     SRV_LOGDIR="${prefix}/log"
dnl ])


AC_MSG_NOTICE(srv_logdir set as ${SRV_LOGDIR})

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

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

AC_ARG_WITH(rundir,
    AS_HELP_STRING([--with-rundir=PATH],[set file path for source rundir (default: /var/run/${PACKAGE})]),
    [ if test ! -z "$with_rundir" ; then
        case $with_rundir in
            /*)
                SRV_RUNDIR="$with_rundir"
                ;;
            *)
                                AC_MSG_ERROR(You must specify an absolute path to --with-rundir=PATH)
                ;;
        esac
    else
        SRV_RUNDIR="/var/run/${PACKAGE}"
    fi ],
    [
    SRV_RUNDIR="/var/run/${PACKAGE}"
    ])

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

dnl AS_IF([test "x$enable_docker_mode" = "xyes"], [
dnl     SRV_RUNDIR="${prefix}/run"
dnl ])


AC_MSG_NOTICE(srv_rundir set as ${SRV_RUNDIR})

dnl AC_DEFINE_UNQUOTED(SRV_RUNDIR, "$SRV_RUNDIR", [location of pid file])
dnl AC_DEFINE_UNQUOTED(srv_rundir, "$SRV_RUNDIR", [location of pid file])
AC_SUBST(SRV_RUNDIR, "$SRV_RUNDIR")
AC_SUBST(srv_rundir, "$SRV_RUNDIR")

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

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

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

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

dnl AS_IF([test "x$enable_docker_mode" = "xyes"], [
dnl     SRV_SHAREDIR="${prefix}/share"
dnl ])

AC_MSG_NOTICE(srv_sharedir set as ${SRV_SHAREDIR})

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

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)
                ;;
            *)
                SRV_USER="$with_user"
                ;;
        esac
    else
        SRV_USER="$default_user"
    fi ],
    [ SRV_USER="$default_user" ])

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

AC_MSG_NOTICE(srv_user set as ${SRV_USER})

AC_DEFINE_UNQUOTED(srv_user, "$SRV_USER", [effective user])
AC_SUBST(srv_user, "$SRV_USER")

AC_DEFINE_UNQUOTED(SRV_USER, "$SRV_USER", [effective user])
AC_SUBST(SRV_USER, "$SRV_USER")


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

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

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

AC_MSG_NOTICE(srv_group set as ${SRV_GROUP})

AC_DEFINE_UNQUOTED(srv_group, "$SRV_GROUP", [effective group])
AC_SUBST(srv_group, "$SRV_GROUP")

AC_DEFINE_UNQUOTED(SRV_GROUP, "$SRV_GROUP", [effective group])
AC_SUBST(SRV_GROUP, "$SRV_GROUP")

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

AM_CONDITIONAL(DEV_MODE, false)
AS_IF([test "x$enable_devel_mode" = "xyes"], [
    AM_CONDITIONAL(DEV_MODE, true)
    sbindir="./"
])

dnl SRV_BACKGROUND="true"
dnl AS_IF([test "x$enable_devel_mode" = "xyes"], [
dnl     SRV_BACKGROUND="false"
dnl     INSTALLRC="false"
dnl ])

dnl AS_IF([test "x$enable_docker_mode" = "xyes"], [
dnl     SRV_BACKGROUND="false"
dnl     INSTALLRC="false"
dnl ])

dnl AC_SUBST(srv_background, "$SRV_BACKGROUND")
dnl AC_SUBST(srv_conf, "$PACKAGE.yaml")

AC_SUBST(srv_name, "$PACKAGE")

AC_CONFIG_FILES([
Makefile
rc/Makefile
internal/config/path.go
internal/config/user.go
rc/rc.d/engined
rc/engined.service
front/Makefile
])
AC_OUTPUT
