#! /bin/bash

# $1 - a version number like 4.4.0
function buildVersion {
    cd $BASE
    echo Building release version $1
    VERSION_STRING=`echo $1 | sed 's/\./_/g'`

    echo Adding stamps
    mkdir -p stamps/gcc_"$VERSION_STRING"_stamps
    cd stamps/gcc_"$VERSION_STRING"_stamps

    TAG_LINE=`svn log svn://gcc.gnu.org/svn/gcc/tags/gcc_"4_4_0"_release --stop-on-copy | grep '^r' | tail -n 1`

    REVISION=`echo "$TAG_LINE" | sed 's/^r\| .*//g'`
    echo "$REVISION" > revision-number

    echo "$TAG_LINE" | sed -e 's/^[^|]*|[^|]*| //' -e 's/ (.*//' > human-readable-time

    EPOCH_RELATIVE_TIME=`date -f human-readable-time +%s`
    echo "$EPOCH_RELATIVE_TIME" > epoch-relative-time

    MIN=0
    MAX=50000
    while [ $MIN -lt $MAX ]
    do
	((MID=(MIN+MAX)/2))
	BUG_TAG_LINE=`wget --quiet --output-document=- "http://gcc.gnu.org/bugzilla/show_bug.cgi?id=$MID" | grep '<b>Opened:</b>' | sed 's/.*<.b> //'`
	EPOCH_RELATIVE_BUG_TIME=`date -d "$BUG_TAG_LINE" +%s`
	if [ $EPOCH_RELATIVE_BUG_TIME -lt $EPOCH_RELATIVE_TIME ]
	then
	    ((MIN=MID+1))
	else
	    ((MAX=MID))
	fi
    done
    echo "$MIN" > first-bug-after-release

    grep '' revision-number human-readable-time epoch-relative-time first-bug-after-release
    cd ../..

    echo Beginning download
    cd $BASE
    mkdir -p source
    cd source
    echo Destination directory is $BASE/source/gcc_"$VERSION_STRING"_release
    svn -r$REVISION co svn://gcc.gnu.org/svn/gcc/tags/gcc_"$VERSION_STRING"_release
    echo Download done

    echo Configuring
    cd ..
    mkdir -p builds/gcc_"$VERSION_STRING"_release
    cd builds/gcc_"$VERSION_STRING"_release
    ../../source/gcc_"$VERSION_STRING"_release/configure --prefix=$BASE/installs/gcc_"$VERSION_STRING"_release
    echo Configuration done

    echo Making
    mkdir -p ../../installs/gcc_"$VERSION_STRING"_release
    make
    make install
    echo Make done
    cd $BASE
}

# $1 - a bug number like 12603
function buildBug {
    echo Setting up bug $1
    PRIORITY=`wget --quiet --output-document=- "http://gcc.gnu.org/bugzilla/show_bug.cgi?id=$1" | grep -A11 'id="priority"' | grep 'selected>P' | sed 's/.*selected>//'`
    echo "$PRIORITY" > $BUGS/$1/priority-level
    chmod u+x $BUGS/$1/check.sh
    echo Bug setup done
}

# No arguments
function buildTools {
    echo Compiling MiniSAT
    (cd $MINISAT && make)
    echo Compilation done
    echo Compiling custom tools
    javac -Xlint:unchecked `find $CUSTOM -name '*.java'`
    echo Compilation done
}

# No arguments
function cleanup {
    echo Cleaning up
    mkdir -p $SANDBOX
    cd $SANDBOX
    rm *
    cd $BASE
    echo Clean-up done
}

# $1 - a version number like 4.4.0
# $2 - a bug number like 12603
function checkBug {
    cleanup
    cd $SANDBOX
    echo Checking version $1 with script script $BUGS/$2/check.sh
    VERSION_STRING=`echo $1 | sed 's/\./_/g'`
    REPORTED_CONFIG=`cat $BUGS/$2/check.sh | head -n2 | tail -n1 | sed 's/#//'`
    echo Bug $2 was reported under this configuration: $REPORTED_CONFIG
    echo Checking
    $BUGS/$2/check.sh
    echo "Exit code for $1 under default configuration: $?"
    $BUGS/$2/check.sh $REPORTED_CONFIG
    echo "Exit code for $1 under reported configuration: $?"
    echo Check done
}

# $1 - a version number like 4.4.0
# $2 - a bug number like 12603
function isolateBug {
    cleanup
    cd $SANDBOX
    echo Isolating version $1 with script script $BUGS/$2/check.sh
    VERSION_STRING=`echo $1 | sed 's/\./_/g'`
    REPORTED_CONFIG=`cat $BUGS/$2/check.sh | head -n2 | tail -n1 | sed 's/#//'`
    echo Bug $2 was reported under this configuration: $REPORTED_CONFIG
    echo Isolating

    java isolator.Isolator $RESULTS/isolation/$VERSION_STRING"-bug-"$2 $BASE/models/gcc_"$VERSION_STRING"_release $2 $BUGS/$2/check.sh $REPORTED_CONFIG
    echo Isolation done
}

# No arguments
function postprocess {
    echo Building schedule
    java scheduler.Scheduler $BASE/schedules/gcc $BUGS
    echo Schedule building done
    echo Postprocessing
    java postprocessor.Postprocessor $BASE/schedules/gcc `find $RESULTS/isolation`
    echo Postprocessing done
}

set -v

echo -n Checking that the working directory encloses deploy.sh...
export BASE=`pwd`
ls $BASE/deploy.sh > /dev/null
if [ $? -eq 0 ]
then
    echo ok
else
    echo deploy.sh should be invoked from the enclosing directory
    exit 1
fi

echo Setting environment variables:
echo Base path is $BASE

export TIMEOUT=$BASE/utilities/timeout.sh
echo Environment variable TIMEOUT=$TIMEOUT

export BUGS=$BASE/bugs
echo Environment variable BUGS=$BUGS

export TOOLS=$BASE/../tools
echo Environment variable TOOLS=$TOOLS

export MINISAT=$TOOLS/minisat/simp
echo Environment variable MINISAT=$MINISAT

export CUSTOM=$TOOLS/custom
echo Environment variable CUSTOM=$CUSTOM

export CLASSPATH=$CUSTOM
echo Environment variable CLASSPATH=$CLASSPATH

export SANDBOX=$BASE/sandbox
echo Environment variable SANDBOX=$SANDBOX

export RESULTS=$BASE/results
echo Environment variable RESULTS=$RESULTS

export VERSION_NUMBERS="4.4.0 4.4.1 4.4.2"
echo Environment variable VERSION_NUMBERS=$VERSION_NUMBERS

pushd $BUGS
export BUG_NUMBERS=`ls | grep '^[0-9]\+$'`
popd
echo Environment variable BUG_NUMBERS=$BUG_NUMBERS

for v in $VERSION_NUMBERS
do
    buildVersion "$v"
done
buildTools
for b in $BUG_NUMBERS
do
    buildBug "$b"
    for v in $VERSION_NUMBERS
    do
	# checkBug "$v" "$b"
	isolateBug "$v" "$b"
    done
done
postprocess
