#!/bin/sh
# The simplest configure for the vidix
cc_check() {
  echo >> "$TMPLOG"
  cat "$TMPC" >> "$TMPLOG"
  echo >> "$TMPLOG"
  echo "$_cc $_inc_extra $_ld_static $_ld_extra $TMPC -o $TMPO $@" >> "$TMPLOG"
  > "$TMPO"
  ( "$_cc" $_inc_extra $_ld_static $_ld_extra "$TMPC" -o "$TMPO" "$@" ) >> "$TMPLOG" 2>&1
  TMP="$?"
  echo >> "$TMPLOG"
  echo "ldd $TMPO" >> "$TMPLOG"
  ( ldd "$TMPO" ) >> "$TMPLOG" 2>&1
  echo >> "$TMPLOG"
  return "$TMP"
}

# Display error message, flushes tempfile, exit
die() {
  echo
  echo "Error: $@" >&2
  echo >&2
  rm -f "$TMPO" "$TMPC" "$TMPS" "$TMPCPP"
  echo "Check "$TMPLOG" if you do not understand why it failed."
  exit 1
}

# OS test booleans functions
issystem() {
  test "`echo $system_name | tr A-Z a-z`" = "`echo $1 | tr A-Z a-z`"
}
linux()   { issystem "Linux"   ; return "$?" ; }
sunos()   { issystem "SunOS"   ; return "$?" ; }
irix()    { issystem "IRIX"    ; return "$?" ; }
cygwin()  { issystem "CYGWIN"  ; return "$?" ; }
freebsd() { issystem "FreeBSD" ; return "$?" ; }
netbsd()  { issystem "NetBSD"  ; return "$?" ; }
bsdos()   { issystem "BSD/OS"  ; return "$?" ; }
openbsd() { issystem "OpenBSD" ; return "$?" ; }
bsd()     { freebsd || netbsd || bsdos || openbsd ; return "$?" ; }
qnx()     { issystem "QNX"     ; return "$?" ; }

# arch test boolean functions
x86() {
  case "$host_arch" in
    i*86|x86|x86pc) return 0 ;;
    *) return 1 ;;
  esac
}

x86_64() {
  case "$host_arch" in
    x86_64) return 0 ;;
    *) return 1 ;;
  esac
}

# Use this before starting a check
echocheck() {
  echo "============ Checking for $@ ============" >> "$TMPLOG"
  echo ${_echo_n} "Checking for $@ ... ${_echo_c}"
}

# Use this to echo the results of a check
echores() {
  echo "Result is: $@" >> "$TMPLOG"
  echo "##########################################" >> "$TMPLOG"
  echo "" >> "$TMPLOG"
  echo "$@"
}
#############################################################################

# Check how echo works in this /bin/sh
case `echo -n` in
  -n)	_echo_n=	_echo_c='\c'	;;	# SysV echo
  *)	_echo_n='-n '	_echo_c=	;;	# BSD echo
esac


for parm in "$@" ; do
  if test "$parm" = "--help" || test "$parm" = "-help" || test "$parm" = "-h" ; then
    cat << EOF

Usage: $0 [OPTIONS]...

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

Installation directories:
  --prefix=DIR           use this prefix for installing vidix [/usr/local]
  --datadir=DIR          use this prefix for installing machine independent
                         data files [PREFIX/share/vidix]
  --confdir=DIR          use this prefix for installing configuration files
                         [same as datadir]

Use these options if autodetection fails:
  --with-extraincdir=DIR   extra headers in DIR
  --with-extralibdir=DIR   extra library files in DIR

Miscellaneous options:
  --cc=COMPILER          use this C compiler to build VIDIX [gcc]
  --target=PLATFORM      target platform (i386-linux, arm-linux, etc)

EOF
    exit 0
  fi
done # for parm in ...


# LGB: temporary files
for I in "$TMPDIR" "$TEMPDIR" "/tmp" ; do
  test "$I" && break
done

TMPLOG="configure.log"
rm -f "$TMPLOG"
TMPC="$I/vidix-conf-$RANDOM-$$.c"
TMPCPP="$I/vidix-conf-$RANDOM-$$.cpp"
TMPO="$I/vidix-conf-$RANDOM-$$.o"
TMPS="$I/vidix-conf-$RANDOM-$$.S"

_cc=gcc
_prefix="/usr/local"

# checking for OS name
if test -z "$system_name" ; then
  # OS name
  system_name=`( uname -s ) 2>&1`
  case "$system_name" in
  Linux|FreeBSD|NetBSD|BSD/OS|OpenBSD|SunOS|QNX)
    ;;
  IRIX*)
    system_name=IRIX
    ;;
  [cC][yY][gG][wW][iI][nN]*)
    system_name=CYGWIN
    ;;
  *)
    system_name="$system_name-UNKNOWN"
    ;;
  esac
fi

