#!/bin/sh
# 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"
}

#==========================
# 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

GCCVER=${GNU_GCCVER}-dd9jn-${CPDVER}

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")
    /bin/echo "$1 [$default]: \c"
    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
	/bin/echo "$1 (y/n) [$default]: \c"
	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
if [ ! -f "$GCC_SRC_PATH/version.c" ]; then
    echo "gcc is not installed in $GCC_SRC_PATH"
    exit 1
fi
gcc_version=$(sed -e 's/.* "\([0-9.]*\).*/\1/' "$GCC_SRC_PATH/version.c")
if [ "$gcc_version" != "${GNU_GCCVER}" ]; then
    echo "Sorry, this is gcc version $gcc_version"
    echo "       but we need version ${GNU_GCCVER}"
    exit 1;
fi

prompt "Where is the binutils source" BINUTILS_SRC_PATH
make_abs BINUTILS_SRC_PATH
if [ ! -f "$BINUTILS_SRC_PATH/bfd/VERSION" ]; then
    echo "binutils are not installed in $BINUTILS_SRC_PATH"
    exit 1
fi
bfd_version=$(cat "$BINUTILS_SRC_PATH/bfd/VERSION")
if [ "$bfd_version" != "2.8.1" ]; then
    echo "Sorry, these are binutils version $bfd_version"
    echo "       but we need version 2.8.1"
    exit 1;
fi

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 GNU_GCCVER
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
#----------------------------------

grep -q mingw32 "$BINUTILS_SRC_PATH/configure"
if [ $? = 0 ] ; 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-2.8.1.diff || fatal "patch failed"
    chdir $here
fi


grep -q $GCCVER "$GCC_SRC_PATH/version.c"
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-2.7.2.1.diff || fatal "patch failed"
    chdir $here
fi

#------------------------------------
# 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 $BINUTILS_SRC_PATH
./configure --target=${TARGET} --prefix=$PREFIX || fatal "configure failed"

cd $GCC_SRC_PATH
./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 ###
