## Autoconf macros for writing Varnish modules
## Copyright (C) 2016,2017 Sergey Poznyakoff
##
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 3, or (at your option)
## any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program.  If not, see <http://www.gnu.org/licenses/>.

## serial 2

## VAPI_CHECK_VER([MAJOR], [MINOR], [PATCH]) - Check version
##
## Arguments are literal numbers.  All of them are optional, but at least
## MAJOR must be present, otherwise the macro will produce empty expansion.
## The macro expands to shell code that compares the supplied numbers with
## the Varnish API version stored in environment variables VARNISHAPI_MAJOR,
## VARNISHAPI_MINOR, and VARNISHAPI_PATCH.  Depending on the comparison,
## the code sets variable varnishapi_version_diff to one of: "older", "newer",
## or "same".
##
m4_define([VAPI_CHECK_VER],[
  m4_if([$1],,,[if test $VARNISHAPI_MAJOR -lt $1; then
      varnishapi_version_diff=older
  elif test $VARNISHAPI_MAJOR -gt $1; then
      varnishapi_version_diff=newer
  m4_if([$2],,,[elif test $VARNISHAPI_MINOR -lt $2; then
      varnishapi_version_diff=older
  elif test $VARNISHAPI_MINOR -gt $2; then
      varnishapi_version_diff=newer
  ])m4_if([$3],,,[elif test $VARNISHAPI_PATCH -lt $3; then
      varnishapi_version_diff=older
  elif test $VARNISHAPI_PATCH -gt $3; then
      varnishapi_version_diff=newer
  ])else
      varnishapi_version_diff=same
  fi
  if test $varnishapi_version_diff = older; then
    AC_MSG_ERROR([unsupported varnishapi version $VARNISHAPI_MAJOR.$VARNISHAPI_MINOR.$VARNISHAPI_PATCH])
  fi
])])

## AM_VARNISHAPI([VERSION])
## Tests if the programs and libraries needed for compiling a varnish
## module are present. If VERSION argument is supplied, checks if varnish API
## version is the same or newer than that.  Otherwise, if package version
## string ends in -N.N.N, where N stands for a decimal digit, checks if varnish
## API version is at least N.N.N. If API is older, emits error message and
## aborts. If it is newer, emits a warning message at the end of configure.
##
## Sets the following configuration variables:
##
##   VMODDIR         the path of the varnish module directory.
##   VARNISH_MAJOR, VARNISH_MINOR, and VARNISH_PATCH
##                   the corresponding numbers of the varnish API version.
##   VARNISHD        full pathname of the varnishd binary
##   VARNISHTEST     full pathname of the varnishtest binary
##   VARNISHAPI_PKGDATADIR
##                   varnish API package data directory
##   VARNISHAPI_VMODTOOL
##                   full pathname of the vmodtool.py script 
##
AC_DEFUN([AM_VARNISHAPI],
[ # Check for pkg-config
  PKG_PROG_PKG_CONFIG

  # Check for python
  AC_CHECK_PROGS(PYTHON, [python], [AC_MSG_ERROR([python is not found.])])

  # Varnish source tree (deprecated)
  AC_ARG_WITH([varnish-source],
     [],
     [AC_MSG_ERROR([the --with-varnish-source option has been withdrawn])])

  # pkg-config
  PKG_PROG_PKG_CONFIG
  PKG_CHECK_MODULES([VARNISHAPI], [varnishapi])

  varnishapi_version() {
    VARNISHAPI_MAJOR=$[]1
    VARNISHAPI_MINOR=$[]2
    VARNISHAPI_PATCH=$[]3
  }

  v=$($PKG_CONFIG --modversion varnishapi)

  if test -n "$v"; then
    save_IFS=$IFS
    IFS='.'
    varnishapi_version $v
    IFS=$save_IFS
    VAPI_CHECK_VER(m4_if([$1],,
     [m4_pushdef([ver],[m4_bpatsubst(AC_PACKAGE_VERSION,[.*-\([0-9]\)\.\([0-9]\)\.\([0-9]\)$],[m4_quote(\1,\2,\3)])])dnl
m4_if(ver,AC_PACKAGE_VERSION,,[m4_unquote(ver)])dnl
m4_popdef([ver])],
     [m4_unquote(m4_split([$1],\.))]))
  else
    AC_MSG_ERROR([unknown varnishapi version])
  fi
  
  # vmod installation dir
  AC_ARG_VAR([VMODDIR],  [vmod installation directory])
  AC_ARG_WITH([vmoddir],
    AC_HELP_STRING([--with-vmoddir=DIR],
                   [install modules to DIR]),
    [case "$withval" in
     /*)   VMODDIR=$withval;;
     no)   unset VMODDIR;;
     *)    AC_MSG_ERROR([argument to --with-vmoddir must be absolute pathname])
     esac],[VMODDIR=$($PKG_CONFIG --variable=vmoddir varnishapi)
     if test -z "$VMODDIR"; then
       AC_MSG_FAILURE([cannot determine vmod installation directory])
     fi])

  if test -z "$VMODDIR"; then
    VMODDIR='$(libdir)/varnish/mods'
  fi

  AC_ARG_VAR([VARNISHD],[full pathname of the varnishd binary])
  AC_PATH_PROG([VARNISHD],
               [varnishd],[\$(abs_top_srcdir)/build-aux/missing varnishd],
               [$($PKG_CONFIG --variable=sbindir varnishapi):$PATH])
  AC_ARG_VAR([VARNISHTEST],[full pathname of the varnishtest binary])
  AC_PATH_PROG([VARNISHTEST],
               [varnishtest],[\$(abs_top_srcdir)/build-aux/missing varnishtest],
               [$($PKG_CONFIG --variable=bindir varnishapi):$PATH])

  AC_ARG_VAR([VARNISHAPI_PKGDATADIR],[full pathname of the varnish lib directory])
  VARNISHAPI_PKGDATADIR=$($PKG_CONFIG --variable=pkgdatadir varnishapi)
  AC_ARG_VAR([VARNISHAPI_VMODTOOL],[full pathname of the vmodtool.py script])
  VARNISHAPI_VMODTOOL='$(VARNISHAPI_PKGDATADIR)/vmodtool.py'
  
  AC_CONFIG_COMMANDS([status],[
delim="---------------------------------------------------------------------------"
echo ""
echo $delim
echo "Building for Varnish version $version"
if test "$varnishapi_version_diff" = newer; then
  fmt <<EOT
WARNING: This version is newer than the latest version for which
$PACKAGE_STRING was tested.  If it doesn't compile, please send a mail to
<$PACKAGE_BUGREPORT>.
EOT
fi
echo $delim
echo ""
],
[version="$VARNISHAPI_MAJOR.$VARNISHAPI_MINOR"
if test -n "$VARNISHAPI_PATCH"; then
  version="\$version.$VARNISHAPI_PATCH"
fi
varnishapi_version_diff=$varnishapi_version_diff
PACKAGE_STRING="$PACKAGE_STRING"
PACKAGE_BUGREPORT=$PACKAGE_BUGREPORT
])])

## varnishapi.m4 ends