# host's CPU/instruction set
if test -z "$host_arch" ; then
host_arch=`( uname -p ) 2>&1`
case "$host_arch" in
   i386|sparc|ppc|alpha|arm|mips)
     ;;

   *) # uname -p on Linux returns 'unknown' for the processor type,
      # OpenBSD returns 'Intel Pentium/MMX ("Genuine Intel" 586-class)'

      # Maybe uname -m (machine hardware name) returns something we
      # recognize.

      case "`( uname -m ) 2>&1`" in
      i*86|x86|x86pc) host_arch=i386 ;;
      x86_64|amd64)
        if [ "`$_cc -dumpmachine | grep x86_64 | cut -d- -f1`" = "x86_64" -a \
             -z "`echo $CFLAGS | grep -- -m32`"  ]; then
          host_arch=x86_64
        else
          host_arch=i386
        fi
      ;;
      ppc) host_arch=ppc ;;
      alpha) host_arch=alpha ;;
      sparc*) host_arch=sparc ;;
      arm*) host_arch=arm ;;
      *) host_arch=UNKNOWN ;;
    esac
    ;;
esac
fi

# Printout architecture
case "$host_arch" in
  i*86|x86|x86pc)
  _def_arch="#define ARCH_X86 1"
  _target_arch="TARGET_ARCH_X86 = yes"
  _def_words_endian="#undef WORDS_BIGENDIAN"
    ;;

  x86_64)
  _def_arch="#define ARCH_X86_64 1"
  _target_arch="TARGET_ARCH_X86_64 = yes"
  _def_words_endian="#undef WORDS_BIGENDIAN"
    ;;

  sparc)
    _def_arch='#define ARCH_SPARC 1'
    _target_arch='TARGET_ARCH_SPARC = yes'
    _def_words_endian='#define WORDS_BIGENDIAN 1'
    ;;

  arm)
    _def_arch="#define ARCH_ARM 1"
    _target_arch='TARGET_ARCH_ARM = yes'
    _def_words_endian='#undef WORDS_BIGENDIAN'
    ;;

  ppc)
    _def_arch='#define ARCH_PPC 1'
    _target_arch='TARGET_ARCH_PPC = yes'
    _def_words_endian='#define WORDS_BIGENDIAN 1'
    ;;

  alpha)
    _def_arch='#define ARCH_ALPHA 1'
    _target_arch='TARGET_ARCH_ALPHA = yes'
    _def_words_endian='#undef WORDS_BIGENDIAN'
    ;;

  mips)
    _def_arch="#define ARCH_SGI_MIPS 1"
    _target_arch="TARGET_ARCH_SGI_MIPS = yes"
    _def_words_endian='#define WORDS_BIGENDIAN 1'
    ;;

  *)
    echo "The architecture of your CPU ($host_arch) is not supported by this configure script"
    echo "It seems noone has ported VIDIX to your OS or CPU type yet."
    echo "unsupported architecture: $host_arch"
    _def_arch="#define ARCH_GENERIC 1"
    _target_arch="TARGET_ARCH_GENERIC = yes"
    _def_words_endian="#undef WORDS_BIGENDIAN"
    ;;
esac

# return result to parent script
for ac_option do
  case "$ac_option" in
  --prefix=*)
    _prefix=`echo $ac_option | cut -d '=' -f 2`
    ;;
  --datadir=*)
    _datadir=`echo $ac_option | cut -d '=' -f 2`
    ;;
  --confdir=*)
    _confdir=`echo $ac_option | cut -d '=' -f 2`
    ;;
  --with-extraincdir=*)
    _inc_extra=-I`echo $ac_option | cut -d '=' -f 2 | sed 's,:, -I,g'`
    ;;
  --with-extralibdir=*)
    _ld_extra=-L`echo $ac_option | cut -d '=' -f 2 | sed 's,:, -L,g'`
    ;;
  --target=*)
    _target=`echo $ac_option | cut -d '=' -f 2`
    ;;
  --cc=*)
    _cc=`echo $ac_option | cut -d '=' -f 2`
    ;;
  *)
    ;;
  esac
done

# Determine our OS name and CPU architecture
if test -n "$_target" ; then
  system_name=`echo $_target | cut -d '-' -f 2`
  host_arch=`echo $_target | cut -d '-' -f 1`
fi

echo "Detected operating system: $system_name"
echo "Detected host architecture: $host_arch"

# Atmos: moved this here, to be correct, if --prefix is specified
case "$system_name" in
  CYGWIN|MINGW32)
# Keep everything in .exe folder
test -z "$_datadir" && _datadir="."
test -z "$_confdir" && _confdir="."
test -z "$_libdir" && _libdir="."
    ;;
  *)
test -z "$_datadir" && _datadir="$_prefix/share/vidix"
test -z "$_confdir" && _confdir="$_datadir"
test -z "$_libdir" && _libdir="$_prefix/lib/vidix"
    ;;
  esac

