#!/bin/sh
# Note: we expect a POSIX shell
# Configure the Mingw32/CPD cross platform development system

here=`pwd`
pgmname=$0

fatal () {
    echo "$pgmname: FATAL:" $* >&2
    exit 1;
}

chdir () {
    cd $1 || fatal "cannot cd to $1"
}


echo_n_init=no
echo_n () {
  if test "$echo_n_init" = "no"; then
    if (echo "testing\c"; echo 1,2,3) | grep c >/dev/null; then
      if (echo -n testing; echo 1,2,3) | sed s/-n/xn/ | grep xn >/dev/null; then
	echo_n_n=
	echo_n_c='
'
      else
	echo_n_n='-n'
	echo_n_c=
      fi
    else
      echo_n_n=
      echo_n_c='\c'
    fi
    echo_n_init=yes
  fi
  echo $echo_n_n "${1}$echo_n_c"
}

#==========================
# read defaults
#==========================
if [ ! -r config.in ]; then
    echo "config.in does not exist!"
    exit 1
fi
. config.in
if [ -r config.cache ] ; then
    . config.cache 2>/dev/null
fi


CONFIG=config.new
CONFIG_MK=config.mk
rm -f $CONFIG $CONFIG_MK config.okay

cat << EOF > $CONFIG
#
# Automatically generated by ./Configure --- do not edit!
#
EOF

cat << EOF > $CONFIG_MK
#
# Automatically generated by ./Configure --- do not edit!
#
EOF

#==========================
# Some functions
#==========================

function write_mk () {
    value=$(eval echo "\$$1")
    echo "$1=$value" >> $CONFIG_MK
}

function prompt () {
    export $2
    default=$(eval echo "\$$2")
    echo_n "$1 [$default]: "
    read answer
    [ -z "$answer" ] && answer="$default"
    eval "$2"=\"$answer\"
    echo "$2"=\"$answer\" >> $CONFIG
}

function prompt_bool () {
    default="$2"
    [ "$default" = "" ] && default=n
    answer=""
    while [ "$answer" != n -a "$answer" != y ] ; do
	echo_n "$1 (y/n) [$default]: "
	read answer
	[ -z "$answer" ] && answer="$default"
    done
}

function make_abs () {
    a_path=$(eval echo "\$$1")
    if cd $a_path 2>/dev/null ; then
	a_path=`/bin/pwd`
	eval "$1"=\"$a_path\"
	chdir $here;
    fi
}

#===========================================================
# Start
#===========================================================

echo ""
echo "Mingw32/CPD Configuration Script"
echo ""

prompt "Where to install" PREFIX
make_abs PREFIX
if [ ! -d "$PREFIX" ]; then
    fatal "Directory $PREFIX does not exists"
fi

prompt "Where is the gcc source" GCC_SRC_PATH
make_abs GCC_SRC_PATH
gcc_version_c_path="$GCC_SRC_PATH/version.c"
[ -f $gcc_version_c_path ] || gcc_version_c_path="$GCC_SRC_PATH/gcc/version.c"
if [ ! -f "$gcc_version_c_path" ]; then
    echo "gcc is not installed in $GCC_SRC_PATH"
    exit 1
fi
gcc_version=$(sed -e 's/.* "\([0-9.]*\).*/\1/' "$gcc_version_c_path")
case "$gcc_version" in
    2.7.2.1)
	;;
    2.95.[12])
	;;
    *)
	echo "Sorry, this is gcc version $gcc_version"
	echo "       which is not supported"
	exit 1;
	;;
esac
GCCVER=${gcc_version}wk1

prompt "Where is the binutils source" BINUTILS_SRC_PATH
make_abs BINUTILS_SRC_PATH
if [ -f "$BINUTILS_SRC_PATH/bfd/VERSION" ]; then
    bfd_version=$(cat "$BINUTILS_SRC_PATH/bfd/VERSION")
elif [ -f "$BINUTILS_SRC_PATH/bfd/configure.in" ]; then
    bfd_version=$(sed -n '/AM_INIT_AUTOMAKE/ s/.*, *\([0-9.]*\).*/\1/p' \
		    < "$BINUTILS_SRC_PATH/bfd/configure.in")
else
    echo "binutils are not installed in $BINUTILS_SRC_PATH"
    exit 1
fi


case "$bfd_version" in
    2.8.1)
	;;
    2.9.1)
	;;
    *)
	echo "Sorry, these are binutils version $bfd_version"
	echo "       but we need version 2.8.1 or 2.9.1"
	exit 1;
	;;
