#!/bin/sh

# File: configure
#
# Simlpe configure script for develop coLinux and cross platform, checks
# build environment (gcc and tools) and create bin/user-build.cfg.
#
# - Henry Nestler <henry@bigfoot.de>
#
# More arguments and directory options:
# ./configure --help

# Some defaults from config file (Kernel version and dir, binutil version)
. bin/build-common.sh --get-vars

# Some default values
target=$TARGET
nonopt=NONE
prefix=NONE
distdir="\$USER_TOPDIR/dist"
downloaddir=NONE
builddir="\$USER_TOPDIR/build"
logfile="\$USER_TOPDIR/log/build-colinux-\$\$.log"
errfile="\$USER_TOPDIR/log/build-colinux-\$\$.err"
targetkernelsource="\$BUILD_DIR/linux-\$KERNEL_VERSION-source"
targetkernelbuild="\$BUILD_DIR/linux-\$KERNEL_VERSION-build"
targetkerneldir=NONE
targetmoduledir="\$COLINUX_TARGET_KERNEL_BUILD/_install"
hostkerneldir=NONE
colinux_os=NONE
colinux_arch=NONE
configfile="bin/user-build.cfg"
no_create=""
verbose=no
kernel_untar=no
gcc_guest_target=i686-co-linux
gcc_guest_build=no
enable_wx=no

# Try the directory containing this script, then its working dir.
prog=$0
confdir=`echo $prog|sed 's%/[^/][^/]*$%%'`
test "x$confdir" = "x$prog" && confdir=.

# Go into source dir and give path absolute
srcdir=`( cd $confdir ; pwd )`

# Go into upper of source dir and give path absolute
topdir=`( cd $srcdir/.. ; pwd )`

prev=
for option
do

  # If the previous option needs an argument, assign it.
  if test -n "$prev"; then
    eval "$prev=\$option"
    prev=
    continue
  fi

  case "$option" in
  -*=*) optarg=`echo "$option" | sed 's/[-_a-zA-Z0-9]*=//'` ;;
  *) optarg= ;;
  esac

  case "$option" in

  help | -help | --help | --hel | --he)
    # Omit some internal or obsolete options to make the list less imposing.
    # This message is too long to be a string in the A/UX 3.1 sh.
    cat << EOF

Usage: configure [options]
Options: [defaults in brackets after descriptions]
Configuration:
  --help                  print this message

Host type:
  --target=TARGET         configure for TARGET [$target]
  --colinux-os=OS         target platform string OS=linux|winnt [winnt]

Directories:
  --srcdir=DIR            directory of this script
                          [$srcdir]
  --topdir=TOPDIR         base directory for all other dirs
                          [$topdir]
  --downloaddir=DIR       source download
                          [\$TOPDIR/download]
  --builddir=BUILD_DIR    extract and compile tools
                          [\$TOPDIR/build]
  --prefix=PREFIX         install architecture-independent files in PREFIX
                          [\$TOPDIR/mingw32]
  --distdir=DIR           distribution of all winnt files
                          [\$TOPDIR/dist]
  --targetkernelsource=DIR  path to patched colinux guest kernel source tree
                          [$targetkernelsource]
  --targetkernelbuild=DIR output directory for building colinux guest kernel
                          [$targetkernelbuild]
  --targetkerneldir=DIR   path to patched colinux guest kernel tree, source
                          and builds in same directory  (obsolate, see above)
  --targetmoduledir=DIR   Modules output for target kernel (Guest)
                          [$targetmoduledir]
  --hostkerneldir=DIR     path to your host kernel (only for linux as host)
                          [/usr/src/linux]

Options:
  --configfile=FILE       outputfile for batch build [$configfile]
  --logfile=ARG           logfile for build process, open append.
                          use \$\$ for process id and separate files.
                          [\$TOPDIR/log/build-colinux-\$\$.log]
  --errfile=ARG           error file for build process, open append.
                          [\$TOPDIR/log/build-colinux-\$\$.err]
  --kernel-untar          Untar can overwrite kernel tree, if exist.  Is good
                          for clean builds or automation builds.
  --disable-kernel-untar  Never overwrite kernel sources. (default)
  --dry-run               Test only configure steps, don't write config.
  --verbose               More messages in configure.
  --gcc-guest-build       Build C compiler from source of gnu cross compiler,
                          and use for kernel compiling. Should use, if gcc version
                          (major + minor) is different from your distribution.
  --enable-wx             Build WxWidgets lib and console-wx (default disabled).