# Checking for CFLAGS
if test -z "$CFLAGS" ; then
    CFLAGS="-O2"
fi

# checking for a working awk, I'm using mawk first, because it's fastest - atmos
_awk=
_awk_verc_fail=yes
echo ${_echo_n} "Checking for "awk" ... ${_echo_c}"
for _awk in mawk gawk nawk awk; do
  if ( $_awk 'BEGIN{testme();}function testme(){print"";}' ) >> "$TMPLOG" 2>&1; then
    _awk_verc_fail=no
    break
  fi
done
test "$_awk_verc_fail" = yes && _awk=no
echo "$_awk"
if test "$_awk_verc_fail" = yes; then
echo "VIDIX needs Awk, but there was no working implementation found!"
echo "Try the GNU implementation, which can be downloaded from:"
die  "ftp://ftp.gnu.org/gnu/gawk/"
fi

# Checking for mman
echocheck "mman.h"
cat > $TMPC << EOF
#include <sys/types.h>
#include <sys/mman.h>
int main(void) { (void) mmap(0, 0, 0, 0, 0, 0); return 0; }
EOF
_mman=no
cc_check && _mman=yes
if test "$_mman" = yes ; then
  _def_mman='#define HAVE_SYS_MMAN_H 1'
else
  _def_mman='#undef HAVE_SYS_MMAN_H'
fi
echores "$_mman"

# Checking for malloc.h
echocheck "malloc.h"
cat > $TMPC << EOF
#include <malloc.h>
int main(void) { (void) malloc(0); return 0; }
EOF
_malloc=no
cc_check && _malloc=yes
if test "$_malloc" = yes ; then
  _def_malloc='#define HAVE_MALLOC_H 1'
else
  _def_malloc='#undef HAVE_MALLOC_H'
fi
echores "$_malloc"

# Checking for memalign
echocheck "memalign"
cat > $TMPC << EOF
#include <malloc.h>
int main (void) { (void) memalign(64, sizeof(char)); return 0; }
EOF
_memalign=no
cc_check && _memalign=yes
if test "$_memalign" = yes ; then
  _def_memalign='#define HAVE_MEMALIGN 1'
else
  _def_memalign='#undef HAVE_MEMALIGN'
fi
echores "$_memalign"

# Checking for mlock
echocheck "mlock"
cat > $TMPC << EOF
#include <sys/mman.h>
int main (void) { return mlock(0,0); }
EOF
_mlock=no
cc_check && _mlock=yes
if test "$_mlock" = yes ; then
  _def_mlock='#define HAVE_MLOCK 1'
else
  _def_mlock='#undef HAVE_MLOCK'
fi
echores "$_mlock"

echocheck "dynamic loader"
cat > $TMPC << EOF
#include <dlfcn.h>
int main(void) { dlopen(0, 0); dlclose(0); dlsym(0, 0); return 0; }
EOF
_dl=no
_ld_dl=''
if cc_check ; then
  _dl=yes
elif cc_check -ldl ; then
  _dl=yes
  _ld_dl='-ldl'
fi
if test "$_dl" = yes ; then
 _def_dl='#define HAVE_LIBDL 1'
else
 die "dynamic loader was not found"
fi
echores "$_dl (using '$_ld_dl')"

#############################################################################
echo "Creating config.mak"
cat > config.mak << EOF
# -------- Generated by configure -----------

prefix = $_prefix
DATADIR = $_datadir
LIBDIR = $_libdir
CONFDIR = $_confdir
OPTFLAGS = $CFLAGS
AWK = $_awk
CC=$_cc

$_target_arch
TARGET_OS=$system_name
LIBDL=$_ld_dl
EOF

_lc_build_os=`echo $system_name | tr [:lower:] [:upper:]`

#############################################################################
echo "Creating config.h"
cat > config.h << EOF
/* -------- This file has been automatically generated by configure --------- */
$_def_arch
#define TARGET_$_lc_build_os 1

/* Common data directory (for fonts, etc) */
#define DATADIR "$_datadir"
#define CONFDIR "$_confdir"
#define LIBDIR "$_libdir"

/* Define this if your system has the "sys/mman.h" header file */
$_def_mman

/* Define this if your system has the "malloc.h" header file */
$_def_malloc

/* memalign is mapped to malloc if unsupported */
$_def_memalign
#ifndef HAVE_MEMALIGN
# define memalign(a,b) malloc(b)
#endif

/* Define this if your system has the mlock() in "sys/mman.h" header file */
$_def_mlock

/* Define if your processor stores words with the most significant
   byte first (like Motorola and SPARC, unlike Intel and VAX).  */
$_def_words_endian

/* Define this if you have the elf dynamic linker -ldl library */
$_def_dl
EOF

# Last move:
rm -f "$TMPO" "$TMPC" "$TMPS" "$TMPCPP"
