#!/usr/bin/env bash

# Load the shell functions needed for the rest of this script.

BASH_BASED_CONFIGURE=../scripts/bash-based-configure
REQUIRED_CFLAGS="-D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE" 
RECOMMENDED_CFLAGS="-O2 -Wall"
DEBUG_CFLAGS="-ggdb -Wall"

CFG_USE_CYGWIN="no"   # do not change

if test -e $BASH_BASED_CONFIGURE; then
   source $BASH_BASED_CONFIGURE
else
   echo "Could not find $BASH_BASED_CONFIGURE"
   echo "You're probably not in the right directory."
   exit 1
fi

# Set the package name and version

PACKAGE pngpack 0.11

# Check for some essential tools.

REQUIRE_GMAKE
REQUIRE_GCC

# Find out and verify the basedir of the source installation,
# and where to install the binary.

GET_SRCDIR Makefile.template
GET_PREFIX /usr/local
GET_BINDIR

# Look for required libraries

PRINT_MESSAGE "\nLooking for includes and libraries:"

REQUIRE_INCLUDE bzlib.h bz2
REQUIRE_LIBRARY bz2 BZ2_bzReadOpen bz2

REQUIRE_PNG

# Byte order

echo
CHECK_ENDIAN
WITH_OPTION memdebug no "[no | yes]"

# Do not proceed further if in --help mode

if test -n "$cfg_help_mode"; then
     FINALIZE_HELP
     exit 0
fi

# Fetch the source files and build the CFILES/OFILES list

PRINT_MESSAGE "\nCollecting source files:"

if ! test -e md5.h; then ln -s ../md5.h md5.h; fi
if ! test -e md5.c; then ln -s ../md5.c md5.c; fi

rm -f conftest.c
cfiles=
ofiles=

for cfile in *.c; do
  cfile_prefix=`echo $cfile | sed -e 's/\.c//'`
  cfiles="$cfiles $cfile"
  ofiles="$ofiles $cfile_prefix.o"
  echo -n " $cfile_prefix"
done

echo
echo -e "\nCFG_CFILES = $cfiles" >> Makefile.config
echo "CFG_OFILES = $ofiles" >> Makefile.config

# Okay, hopefully we've got everything together now.

CREATE_MAKEFILES Makefile 

echo
echo "Configuration is complete."
echo "Type 'make show' to verify the settings,"
echo "or enter 'make' to start the build immediately."
echo 

if test -n "$CFLAGS"; then
  echo "* Note that your \$CFLAGS=$CFLAGS"
  echo "* have been included into the configuration."
  echo "* It is recommended not to specify additional \$CFLAGS,"
  echo "* but feel free to continue at your own risk."
  echo
fi

if test -n "$LDFLAGS"; then
  echo "* Note that your \$LDFLAGS=$LDFLAGS"
  echo "* have been included into the configuration."
  echo "* It is recommended not to specify additional \$LDFLAGS,"
  echo "* but feel free to continue at your own risk."
  echo
fi
