#!/bin/sh
#########################################################################
#
# mairix - message index builder and finder for maildir folders.
#
# Copyright (C) Richard P. Curnow  2003,2004,2005
# Copyright (C) Paramjit Oberoi 2005
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of version 2 of the GNU General Public License as
# published by the Free Software Foundation.
# 
# 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.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
# =======================================================================

if [ -f config.log ]; then rm -f config.log ; fi
exec 5>config.log

MYCC=${CC:-gcc}
MYCFLAGS=${CFLAGS:--O2 -Wall}
MYCPPFLAGS=${CPPFLAGS:-}
MYLDFLAGS=${LDFLAGS:-}

# =======================================================================
# Functions

#{{{ cleanup
cleanup () {
  if [ -f docheck.c ]; then rm -f docheck.c ; fi
  if [ -f docheck.o ]; then rm -f docheck.o ; fi
  if [ -f docheck   ]; then rm -f docheck   ; fi
  rm -rf docheck.c docheck.o docheck
}
#}}}

#{{{ test_cc : basic compiler sanity check
test_cc () {
  printf "Testing whether your compiler \"$MYCC $MYCFLAGS\" works : "
  cat >docheck.c <<EOF;
#include <stdio.h>
int main (int argc, char **argv)
{
  return 0;
}
EOF
  ${MYCC} ${MYCFLAGS} -o docheck docheck.c 1>&5 2>&5
  if [ $? -eq 0 ]
  then
    printf "it works\n"
  else
    printf "it doesn't work\n"
    printf "Failed program was\n" 1>&5
    cat docheck.c 1>&5
    rm -f docheck.c docheck
    exit 1
  fi
  cleanup
}
#}}}