-------------------------------------------------------------------------------
Sample for WinNT as host (default):
  ./configure --target=i686-pc-mingw32 \\
	--downloaddir=$DOWNLOADS \\
	--prefix=$PREFIX

Sample for WinNT 64 bit as host:
  ./configure --target=x86_64-w64-mingw32 \\
	--downloaddir=$DOWNLOADS \\
	--prefix=$PREFIX

Sample for Linux as host:
  ./configure --target=i686-pc-linux \\
	--downloaddir=$DOWNLOADS \\
	--targetkerneldir=$BUILD_DIR/linux-$KERNEL_VERSION \\
	--hostkerneldir=/usr/src/linux-`uname -r`

Sample for WinNT and Linux as host in same config:
  ./configure \\
	--downloaddir=$DOWNLOADS \\
	--prefix=$PREFIX \\
	--targetkerneldir=$BUILD_DIR/linux-$KERNEL_VERSION \\
	--hostkerneldir=/usr/src/linux-`uname -r`

EOF

    exit 0 ;;

  --topdir)
    prev=topdir ;;
  --topdir=*)
    topdir="$optarg" ;;

  --srcdir)
    prev=srcdir ;;
  --srcdir=*)
    srcdir="$optarg" ;;

  --target)
    prev=target ;;
  --target=*)
    target="$optarg" ;;

  --prefix)
    prev=prefix ;;
  --prefix=*)
    prefix="$optarg" ;;

  --downloaddir)
    prev=downloaddir ;;
  --downloaddir=*)
    downloaddir="$optarg" ;;

  --builddir)
    prev=builddir ;;
  --builddir=*)
    builddir="$optarg" ;;

  --distdir)
    prev=distdir ;;
  --distdir=*)
    distdir="$optarg" ;;

  --targetkernelsource)
    prev=targetkernelsource ;;
  --targetkernelsource=*)
    targetkernelsource="$optarg" ;;

  --targetkernelbuild)
    prev=targetkernelbuild ;;
  --targetkernelbuild=*)
    targetkernelbuild="$optarg" ;;

  --targetkerneldir)
    prev=targetkerneldir ;;
  --targetkerneldir=*)
    targetkerneldir="$optarg" ;;

  --targetmoduledir)
    prev=targetmoduledir ;;
  --targetmoduledir=*)
    targetmoduledir="$optarg" ;;

  --hostkerneldir)
    prev=hostkerneldir ;;
  --hostkerneldir=*)
    hostkerneldir="$optarg" ;;

  --colinux-os)
    prev=colinux-os ;;
  --colinux-os=*)
    colinux_os="$optarg" ;;

  --colinux-arch)
    prev=colinux-arch ;;
  --colinux-arch=*)
    colinux_arch="$optarg" ;;

  --configfile)
    prev=configfile ;;
  --configfile=*)
    configfile="$optarg" ;;

  --logfile)
    prev=logfile ;;
  --logfile=*)
    logfile="$optarg" ;;

  --errfile)
    prev=errfile ;;
  --errfile=*)
    errfile="$optarg" ;;

  --no-create|--dry-run)
    no_create=yes ;;

  --no-kernel-untar | \
  --disable-kernel-untar)
    kernel_untar=no ;;

  --kernel-untar | \
  --enable-kernel-untar)
    kernel_untar=yes ;;

  --gcc-guest*)
    gcc_guest_build=yes ;;

  --enable-wx)
    enable_wx=yes ;;

  --disable-wx)
    enable_wx=no ;;

  -v | -verbose | --verbose)
    verbose=yes ;;

   -*) { echo "configure: error: $option: invalid option; use --help to show usage" 1>&2; exit 1; }
    ;;

  *)
    if test -n "`echo $option| sed 's/[-a-z0-9.]//g'`"; then
      echo "configure: warning: $option: invalid host type" 1>&2
    fi
    if test "x$nonopt" != xNONE; then
      { echo "configure: error: can only configure for one host and one target at a time" 1>&2; exit 1; }
    fi
    nonopt="$option"
    ;;

  esac
done

if test -n "$prev"; then
  { echo "configure: error: missing argument to --`echo $prev | sed 's/_/-/g'`" 1>&2; exit 1; }
fi


#
# Check for target system
#

