#!/bin/sh

directory=${1-gcc}
options=${2-standard}
languages=${3-fortran}
patch=${4-nopatch}
action=${5-test}
ntasks=${6-8}

# Number of cores

ncores=`cat /proc/cpuinfo | grep ^processor | wc -l`

# How to connect to the central system to send e-mail

export ZEND=

# Remove previous compilation log and move to the correct directory

rm $HOME/compilers/log
cd $HOME/compilers/$directory || exit 1

# Request the inclusion of a patch - or none if not given

if [ $patch = nopatch ]
then
   patcharg=""
else
   patcharg="--patch $patch"
fi

# Update the git repository, and - after that - apply a patch, if any

datetime=`date -u +"%Y%m%d%H%M%S"`
./contrib/gcc_update &>> $HOME/compilers/log
if [ $? -ne 0 ]
then
    tail -90 $HOME/compilers/log |	\
        $ZEND mail -s			\
            \"$0: gcc_update FAILED\"	\
                toon@moene.org
    exit 1
fi

if [ ! -z "$patcharg" ]
then
    ./contrib/gcc_update $patcharg &>> $HOME/compilers/log
    if [ $? -ne 0 ]
    then
        tail -90 $HOME/compilers/log |			\
            $ZEND mail -s				\
                \"$0: gcc_update $patcharg FAILED\"	\
                    toon@moene.org
        exit 1
    fi
fi

# Save the patch - if any

[ -z "$patcharg" ] || { echo "------"; echo WITH PATCH: ; echo; git diff; echo "------"; echo; } > /tmp/thepatch

# Save other specifics for this build

system=`gcc -v 2>&1 | awk '/^Target: / {print $2}'`
repository=`git remote show origin | awk '/^  Fetch/ {print $3}'`
branch=`git branch | awk '/^\*/ {print $2}'`
descr=`git gcc-descr $branch`
[ -z "$descr" ] && descr=$datetime

# Get the external (source) libraries required for the build

./contrib/download_prerequisites --force &>> $HOME/compilers/log
if [ $? -ne 0 ]
then
    tail -90 $HOME/compilers/log |			\
        $ZEND mail -s					\
            \"$0: download_prerequisites FAILED\"	\
                toon@moene.org
    exit 1
fi

# Where to place the files during the build

if [ `awk '/MemTotal:/ {print $2}' /proc/meminfo` -gt 32000000 ]
then
    export BLDDIR=/dev/shm/bld$$
else
    export BLDDIR=$HOME/scratch/bld$$
fi
mkdir $BLDDIR || exit 1
cd $BLDDIR || exit 1

# Construct the configuration command

prefix=$HOME/compilers/install/$directory
[ -d $prefix ] && rm -rf $prefix
configure="$HOME/compilers/$directory/configure	\
                --prefix=$prefix		\
                --with-gnu-as			\
                --with-gnu-ld			\
                --enable-languages=$languages	\
                --disable-multilib		\
                --disable-nls"

# ... and augment it with options given on the Bootstrap command

if [ "$options" != "standard" ]
then
    for option in `echo $options | sed -e 's/:/ /g'`
    do
        if [ "${option:0:9}" = "checking=" ]
        then
            configure="$configure --enable-$option"
        else
            configure="$configure --with-build-config=bootstrap-$option"
        fi
    done
fi

# Summarize the options asked for in the bootstrap configuration

comment=""; [ -z "$patcharg" ] || comment="; with patch"
configspec="(branch: $branch; revision: $descr; build config: $options; languages: $languages; tasks: $ntasks$comment) on $system"

# Start the bootstrap

($configure && make -j $ntasks STAGE1_CFLAGS="-O2") &>> $HOME/compilers/log

# Evaluate the results

if [ $? -eq 0 ]
then
    if [ "$action" = "test" ]
    then
        make -j $ncores -k check &>> $HOME/compilers/log
        prepend=""; [ -z "$patcharg" ] || prepend="-p /tmp/thepatch"
        $HOME/scr/test_summary $prepend | sed -e "s|Results|'Results of testsuite $configspec.'|" | sh
        (cd $BLDDIR/gcc/testsuite; cat */*.log.sent | gzip > $HOME/compilers/gcc-tests.log.gz)
    fi
    make -j $ncores install &>> $HOME/compilers/log
else
    rm $HOME/compilers/errorlog
    subject="FAILED: Bootstrap $configspec."
    [ -z "$patcharg" ] || cp /tmp/thepatch $HOME/compilers/errorlog
    grep -C 200 "internal compiler error" $HOME/compilers/log >> $HOME/compilers/errorlog
    [ $? -eq 0 ] || tail -500 $HOME/compilers/log >> $HOME/compilers/errorlog
    (fold -s -w 300 $HOME/compilers/errorlog; echo; echo Configured by: $configure) | \
        $ZEND mail -s \"$subject\" gcc-testresults@gcc.gnu.org
fi

# Wrapup

cd

[ -z "$patcharg" ] || (rm /tmp/thepatch && cd $HOME/compilers/$directory && git restore .)

rm -rf $BLDDIR

exit 0
