#!/bin/sh

# CC='"gcc -mno-cygwin"'   # for mingw
CONF_OPT=       # essential configure option (e.g. --enable-shared on Cygwin)
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 2>&1 ||
                                  die "configure failed"
make > log.make 2>&1           || die "make failed"
make test                      || die "make test failed"
make install > log.inst 2>&1   || 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
./configure CFLAGS=$DEBUG_FLAG --prefix=$PREFIX/d2 $CONF_OPT > log.conf 2>&1 ||
                                  die "configure failed"
make > log.make 2>&1           || die "make failed"
make test                      || die "make test failed"
make install > log.inst 2>&1   || 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
../src3/configure CFLAGS=$DEBUG_FLAG --prefix=$PREFIX/d3 $CONF_OPT > log.conf 2>&1 ||
                                  die "configure failed"
#touch lex.c
make > log.make 2>&1           || die "make failed"
make test                      || die "make test failed"
make install > log.inst 2>&1   || 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"