target_cpu=`echo $target | sed 's/^\([^-]*\)-\([^-]*\)-\(.*\)$/\1/'`
target_vendor=`echo $target | sed 's/^\([^-]*\)-\([^-]*\)-\(.*\)$/\2/'`
target_os=`echo $target | sed 's/^\([^-]*\)-\([^-]*\)-\(.*\)$/\3/'`

if [ "$verbose" = "yes" ]; then
  echo " confdir=$confdir"
  echo " srcdir=$srcdir"
  echo " topdir=$topdir"

  echo " TOPDIR=$TOPDIR"
  echo " BINDIR=$BINDIR"

  echo " target_cpu=$target_cpu"
  echo " target_vendor=$target_vendor"
  echo " target_os=$target_os"

  echo " colinux version=$CO_VERSION"
  echo " target kernel_dir=$KERNEL_DIR"
  echo " target kernel_version=$KERNEL_VERSION"
fi

#
# Check building for linux or winnt
# Mapping real OS into coLinux OS-Name
#
case $colinux_os in
NONE)
  case $target_os in
  linux|Linux) colinux_os="linux" ;;
  darwin*|Darwin) colinux_os="darwin" ;;
  *) colinux_os="winnt" ;;
  esac ;;
esac
echo "Setting colinux_os=$colinux_os"

#
# Check defaulting arch
#
case $colinux_arch in
NONE)
  case $target_cpu in
  x86_64) colinux_arch=$target_cpu ;;
  *) colinux_arch="i386" ;;
  esac ;;
esac
echo "Setting colinux_arch=$colinux_arch"

# Default prefix directory
case $prefix in
NONE)
  case $target_os in
  NONE) prefix="$topdir/prefix-unknown" ;;
  *) prefix="$topdir/$target_os" ;;
  esac
  echo "Setting prefix=$prefix"
  ;;
esac

# Default download directory
case $downloaddir in
NONE)
  downloaddir="$topdir/download"
  echo "Setting downloaddir=$downloaddir"
  ;;
esac

# Splitted target kernel directories?
case $targetkerneldir in
NONE)
  targetkerneldir_cfg="# COLINUX_TARGET_KERNEL_PATH=\"\$BUILD_DIR/linux-\$KERNEL_VERSION\""
  ;;
*)
  targetkerneldir_cfg="COLINUX_TARGET_KERNEL_PATH=\"$targetkerneldir\""
  targetkernelsource="\$COLINUX_TARGET_KERNEL_PATH"
  targetkernelbuild="\$COLINUX_TARGET_KERNEL_PATH"
  ;;
esac

#
# Check host system
#

if [ "$target_os" = "linux" ] ; then
 UNAME_RELEASE=`(uname -r) 2>/dev/null` || UNAME_RELEASE=unknown
 echo " UNAME_RELEASE=$UNAME_RELEASE"

 # Search Linux-Host kernel sourcs:
 # 1. /lib/modules/$UNAME_RELEASE/build
 # 2. /lib/modules/$UNAME_RELEASE/source
 # 3. /usr/src/linux
 # 4. /usr/src/linux-$UNAME_RELEASE

 if [ "$hostkerneldir" = "NONE" ] ; then
  if [ -d /lib/modules/$UNAME_RELEASE/build ] ; then
    hostkerneldir="/lib/modules/$UNAME_RELEASE/build"
  elif [ -d /lib/modules/$UNAME_RELEASE/source ] ; then
    hostkerneldir="/lib/modules/$UNAME_RELEASE/source"
  elif [ -d /usr/src/linux ] ; then
    hostkerneldir="/usr/src/linux"
  elif [ -d /usr/src/linux-$UNAME_RELEASE ] ; then
    hostkerneldir="/usr/src/linux-$UNAME_RELEASE"
  else
    { echo "configure: error: source directory for host kernel missing, use --hostkerneldir" 1>&2; exit 1; }
  fi
  echo "Setting hostkerneldir=$hostkerneldir"
 fi
fi

