#!/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" MAINPREFIX
make_abs MAINPREFIX
if [ ! -d "$MAINPREFIX" ]; then
    fatal "Directory $MAINPREFIX does not exists"
fi
PREFIX=$MAINPREFIX/lib/mingw32-cpd
if [ ! -d "$PREFIX" ]; then
    mkdir -p $PREFIX
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

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


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

write_mk GCCVER
write_mk CPDVER
write_mk TARGET
write_mk MAINPREFIX
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
#----------------------------------

# we are going to build them in a subdirectory
[ -d $here/binutils-build ] || mkdir $here/binutils-build
[ -d $here/gcc-build ] || mkdir $here/gcc-build

if [ -f $here/patches/binutils-${bfd_version}.diff ]; then
    if [ -f "$here/binutils-build.src/mingw32-cpd-$CPDVER.stamp" ] ; then
	echo "Fine, binutils are already patched"
    else
	echo "Patching in binutils-build"
        [ -d $here/binutils-build.src ] || mkdir $here/binutils-build.src
        chdir $here/binutils-build.src
        ( cd $BINUTILS_SRC_PATH && tar cf - . ) | tar xf - 
	patch -p1 < $here/patches/binutils-${bfd_version}.diff || fatal "patch failed"
        touch "mingw32-cpd-$CPDVER.stamp" 
	chdir $here
   fi
fi


if [ -f $here/patches/gcc-${gcc_version}.diff ]; then
    if [ -f "$here/gcc-build.src/mingw32-cpd-$CPDVER.stamp" ] ; then
	echo "Fine, gcc is already patched"
    else
	echo "Patching in gcc-build.src"
        # first create copies and then patch
        # ooos: we cant do tha because the Makefile sometimes uses
        # paths to the orginal src and does not relay on the VPATH feature.
        # So, lets copy the whole source 
        #files=`awk <$here/patches/gcc-${gcc_version}.diff '
        #         /^diff / {sub(/^[^\/]*\//,"",$NF); print $NF}
        #        '`
        #for i in $files; do
        #   mkdir -p $here/gcc-build/`dirname $i`
        #   cat $GCC_SRC_PATH/$i >$here/gcc-build/$i
        #one
        [ -d $here/gcc-build.src ] || mkdir $here/gcc-build.src
        chdir $here/gcc-build.src
        ( cd $GCC_SRC_PATH && tar cf - . ) | tar xf - 
	patch  -p1 < $here/patches/gcc-${gcc_version}.diff || fatal "patch failed"
        # need some additional fixes
        for i in gcc/config/i386/mingw32.h gcc/config/i386/crtdll.h ; do
            [ -f "$i" ] || fatal "$i not found"
            sed \
            -e "s,-D__MINGW32_CPD__=[0-9]*,-D__MINGW32_CPD__=${CPDVER_AGE},"\
            -e "s,/usr/local,${PREFIX},g"  $i >$i.tmp && mv $i.tmp $i 
         done
        touch "mingw32-cpd-$CPDVER.stamp" 
	chdir $here
    fi
fi

# Now get the GCC version
[ -f gcc-build.src/gcc/version.c ] \
     || fatal "gcc-build.src/gcc/version.c not found"
GCCVER=`sed -n < gcc-build.src/gcc/version.c '
/version_string/ s/[^"]*"\([^ "]*\).*/\1/p
'`
[ -z "$GCCVER" ] && fatal "gcc version not found"
write_mk GCCVER

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

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


cd $here/binutils-build
if [ -f ../binutils-build.src/configure ]; then
  tmp=../binutils-build.src
else
  tmp=`cd $BINUTILS_SRC_PATH; pwd`
fi
$tmp/configure --target=${TARGET} --prefix=$PREFIX || fatal "configure failed"


cd $here/gcc-build
if [ -f ../gcc-build.src/configure ]; then
  tmp=../gcc-build.src
else
  tmp=`cd $GCC_SRC_PATH; pwd`
fi
$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 ###
