#!/bin/sh


if [ -z "$WINEPREFIX" ]
then
    echo "\"\$WINEPREFIX\" is empty"
    exit 1
fi

old_dir="`pwd`"

cd "$WINEPREFIX"
[ -d "bin" ] || mkdir "bin"
cd "bin"


#if [ -z "$CFLAGS" ]
#then
#    CFLAGS="-O2 -march=pentium4 -g"
#    export CFLAGS
#fi


# CC="$CC" CFLAGS="$CFLAGS" LD="$LD" LDFLAGS="$LDFLAGS"  

_makewine_cflags=""

cmpl="`which colormake.pl`"

if [ -z "$cmpl" ]
then
    cmpl="cat"
fi


if [ -n "$CFLAGS" ]
then
    _makewine_cflags="CFLAGS=\"\$CFLAGS\""
fi


if [ "$1" = "docs" ]
then
    shift
    cd "$WINEPREFIX"
    [ -d "docs" ] || mkdir "docs"
    cd "docs"

    time "$WINEPREFIX/docs.src/configure" $1 $2 $3 $4 $5 | tee configure.txt  | "$cmpl" 
    sleep 0.5s
    time make | tee  make.txt | "$cmpl" 

else
    if [ "$1" = "pe" -o "$1" == "w32" -o "$1" == "win" ]
    then

        shift
	cd "$WINEPREFIX"
        [ -d "bin" ] || mkdir "bin"
	cd "bin"

	time  "$WINEPREFIX/src/configure"  $2 $3 $4 $5 $6 | tee configure.txt  | "$cmpl" 
        sleep 0.5s
	time make    ${_makewine_cflags}  depend | "$cmpl"
        sleep 0.5s
	time make    ${_makewine_cflags} -C tools 2>&1  | tee  make.txt | "$cmpl" 


        cd "$WINEPREFIX"
	[ -d "bin.pe" ] || mkdir "bin.pe"
        cd "bin.pe"

	time  "$WINEPREFIX/src/configure" --with-wine-tools="$WINEPREFIX/bin" --host=i586-mingw32msvc  $2 $3 $4 $5 $6 | tee configure.txt  | "$cmpl" 
        sleep 0.5s
	time make    ${_makewine_cflags}  depend | "$cmpl"
        sleep 0.5s
	time make    ${_makewine_cflags} -k  $1  2>&1  | tee  make.txt | "$cmpl" 



    else
	time "$WINEPREFIX/src/configure"  $2 $3 $4 $5 $6 | tee configure.txt  | "$cmpl" 
        sleep 0.5s
	time make    ${_makewine_cflags}  depend | "$cmpl"
        sleep 0.5s
	time make    ${_makewine_cflags} $1  2>&1 | tee  make.txt | "$cmpl" 
    fi

fi

cd "$old_cd"

