#!/bin/sh

#--------------------------------------------------------------------------#

usage() {
  echo "usage:  configure [--prefix=<dir>][--debug]"
  echo ""
  echo "  --prefix=<dir>   specify installation prefix"
  echo "  --debug          enable internal debugging of library"
  echo
  cat <<EOF
The environment variable 'CC' may be used to specify the preferred C
compiler.  For example if you use a Bourne shell you could say
  
  CC=cc ./configure

to overwrite the default C compiler search list, which is 'gcc cc'.
Similarly the environment variable CXX can be used to speficy a list of C++
compilers, for which we build special object files.  Here the default search
list is 'g++ gcc CC'.
EOF
}

#--------------------------------------------------------------------------#
# setup default values
#
fmt="%-26s ..."
debug=no
prefix=/usr/local
os=unsupported
libc="libc.so"

#--------------------------------------------------------------------------#
# process command line options
#
while [ $# -gt 0 ]
do
  case $1 in
    -h | --help | -?)
      usage
      exit 0
      ;;
    --prefix=*)
      prefix=`expr $1 : '--prefix=\(.*\)'`
      ;;
    --debug)
      debug=yes
      ;;
    *)
      echo "*** configure: unknown command line option (try '-h')" 1>&2
      exit 1
      ;;
  esac
  shift
done

#--------------------------------------------------------------------------#
# get version
#
printf "$fmt" "version"
version=`cat VERSION`
echo " $version"

#--------------------------------------------------------------------------#
# check if OS is supported
#

printf "$fmt" "system"

case `uname` in
  SunOS )
    case `uname -r` in
      5.* )
        os=solaris
        ;;
    esac
    ;;
  Linux )
    os=linux
    ;;
esac

if [ $os = unsupported ]
then
  echo 
  echo "*** configure: unsupported operating system" 1>&2
  exit 1
fi

echo " $os"

#--------------------------------------------------------------------------#
# search for C compilers

printf "$fmt" "C compiler"

if [ "$CC" ]; then searchlist="$CC"; else searchlist="gcc cc"; fi
CC=""

compilers=""
for cc in $searchlist
do
  for dir in `echo $PATH | sed -e 's,:, ,g'`
  do
    if [ -f $dir/$cc ]
    then
      [ "$CC" ] || CC="$cc"
      [ "$compilers" ] && compilers="$compilers "
      compilers="${compilers}$cc"
      break
    fi
  done
done

if [  "$CC" ]
then
  case $CC in
    gcc) CFLAGS="-g -Wall";;
    cc) CFLAGS="-g -W";;
    *) CFLAGS="-g";;
  esac
  echo " $CC $CFLAGS"
else
  echo
  echo "*** configure: no C compiler found" 1>&2
  exit 1
fi

#--------------------------------------------------------------------------#
# search for C++ compilers

printf "$fmt" "C++ compilers"

if [ "$CXX" ]; then searchlist="$CXX"; else searchlist="g++ gcc CC"; fi

COMPILERS=""
for cc in $searchlist
do
  for dir in `echo $PATH | sed -e 's,:, ,g'`
  do
    if [ -f $dir/$cc ]
    then
      [ "$COMPILERS" ] && COMPILERS="$COMPILERS "
      COMPILERS="${COMPILERS}$cc"
      break
    fi
  done
done

if [ "$COMPILERS" ]
then
  echo " $COMPILERS"
else
  echo
  echo "*** configure: no C++ compiler found" 1>&2
  exit 1
fi

for cc in $COMPILERS
do
  [ $cc = CC ] && \
    echo "*** warning: CC as C++ compiler does not work properly yet" 1>&2
done

#--------------------------------------------------------------------------#
# check if installation directory exists
#
printf "$fmt" "prefix"

if [ -d $prefix ]
then
  echo " $prefix"
else
  echo
  echo "*** configure: $prefix is not a directory" 1>&2
  exit 1
fi

#--------------------------------------------------------------------------#
# searching for real libc

printf "$fmt" "libc"

canfopen() {
  tmp=/tmp/configure-ccmalloc-$$
  dir=`pwd`
  mkdir /tmp/configure-ccmalloc-$$ || exit 1
  cd $tmp
  rm -f main.c
cat <<EOF >main.c
#include <stdio.h>
#include <dlfcn.h>
#include <stdlib.h>
int main(int arc, char **argv)
{
  int bl;
  const char * str;
  void * handle = dlopen(argv[1], RTLD_NOW);
  if((str = dlerror()) != NULL) fprintf(stderr, "*** %s\n", str);
  exit(str != NULL);
}
EOF
  if $CC main.c -ldl 1>/dev/null 2>/dev/null
  then
    if ./a.out $1 1>/dev/null 2>/dev/null; then libc=$1; fi
  fi
  cd $dir
  rm -rf $tmp
}

libc=notfound
for name in libc.so \
  /lib/libc.so /usr/lib/libc.so /lib/libc.so.* /usr/lib/libc.so.*
do
  canfopen $name
  [ $libc = notfound ] || break
done

if [ $libc = notfound ]
then
  echo
  echo "*** configure: could not find 'libc.so'" 1>&2
  exit 1
fi

echo " $libc"

#--------------------------------------------------------------------------#

printf "$fmt" "setting up directories"
for d in obj lib bin
do
  if [ ! -d $d ]; then mkdir $d; fi
done
echo " done"

#--------------------------------------------------------------------------#
#
printf "$fmt" "generating src/config.h"

rm -f src/config.h
(
	echo "#ifndef _config_h_INCLUDED"
	[ $debug = no ] && echo "#define NDEBUG"
	[ $os = solaris ] && echo "#define OS_IS_SOLARIS"
	[ $os = linux ] && echo "#define OS_IS_LINUX"
	cat<<-EOF
		#define VERSION "$version"
		#define NAME_OF_LIBC "$libc"
		#if defined(OS_IS_LINUX) || defined(OS_IS_SOLARIS)
		#define CAN_GET_ARGV0
		#endif
		#endif /* _config_h_INCLUDED */
	EOF
) > src/config.h
echo " done"

#--------------------------------------------------------------------------#
#
printf "$fmt" "generation Makefile"

rm -f Makefile
sed \
-e "s,@PREFIX@,$prefix,g" \
-e "s,@CC@,$CC,g" \
-e "s,@CFLAGS@,$CFLAGS,g" \
-e "s,@COMPILERS@,$COMPILERS,g" \
Makefile.in > Makefile
echo " done"

#--------------------------------------------------------------------------#
#
printf "$fmt" "generating ccmalloc script"
rm -f bin/ccmalloc
sed \
-e "s,@PREFIX@,$prefix,g" \
-e "s,@COMPILERS@,\"$COMPILERS\",g" \
src/ccmalloc.in > bin/ccmalloc
chmod 755 bin/ccmalloc
echo " done"