if [ "$hostkerneldir" != "NONE" ] ; then
  if [ ! -f $hostkerneldir/Makefile ] ; then
    { echo "configure: error: missing configured host kernel in $hostkerneldir" 1>&2; exit 1; }
  fi

  HOST_KERNELRELEASE=`cd $hostkerneldir; make kernelrelease 2>/dev/null | tail -n1`
  if [ -n "$HOST_KERNELRELEASE" ]; then
    set -- `echo "$HOST_KERNELRELEASE" | sed -e 's/\([0-9]*\)\.\([0-9]*\)\.\([0-9]*\)\(.*\)$/\1 \2 \3 \4/'`
    HOST_VERSION="$1"
    HOST_PATCHLEVEL="$2"
    HOST_SUBLEVEL="$3"
    HOST_EXTRAVERSION="$4"
  else
    HOST_VERSION=`grep "^VERSION = " $hostkerneldir/Makefile | sed -e 's/\([^0-9]*\)\(.*\)$/\2/'`
    HOST_PATCHLEVEL=`grep "^PATCHLEVEL = " $hostkerneldir/Makefile | sed -e 's/\([^0-9]*\)\(.*\)$/\2/'`
    HOST_SUBLEVEL=`grep "^SUBLEVEL = " $hostkerneldir/Makefile | sed -e 's/\([^0-9]*\)\(.*\)$/\2/'`
    HOST_EXTRAVERSION=`grep "^EXTRAVERSION = " $hostkerneldir/Makefile | sed -e 's/\([^0-9\.]*\)\(.*\)$/\2/'`
    HOST_KERNELRELEASE="$HOST_VERSION.$HOST_PATCHLEVEL.$HOST_SUBLEVEL$HOST_EXTRAVERSION"
  fi
  echo " HOST_VERSION=$HOST_VERSION"
  echo " HOST_PATCHLEVEL=$HOST_PATCHLEVEL"
  echo " HOST_SUBLEVEL=$HOST_SUBLEVEL"
  test -n "$HOST_EXTRAVERSION" && echo " HOST_EXTRAVERSION=$HOST_EXTRAVERSION"

  # Hostkernel must be >= 2.6 (compile external module)
  if [ "$HOST_VERSION" != "2" -o "$HOST_PATCHLEVEL" -lt 6 ] ; then
    { echo "configure: error: host kernel $HOST_VERSION.$HOST_PATCHLEVEL.$HOST_SUBLEVEL is to old! Need 2.6.x" 1>&2; exit 1; }
  fi

  if [ "$HOST_KERNELRELEASE" != "`uname -r`" ] ; then
    echo -e "\n WARNING! Running kernel is not same as host kernel - You can not run colinux on this machine!\n"
  fi
fi

# Remove path from md5 files
W32LIBS_CHECKSUM=`basename $W32LIBS_CHECKSUM`
KERNEL_CHECKSUM=`basename $KERNEL_CHECKSUM`

# $1 Name of component
# $2 yes: add to warnings | no: add to failed
add_failed()
{
    if [ "$2" != "yes" ]; then
	test -z "$failed" && failed="$1" || failed="$failed, $1"
    else
	test -z "$warnings" && warnings="$1" || warnings="$warnings, $1"
    fi
}


# $1 Name of component
# $2 Call for check
# $3 Minimum version need, or empty string
# $4 Maximum version supported, or empty string
# '--warn' optional as first param, add to warning list
# Example: "wget-1.8" "wget --version" "1.8.x"
check_exec_tool()
{
  local v1="" v2="" v3="" i1="" i2="" i3="" warn=""

  if [ "$1" = "--warn" ]; then
    warn="yes"
    shift
  fi

  echo -n "Checking $1 ... "
  if ($2) </dev/null >/dev/null 2>&1; then
    if [ -n "$3" ]; then
      # get needed version, transform the 'x' into 0
      eval `echo $3 | sed -e \
        's/\([0-9]*\)\.\([0-9x]*\)\.\([0-9x]*\)/v1=\1 v2=\2 v3=\3/' -e 'y/x/0/'`

      # get installed version
      eval `($2) 2>&1 | sed -n -e \
        's/^[^0-9]*\([0-9]\{1,\}\)\.\([0-9]\{1,\}\)\.\([0-9]\{1,\}\).*/i1=\1 i2=\2 i3=\3/p'`

      if [ "$v3" = "0" -a -z "$i1" ]; then
        # get installed version with two numbers
        eval `($2) 2>&1 | sed -n -e \
          's/^.*\([0-9]\{1,\}\)\.\([0-9]\{1,\}\).*/i1=\1 i2=\2/p'`
        i3="0"
      fi

      if [ -z "$i1" -o -z "$v1" -o -z "$i2" -o -z "$v2" -o -z "$i3" -o -z "$v3" ]; then
        echo "Version not dectect"
        add_failed "$1" $warn
        return 1
      fi

      echo -n "($i1.$i2.$i3) "

      if [ $i1 -lt $v1 -o \
           $i1 -eq $v1 -a $i2 -lt $v2 -o \
           $i1 -eq $v1 -a $i2 -eq $v2 -a $i3 -lt $v3 ]; then
        echo "wrong version, need $3"
        add_failed "$1" $warn
        return 1
      fi

      if [ -n "$4" ]; then
        local m1="" m2="" m3=""
        # get needed version, transform the 'x' into 999
        eval `echo $4 | sed -e \
            's/\([0-9]*\)\.\([0-9x]*\)\.\([0-9x]*\)/m1=\1 m2=\2 m3=\3/'`
        test "$m2" = "x" && m2="999"
        test "$m3" = "x" && m3="999"
        if [ $i1 -gt $m1 -o \
             $i1 -eq $m1 -a $i2 -gt $m2 -o \
             $i1 -eq $m1 -a $i2 -eq $m2 -a $i3 -gt $m3 ]; then
          echo "version not supported, maximum $4"
          add_failed "$1" $warn
          return 1
        fi
      fi
    fi

    echo "found"
  else
    echo "missing (no exec)"
    add_failed "$1" $warn
    return 1
  fi
  return 0
}