#{{{ test_for_stdint_h
test_for_stdint_h () {
  cat >docheck.c <<EOF;
#include <stdint.h>
int main(int argc, char **argv) {
  return 0;
}
EOF

  ${MYCC} ${MYCFLAGS} -c -o docheck.o docheck.c >/dev/null 2>&1
  if [ $? -eq 0 ]
  then
    result=0
  else
    result=1
  fi

  rm -f docheck.c docheck.o
  echo $result
}
#}}}
#{{{ test_for_inttypes_h
test_for_inttypes_h () {
  cat >docheck.c <<EOF;
#include <inttypes.h>
int main(int argc, char **argv) {
  return 0;
}
EOF

  ${MYCC} ${MYCFLAGS} -c -o docheck.o docheck.c >/dev/null 2>&1
  if [ $? -eq 0 ]
  then
    result=0
  else
    result=1
  fi

  rm -f docheck.c docheck.o
  echo $result
}
#}}}
#{{{ test_for_zlib
test_for_zlib () {
  cat > docheck.c <<EOF;
#include <zlib.h>
int main () {
  const char *foo;
  foo = zlibVersion();
  return 0;
}
EOF
  echo "Test program is" 1>&5
  cat docheck.c 1>&5
  ${MYCC} ${MYCPPFLAGS} ${MYCFLAGS} ${MYLDFLAGS} -o docheck docheck.c -lz 1>&5 2>&1
  if [ $? -eq 0 ]
  then
    result=0
  else
    result=1
  fi
  rm -f docheck.c docheck
  echo $result
}
#}}}
#{{{ test_for_bzlib
test_for_bzlib () {
  cat > docheck.c <<EOF;
#include <bzlib.h>
int main () {
  const char *foo;
  foo = BZ2_bzlibVersion();
  return 0;
}
EOF
  echo "Test program is" 1>&5
  cat docheck.c 1>&5
  ${MYCC} ${MYCPPFLAGS} ${MYCFLAGS} ${MYLDFLAGS} -o docheck docheck.c -lbz2 1>&5 2>&1
  if [ $? -eq 0 ]
  then
    result=0
  else
    result=1
  fi
  rm -f docheck.c docheck
  echo $result
}
#}}}
#{{{ usage
usage () {
  cat <<EOF;
\`configure' configures tdl to adapt to many kinds of systems.

Usage: ./configure [OPTION]...

Defaults for the options are specified in brackets.

Configuration:
  -h, --help              display this help and exit

Installation directories:
  --prefix=PREFIX         install architecture-independent files in PREFIX
                          [/usr/local]

By default, \`make install' will install all the files in
\`/usr/local/bin', \`/usr/local/lib' etc.  You can specify
an installation prefix other than \`/usr/local' using \`--prefix',
for instance \`--prefix=$HOME'.

Fine tuning of the installation directories:
  --bindir=DIR           user executables [EPREFIX/bin]
  --infodir=DIR          info documentation [PREFIX/info]
  --mandir=DIR           man documentation [PREFIX/man]
  --docdir=DIR           other documentation [PREFIX/doc/mairix-\$version]

Other options:
  --enable-gzip-mbox     attempt to support gzipped mboxes (requires zlib)
  --disable-gzip-mbox    don't attempt to support gzipped mboxes
  --enable-bzip-mbox     attempt to support bzip2ed mboxes (requires bzlib)
  --disable-bzip-mbox    don't attempt to support bzip2ed mboxes

Some influential environment variables:
  CC          C compiler command
  CFLAGS      C compiler flags
  CPPFLAGS    Extra C preprocessor flags, e.g. -I<include dir> if you
              have header files in a nonstandard directory <include dir>
  LDFLAGS     linker flags, e.g. -L<lib dir> if you have libraries in a
              nonstandard directory <lib dir>

Use these variables to override the choices made by \`configure' or to help
it to find libraries and programs with nonstandard names/locations.

Report bugs to <rc@rc0.org.uk>.
EOF
}
#}}}
# =======================================================================

# Defaults for variables
PREFIX=/usr/local

use_readline=yes
bad_options=no
use_gzip_mbox=yes
use_bzip_mbox=yes

# Parse options to configure
for option
do
	case "$option" in

  --prefix=* | --install-prefix=* )
    PREFIX=`echo $option | sed -e 's/[^=]*=//;'`
    ;;
  --bindir=* )
    BINDIR=`echo $option | sed -e 's/[^=]*=//;'`
    ;;
  --mandir=* )
    MANDIR=`echo $option | sed -e 's/[^=]*=//;'`
    ;;
  --infodir=* )
    INFODIR=`echo $option | sed -e 's/[^=]*=//;'`
    ;;
  --docdir=* )
    DOCDIR=`echo $option | sed -e 's/[^=]*=//;'`
    ;;
  --enable-gzip-mbox )
    use_gzip_mbox=yes
    ;;
  --disable-gzip-mbox )
    use_gzip_mbox=no
    ;;
  --enable-bzip-mbox )
    use_bzip_mbox=yes
    ;;
  --disable-bzip-mbox )
    use_bzip_mbox=no
    ;;
  -h | --help )
    usage
    exit 1
    ;;
  * )
    printf "Unrecognized option : $option\n"
    bad_options=yes
    ;;
  esac
done

if [ ${bad_options} = yes ]; then
  exit 1
fi

DEFS=""
test_cc

printf "Checking for <stdint.h> : "
if [ `test_for_stdint_h` -eq 0 ]; then
  printf "Yes\n"
  DEFS="${DEFS} -DHAS_STDINT_H"
else
  printf "No\n"
fi

printf "Checking for <inttypes.h> : "
if [ `test_for_inttypes_h` -eq 0 ]; then
  printf "Yes\n"
  DEFS="${DEFS} -DHAS_INTTYPES_H"
else
  printf "No\n"
fi

if [ $use_gzip_mbox = "yes" ]; then
  printf "Checking for zlib : "
  if [ `test_for_zlib` -eq 0 ]; then
    printf "Yes\n";
    DEFS="${DEFS} -DUSE_GZIP_MBOX"
    LIBS="-lz"
  else
    printf "No (disabled gzipped mbox support)\n";
  fi
fi

if [ $use_bzip_mbox = "yes" ]; then
  printf "Checking for bzlib : "
  if [ `test_for_bzlib` -eq 0 ]; then
    printf "Yes\n";
    DEFS="${DEFS} -DUSE_BZIP_MBOX"
    LIBS="${LIBS} -lbz2"
  else
    printf "No (disabled bzip2ed mbox support)\n";
  fi
fi

#{{{ Determine version number of the program.
if [ -f version.txt ]; then
	revision=`cat version.txt`
else
	revision="DEVELOPMENT"
fi

#}}}
if [ "x" = "x${BINDIR}" ]; then BINDIR=${PREFIX}/bin ; fi
if [ "x" = "x${MANDIR}" ]; then MANDIR=${PREFIX}/man ; fi
if [ "x" = "x${INFODIR}" ]; then INFODIR=${PREFIX}/info ; fi
if [ "x" = "x${DOCDIR}" ]; then DOCDIR=${PREFIX}/doc/mairix-${revision} ; fi

echo "Generating Makefile"

rm -f Makefile
sed -e "s%@cc@%${MYCC}%; \
        s%@defs@%${DEFS}%; \
        s%@cflags@%${MYCFLAGS}%; \
        s%@prefix@%${PREFIX}%; \
        s%@bindir@%${BINDIR}%; \
        s%@mandir@%${MANDIR}%; \
        s%@infodir@%${INFODIR}%; \
        s%@docdir@%${DOCDIR}%; \
        s%@LIBS@%${LIBS}%; \
        s%@CPPFLAGS@%${MYCPPFLAGS}%; \
        s%@LDFLAGS@%${MYLDFLAGS}%; \
       " < Makefile.in > Makefile

# Avoid editing Makefile instead of Makefile.in
chmod ugo-w Makefile

# =======================================================================
# vim:et:sw=2:ht=2:sts=2:fdm=marker:cms=#%s

