# -copyright-
#-# 
#-# Copyright (C) 1999-2001  Robin Hogan, 2021-2026 Willem Vermin
#-# 
#-# 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 2 of the License, 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, write to the Free Software
#-# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#-# 
AC_INIT([xpenguins], [3.2.4], [wvermin@gmail.com])
AC_CONFIG_SRCDIR([src/main.c])
AC_CONFIG_HEADERS([config.h])
AM_INIT_AUTOMAKE
m4_ifdef([AM_SILENT_RULES], [AM_SILENT_RULES([yes])])

AC_SUBST([PACKAGE_VERSION],[VERSION])

# When creating a package (e.g. debian)
# Get date from debian/changelog or something like that: (thanks to Bernhard M. Wiedemann)
# First: check if SOURCE_DATE_EPOCH is already defined
# if not: use the file 'bootdate' which should contain the output of 'date +%s'
s="$SOURCE_DATE_EPOCH"
if test "x$s" = x ; then
   AC_MSG_WARN([SOURCE_DATE_EPOCH is not defined])
   if test -f "$srcdir/bootdate" ; then
      AC_MSG_NOTICE([Try to read SOURCE_DATE_EPOCH from file '$srcdir/bootdate'])
      s=`head -n1 "$srcdir/bootdate"`
      xd=`date -u -d "@$s" 2>/dev/null || date -u -r "$s" 2>/dev/null || echo "Invalid date format"`
      AC_MSG_NOTICE([Read SOURCE_DATE_EPOCH=$s, i.e: $xd ])
   fi
else
   xd=`date -u -d "@$s" 2>/dev/null || date -u -r "$s" 2>/dev/null || echo "Invalid date format"`
   AC_MSG_NOTICE([Given SOURCE_DATE_EPOCH=$s, i.e: $xd ])
fi
# final checks on the validity of SOURCE_DATE_EPOCH
# - must be an positive integer
# - date must be within -20 years and +1 year from current date
# if these are not fulfilled, use `date +%s`
x=$(echo "$s" | tr -dc "[0-9]" )
good=no
now=`date "+%s"`
if test "x$x" != x ; then
   firstdate=$( expr "$now" - 631152000 )
   lastdate=$(  expr "$now" + 31536000  )
   if test "$x" -gt "$firstdate" -a "$x" -lt "$lastdate" ; then
      good=yes
   fi
fi


if test x"$good" = xyes ; then
   SOURCE_DATE_EPOCH="$x"
else
   SOURCE_DATE_EPOCH="$now"
   AC_MSG_WARN([SOURCE_DATE_EPOCH set from current date])
fi
xd=`date -u -d "@$SOURCE_DATE_EPOCH" 2>/dev/null || date -u -r "$SOURCE_DATE_EPOCH" 2>/dev/null || echo "Invalid date format"`
AC_MSG_NOTICE([Using SOURCE_DATE_EPOCH=$SOURCE_DATE_EPOCH, i.e: $xd ])

DATE_FMT="+%Y-%m-%d"
DATE=`date -u -d "@$SOURCE_DATE_EPOCH" "$DATE_FMT" 2>/dev/null || date -u -r "$SOURCE_DATE_EPOCH" "$DATE_FMT" 2>/dev/null || date -u "$DATE_FMT"`
AC_SUBST([DATE])


# Check if we can create a reproducible 'make dist'.
# If yes: replace the original am__tar (Make's recipe to make a tarball) by
#  a recipe that creates a reproducible tarball
tardir=conftestdir
mkdir "$tardir"
echo "foo" > $tardir/test
AC_MSG_NOTICE([Checking the capabilities of the tar program...])
"${TAR-tar}" --format=posix --pax-option=exthdr.name=%d/PaxHeaders/%f,delete=atime,delete=ctime \ 
	  --owner=0 --group=0 --numeric-owner --mtime="@${SOURCE_DATE_EPOCH}" --sort=name --mode="a+rwX" -chf - "$tardir" > /dev/null
if test $? -eq 0 ; then
   AC_MSG_NOTICE([The program tar is usable to create a reproducible tar ball.])
   am__tar='$${TAR-tar} --format=posix --pax-option=exthdr.name=%d/PaxHeaders/%f,delete=atime,delete=ctime \ 
	  --owner=0 --group=0 --numeric-owner --mtime="'"@${SOURCE_DATE_EPOCH}"'" --sort=name --mode="a+rwX" -chf - "$$tardir"'
else
   AC_MSG_WARN([The program tar is not usable to create a reproducible build.])
   AC_MSG_WARN([If a reproducible build is desired, use environment variable 'TAR' to point to a pax-enabled tar,])
   AC_MSG_WARN([or use the flag '--disable-selfrep'.])
fi
rm -r "$tardir"

AC_ARG_ENABLE(selfrep, [AS_HELP_STRING([--enable-selfrep],[Build with self replicating mode @<:@default=yes@:>@])],[],[enable_selfrep=yes])
AM_CONDITIONAL([MAKESELFREP],[test "x$enable_selfrep" = "xyes"])


dnl Checks for programs.
AC_PROG_CC
AC_PROG_INSTALL
AC_PROG_RANLIB

LIBS="-lm"

PKG_PROG_PKG_CONFIG

PKG_CHECK_MODULES(X11, [x11 xpm xext xkbcommon xinerama xtst])
PKG_CHECK_MODULES(GTK, [gtk+-3.0 gmodule-2.0])

LIBS="$X11_LIBS"

# check for availability of double buffering
AC_CHECK_FUNCS([XdbeAllocateBackBufferName])

#check for availability of xinerama
AC_CHECK_FUNCS([XineramaQueryScreens])

LIBS="-lm"

AM_CONDITIONAL([CROSSCOMPILE],[test "x$cross_compiling" = "xyes"])

dnl Checks for typedefs, structures, and compiler characteristics.
#AC_HEADER_TIME

AC_CONFIG_FILES(Makefile src/Makefile themes/Makefile themes/Penguins/Makefile \
	  themes/Big_Penguins/Makefile themes/Classic_Penguins/Makefile \
	  themes/Turtles/Makefile themes/Bill/Makefile xpenguins.spec)

AC_OUTPUT