as_helper()
{
	if [ "$gcc_guest_build" = "yes" ]; then
		echo "  Build guest '${gcc_guest_target_prefix}as' and binutils for kernel later"
		return
	fi
	exit 1
}


gcc_helper()
{
	if [ "$gcc_guest_build" = "yes" ]; then
		echo "  Build guest '${gcc_guest_target_prefix}gcc' for kernel later"
		return
	fi

	# ABI from local GCC
	touch /tmp/colinux-$$.c
	LOCAL_ABI=`gcc -E -dM /tmp/colinux-$$.c 2>/dev/null | sed -n -e \
          's/^#define __GXX_ABI_VERSION \([0-9]*\)$/\1/p'`
	rm /tmp/colinux-$$.c

	# ABI from known mingw compilers
	case $GCC_VERSION in
	    4.3.*|4.2.*|4.1.*|4.0.*|3.4.*)
		MINGW_ABI="1002"
		;;
	    3.3.*)
		MINGW_ABI="102"
		;;
#	    3.2.*)
#		MINGW_ABI="101"
#		;;
	    *)	MINGW_ABI="unknown"
		;;
	esac

	if [ x$LOCAL_ABI = x$MINGW_ABI ]; then
		echo "  ABI seems be compatible. Use local gcc `gcc -dumpversion` for kernel, $GCC_VERSION for daemons."
		return
	fi

	cat <<EOF

	Cross gcc and local gcc should be the same major and minor version.

	Please use option  "--gcc-guest-build"  to enable building of guest gcc
	from sources, if building cross tools later. (Need for kernel building)

	--- OR ---
	
	# download source for gcc $GCC_NEEDED, configure and build self:
	cd $srcdir/bin
	./build-cross.sh --download-only
	cd /tmp
	tar xzf $downloaddir/gcc-core-$GCC_RELEASE-src.tar.gz
	tar xzf $downloaddir/gcc-g++-$GCC_RELEASE-src.tar.gz
	cd gcc-$GCC_RELEASE
	./configure --prefix=\$HOME/$gcc_guest_target
	make && make install
	cd / ; rm -rf /tmp/gcc-$GCC_RELEASE

	# Than run configure and make again with binary path to this directory:
	export PATH="\$HOME/$gcc_guest_target/bin:\$PATH"
	cd $srcdir
	./configure && make
EOF
	exit 1
}


check_depmod()
{
  echo -n "Checking depmod ... "

  # Locate depmod in user dirs and check for module-init-tools version
  if depmod_cfg=`PATH="$prefix/$target/bin:/sbin:/usr/sbin:/usr/local/sbin:$PATH" \
		 which depmod 2>/dev/null`; then

    # Depmod 2.4.x is bugy with "-V" only, "--help" does nothing real.
    installed=`$depmod_cfg -V --help 2>/dev/null | grep -E 'module-init-tools|kmod version'`
    if [ -z "$installed" ]; then
      cat << EOF
($depmod_cfg) wrong version.

	Warning: you may need to install module-init-tools
	See http://www.codemonkey.org.uk/docs/post-halloween-2.6.txt

	Install the file 'depmod', in directory
	$prefix/$target/bin,
	to use only for cross buildings.

EOF
      add_failed "$1"
    else
      echo "($installed) found"
    fi
  else
    echo "missing (no exec)"
    add_failed "$1"
  fi
}

