#!/bin/sh
# :: ascii-jump project
# :: $Id: configure,v 1.2 2003/02/23 12:19:36 loth Exp $
#
# :: copyright (c) michal moskal, malekith@pld-linux.org
#    copyright (c) grzegorz moskal, g.moskal@opengroup.org

rm -f config.log
usage ()
{
  cat <<EOF
Usage: $0 [OPTIONS]

To assign environment variables (CFLAGS or LDFLAGS), specify them as
VAR=VALUE.  See below for descriptions of some of the useful variables.

Defaults for the options are specified in brackets.

Configuration:
  -h, --help              display this help and exit
  --prefix=PREFIX         install architecture-independent files in PREFIX [$prefix]
  --exec-prefix=EPREFIX   install architecture-dependent files in EPREFIX [$exec_prefix]

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

For better control, use the options below.

Fine tuning of the installation directories:
  --bindir=DIR           user executables [EPREFIX/bin]
  --datadir=DIR          read-only architecture-independent data [PREFIX/share]
  --libdir=DIR           object code libraries [EPREFIX/lib]
  --includedir=DIR       C header files [PREFIX/include]

EOF
  exit 1
}

log ()
{
  echo "$*" >> config.log
}

checking ()
{
  log "test $*"
  echo -n "checking $*... "
}

yes ()
{
  log "test ok"
  echo "yes"
}

no ()
{
  log "test failed"
  echo "no"
  return 1
}

warn ()
{
  log "warn: $*"
  echo 1>&2 "*** warning: $*"
}

error ()
{
  log "error: $*"
  echo 1>&2 "*** error: $*"
}

die ()
{
  error "sorry, cannot continue"
  exit 1
}

check_lib ()
{
  checking "for $1() in $2"
  prog="int main(){ $1(); return 0; }"
  cmd="$CC $2 conftest.c"
  echo "$prog" > conftest.c
  log "conftest:"
  log "$prog"
  log "running $cmd:"
  if $cmd >> config.log 2>&1 ; then
    rm -f conftest.c a.out
    yes
  else
    rm -f conftest.c a.out
    no
  fi
}

check_cmd ()
{
  checking "$1"
  log "running $2:"
  if eval $2 >> config.log 2> /dev/null ; then
    yes
  else
    no
  fi
}

check_header ()
{
  checking "for $2($3) in $1 header file"
  
  prog="#include <$1>\nint main(){ $2($3); return 0; }"
  cmd="$CC -Wall conftest.c"
  echo "$prog" > conftest.c

  log "conftest:"
  log "$prog"
  log "running $cmd:"
  if $cmd >> config.log 2>&1 ; then
    rm -f conftest.c a.out
    yes
  else
    rm -f conftest.c a.out
    no
  fi
} 

create_makefile ()
{
  echo "creating Makefile (from Makefile.input)"
  DATE=`date`
  echo "#:: Generated automatically from Makefile.input by ./configure" > Makefile
  echo "#:: $DATE :: " >> Makefile
  echo "prefix=${prefix}" >> Makefile
  echo "exec_prefix=${exec_prefix}" >> Makefile
  echo "bindir=${bindir}" >> Makefile
  echo "datadir=${datadir}" >> Makefile
  echo "libdir=${libdir}" >> Makefile
  echo "includedir=${includedir}" >> Makefile
  echo "MC_CFLAGS=${CFLAGS}" >> Makefile
  echo "MC_LDFLAGS=${LDFLAGS}" >> Makefile
  echo "" >> Makefile
  cat Makefile.input >> Makefile
}

CC=gcc
prefix=/usr
datadir=${prefix}/share
include=${prefix}/usr/include
includedir=${prefix}/include/tsys
exec_prefix=/usr
bindir=${exec_prefix}/bin
libdir=${exec_prefix}/lib

while test -n "$1" ; do
  string="$1"
  value=`echo $string | sed -e 's/[^=]*=//'`
  case "$string" in
    --help)
       usage
       exit 0
       ;;
       
    --prefix=*)
      prefix="$value"
      datadir=${prefix}/share
      includedir=${prefix}/include/
      include=${prefix}/include/
      ;;
      
    --libdir=*) 
      libdir="$value"
      ;;

    --exec-prefix=*)
       exec_prefix="$value"
       bindir=${exec_prefix}/bin
       libdir=${exec_prefix}/lib
      ;;
      
    --bindir=*)
      bindir="$value"
      ;;
      
    --datadir=*)
      datadir="$value"
      ;;
      
    --includedir=*)
      includedir="$value"/
      ;;
    
    CFLAGS=*)
      CFLAGS="$value"
      ;;
      
    LDFLAGS=*)
      LDFLAGS="$value"
      ;;

    CC=*)
      CC="$value"
      ;;
      
    [A-Z]*)
      log "unsupported varible: $string"
      ;;
    --*)
      log "unsupported command line option: $string"
      ;;
      
    *)
    esac
    shift
done

check_cmd "gcc" "gcc --version" || die
#check_lib "getch" "-lcurses" || die
check_header "string.h" "strlen" "\"foo\"" || die
#check_header "unistd.h" "getpid" || die
#check_header "sys/ioctl.h" "ioctl"  "0, 0" || die
create_makefile


echo "done"
