#!/bin/sh

CONF_OPT="--with-gcc='gcc -mno-cygwin' i386-mingw32"
DEBUG_FLAG=-g   # cc debug option

PREFIX=~/tmp    # install target

GZIP=gzip
TAR=tar
RM_RF='rm -rf'


#
# utils
#
die() {
    echo $1
    exit 1
}


#
# setup sources
#
for num in 1 2 3
do
    $RM_RF src$num
    $GZIP -dc ruby.tar.gz | $TAR xf -
    mv ruby src$num
done
$GZIP -dc nodedump.tar.gz | $TAR xf -


#
# step 1: compile ruby without any special settings.
#
echo "step 1"
cd src1
./configure --prefix=$PREFIX/d1 $CONF_OPT &> log.conf ||
                                  die "configure failed"
make &> log.make               || die "make failed"
make test                      || die "make test failed"
make install &> log.inst       || die "make install failed"
cd -

$PREFIX/d1/bin/ruby testruby.rb    || die "test failed"


#
# step 2: compile ruby with debug flag.
#
echo "step 2"
cd src2
CFLAGS=$DEBUG_FLAG ./configure --prefix=$PREFIX/d2 $CONF_OPT &> log.conf ||
                                  die "configure failed"
make &> log.make               || die "make failed"
make test                      || die "make test failed"
make install &> log.inst       || die "make install failed"
cd -

$PREFIX/d2/bin/ruby testruby.rb    || die "test failed"


#
# step 3: using another objdir.
#
echo "step 3"
$RM_RF obj3
mkdir obj3
cd obj3
CFLAGS=$DEBUG_FLAG ../src3/configure --prefix=$PREFIX/d3 $CONF_OPT &> log.conf ||
                                  die "configure failed"
make &> log.make               || die "make failed"
make test                      || die "make test failed"
make install &> log.inst       || die "make install failed"
cd -

$PREFIX/d3/bin/ruby testruby.rb    || die "test failed"


#
# step 4: compile/test nodedump
#
cd nodedump
$PREFIX/d2/bin/ruby extconf.rb || die "nodedump/extconf.rb failed."
make                           || die "nodedump make failed."
make install                   || die "nodedump install failed."
cd -


echo
echo
echo "test all ok. try:"
echo "$PREFIX/d2/bin/ruby dump-file.rb"
echo "$PREFIX/d2/bin/ruby dump-method.rb"