#
# Check for tools
#
warning=""
failed=""

# Get version from script, replace the last digit
GCC_NEEDED=`echo "$GCC_VERSION" | sed -e 's/\([0-9]*\)\.\([0-9]*\).*$/\1.\2/'`.x

# Build specific path
if [ "$gcc_guest_build" = "no" ]; then
  gcc_guest_build_comment="# "
  gcc_guest_target_prefix=""
else
  gcc_guest_build_comment=""
  gcc_guest_target_prefix=${gcc_guest_target}-
  PATH=$PATH:$prefix/$gcc_guest_target/bin
fi

# General tools for winnt and linux
check_exec_tool "wget" "wget --version" ""
check_exec_tool "Python interpreter" "python -V" "2.3.x" ""
check_exec_tool "patch" "patch --version" ""
check_exec_tool "make" "make --version" ""
check_exec_tool "bunzip2" "bunzip2 --help" ""

case $colinux_os in
  linux) # for Linux as host
    check_exec_tool "gcc for kernel" "${gcc_guest_target_prefix}gcc -dumpversion" "$GCC_NEEDED"
    check_exec_tool "gcc g++" "${gcc_guest_target_prefix}g++ --version" "$GCC_NEEDED"
    check_exec_tool "FLTK devel (patched)" "fltk-config --version" "$FLTK_VERSION" && \
    if nm `fltk-config --libs` | grep -q "fl_x_global_event_hook"
    then
        if [ "$verbose" = "yes" ]; then
            echo "  `fltk-config --libs`: Label fl_x_global_event_hook found"
        fi
    else
        echo "  `fltk-config --libs`: label fl_x_global_event_hook not found"
        echo "  Please set right path to a patched FLTK"
    fi
  ;;
  darwin) # for darwin as host
    GCC_NEEDED=""
    # maybe we should check here for the XCode version, too
    # ToDo: fail on PPC arch!
    check_exec_tool "gcc for kernel" "${gcc_guest_target_prefix}gcc -dumpversion" "$GCC_NEEDED"
    check_exec_tool "gcc g++" "${gcc_guest_target_prefix}g++ --version" "$GCC_NEEDED"
#    check_exec_tool "FLTK devel (patched)" "fltk-config --version" "$FLTK_VERSION" && \
#    if nm `fltk-config --libs` | grep -q "fl_x_global_event_hook"
#    then
#        if [ "$verbose" = "yes" ]; then
#            echo "  `fltk-config --libs`: Label fl_x_global_event_hook found"
#        fi
#    else
#        echo "  `fltk-config --libs`: label fl_x_global_event_hook not found"
#        echo "  Please set right path to a patched FLTK"
#    fi
  ;;
  *) # for Winnt as host
    # Check GCC for kernel
    check_exec_tool --warn "${gcc_guest_target_prefix}gcc for kernel" "${gcc_guest_target_prefix}gcc -dumpversion" \
       "$GCC_NEEDED" "$GCC_NEEDED" || gcc_helper
    check_exec_tool --warn "${gcc_guest_target_prefix}as binutils for kernel" "${gcc_guest_target_prefix}as --version" \
       "$BINUTILS_REQUIREMENTS" || as_helper

    # Locate users depmod and check for module-init-tools
    check_depmod "depmod"
    check_exec_tool "unzip" "unzip -h"
    check_exec_tool "zip" "zip -h"
    # bison-1.75-109   need by binutils (yacc 91.7.30 have a error YYEMPTY)
    check_exec_tool "bison" "bison --version"
    # need by binutils ...
    check_exec_tool "texinfo" "texindex --version"
    check_exec_tool "flex" "flex --version"
    check_exec_tool "gettext" "gettext --version"
  ;;
esac

#
# Stop, if some tools missing or wrong version
#
if [ -n "$warnings" ]
then
    echo " warnings: $warnings"
fi

if [ -n "$failed" ]
then
    echo " failed: $failed"
    exit 1
fi