esac

prompt "Where is the windows32api source" WINDOWS32API_SRC_PATH
make_abs WINDOWS32API_SRC_PATH
if [ ! -f "$WINDOWS32API_SRC_PATH/Version" ]; then
    echo "windows32api source is not installed in $WINDOWS32API_SRC_PATH"
    exit 1
fi
cat >mk.$$.tmp <<EOF
include \$(X)
.PHONY: all
all:
	echo \$(WINDOWS32_API_VERSION)
EOF
api_version=$(make -rsf mk.$$.tmp X=$WINDOWS32API_SRC_PATH/Version)
rm mk.$$.tmp
if [ "$api_version" != "0.1.1" ]; then
    echo "Sorry, this is the windows32api version $api_version"
    echo "       but we need version 0.1.1"
    exit 1;
fi


#------------------------------------
# Write some constants
#------------------------------------

write_mk GCCVER
write_mk CPDVER
write_mk TARGET
write_mk PREFIX
write_mk GCC_SRC_PATH
write_mk BINUTILS_SRC_PATH
write_mk WINDOWS32API_SRC_PATH

echo "GCC=${PREFIX}/${TARGET}/bin/gcc" >> $CONFIG_MK
echo "LD=${PREFIX}/${TARGET}/bin/ld" >> $CONFIG_MK
echo "AS=${PREFIX}/${TARGET}/bin/as" >> $CONFIG_MK
echo "AR=${PREFIX}/${TARGET}/bin/ar" >> $CONFIG_MK
echo "NM=${PREFIX}/${TARGET}/bin/nm" >> $CONFIG_MK
echo "RANLIB=${PREFIX}/${TARGET}/bin/ranlib" >> $CONFIG_MK
echo "DLLTOOL=${PREFIX}/${TARGET}/bin/dlltool" >> $CONFIG_MK
echo "INSTALL=install" >> $CONFIG_MK

echo "" >> $CONFIG
echo "" >> $CONFIG_MK
mv $CONFIG config.cache


#----------------------------------
# apply patches to the GNU packages
#----------------------------------

if [ -f $here/patches/binutils-${bfd_version}.diff ]; then
   if [ -f "$BINUTILS_SRC_PATH/PATCHED-FOR-MINGW32-CPD" ]; then
       echo "Fine, binutils are already patched"
   else
       prompt_bool "Next step is to patch binutils. Okay" y
       if [ "$answer" = "n" ]; then
	   echo "No? So you can't continue"
	   exit 1
       fi
       echo "Patching $BINUTILS_SRC_PATH"
       chdir $BINUTILS_SRC_PATH ; chdir ..
       patch -sl -p0 < $here/patches/binutils-${bfd_version}.diff || fatal "patch failed"
       touch "$BINUTILS_SRC_PATH/PATCHED-FOR-MINGW32-CPD"
       chdir $here
   fi
fi


if [ -f $here/patches/gcc-${gcc_version}.diff ]; then
    grep -q $GCCVER "$gcc_version_c_path"
    if [ $? = 0 ] ; then
	echo "Fine, gcc is already patched"
    else
	prompt_bool "Next step is to patch gcc. Okay" y
	if [ "$answer" = "n" ]; then
	    echo "No? So you can't continue"
	    exit 1
	fi
	echo "Patching $GCC_SRC_PATH"
	chdir $GCC_SRC_PATH ; chdir ..
	patch -sl -p0 < $here/patches/gcc-${gcc_version}.diff || fatal "patch failed"
	chdir $here
    fi
fi

#------------------------------------
# Configure the GNU packages
#------------------------------------

[ -d $here/binutils-build ] || mkdir $here/binutils-build
[ -d $here/gcc-build ] || mkdir $here/gcc-build

prompt_bool "Next step ist to configure gcc and binutils. Okay" y
if [ "$answer" = "n" ]; then
    echo "No? So you can't continue"
    exit 1
fi


tmp=`cd $BINUTILS_SRC_PATH; pwd`
cd $here/binutils-build
$tmp/configure --target=${TARGET} --prefix=$PREFIX || fatal "configure failed"

tmp=`cd $GCC_SRC_PATH; pwd`
cd $here/gcc-build
$tmp/configure --target=${TARGET} --with-gnu-as \
	    --with-gnu-ld --prefix=$PREFIX || fatal "configure failed"
cd $here


#========================
# mark configure complete
#========================
touch config.okay

## end of configure script ###