#
# do nothing, if trace option enabled
#
test "$no_create" = yes && { echo "nothing created!" 1>&2; exit 1; }

# Backup old config file
test -f $configfile -a ! -f $configfile.old && mv $configfile $configfile.old

#
# Create config-file
# Needed for winnt in scripts, such bin/build-colinux.sh
#

echo "Create $configfile"
    cat > $configfile << EOF
# File: $configfile
# Configfile for coLinux $CO_VERSION, kernel $KERNEL_VERSION
# created by configure at `date`

# Setup target OS (winnt or linux)
#
COLINUX_HOST_OS="$colinux_os"

# Setup target architecture (i386 ot x86_64)
#
COLINUX_HOST_ARCH="$colinux_arch"

# Directory where we start for all subdirs in build processing
# default: \$HOME/colinux-devel  (use only in this file)
#
USER_TOPDIR="$topdir"

# Directory where we store download sources or where find source already stored
# default: \$USER_TOPDIR/download
#
DOWNLOADS="$downloaddir"

# In this directoy the cross-tools will be extract and compile (GCC and Co.)
# default: \$USER_TOPDIR/build
#
BUILD_DIR="$builddir"

# Directory for compiler binary (build by cross)
# default: \$USER_TOPDIR/mingw32
# (/usr/local/mingw32 not supported by this scripts, need root level)
#
PREFIX="$prefix"

# Overwrite kernelversion.  Check series files for available versions.
# default: empty
#
# KERNEL_VERSION=$KERNEL_VERSION

# Path to guest kernel, need for colinux daemons build processing (obsolate)
# default: \$BUILD_DIR/linux-\$KERNEL_VERSION
#
$targetkerneldir_cfg

# Directory target kernel source path for unpacking source (Guest kernel)
# default: \$BUILD_DIR/linux-\$KERNEL_VERSION-source
#
COLINUX_TARGET_KERNEL_SOURCE="$targetkernelsource"

# Directory target kernel for building (Guest kernel)
# default: \$BUILD_DIR/linux-\$KERNEL_VERSION-build
#
COLINUX_TARGET_KERNEL_BUILD="$targetkernelbuild"

# Output directory for target modules. Used for INSTALL_MOD_PATH in kernel build
# default: \$COLINUX_TARGET_KERNEL_BUILD/_install
#
COLINUX_TARGET_MODULE_PATH="$targetmoduledir"

# HOST-Kernel (only, if host system is linux)
# default: /path/to/your/host/kernel/source
#
COLINUX_HOST_KERNEL_DIR="$hostkerneldir"

# Install directory for target binaries (daemons, kernel and modules)
# default: \$USER_TOPDIR/dist
#
COLINUX_INSTALL_DIR="$distdir"

# Logfile of building (append)
#
COLINUX_BUILD_LOG="$logfile"
COLINUX_BUILD_ERR="$errfile"

# Overwrite kerneltree before rebuild? yes/no
# yes: Build script will overwrite kerneltree and config.
# no:  Leave kernel source untouched. User friendly for patches and config (default).
#
COLINUX_KERNEL_UNTAR="$kernel_untar"

# Strip the kernel image
# yes: Smalest size for package dist.
# no:  Leave all debug symbols in image. Good choice for error tracing with gdb.
#
COLINUX_KERNEL_STRIP="no"

# Path to depmod, installed from module-init-tools
# default: /sbin/depmod
#
COLINUX_DEPMOD=$depmod_cfg

# "COLINUX_GCC_GUEST_TARGET" is running colinux guest system.
# This compiler will use for building colinux kernel as CROSS_COMPILE.
# Enable GUEST specific compiler, if your compiler from distribution
# is not compatible with current cross gcc.
# default: empty
# optional: $gcc_guest_target
#
${gcc_guest_build_comment}COLINUX_GCC_GUEST_TARGET="$gcc_guest_target"
${gcc_guest_build_comment}COLINUX_GCC_GUEST_PATH="\$PREFIX/\$COLINUX_GCC_GUEST_TARGET/bin"

# Library WxWidgets is disabled by default.
# To build colinux-console-wx.exe with this lib, set COLINUX_ENABLE_WX="yes".
COLINUX_ENABLE_WX="$enable_wx"
EOF

    cat <<EOF
Configure DONE

Ready for "make download && make && make package && make installer"
Run "make help" for more instructions, and read doc/building.

EOF
