#!/bin/sh

if [ x$BUILD_ID = x ]
then
    # Non-Hudson use.
    if [ x$USERNAME = x ]
    then
        BUILD_ID=private
    else
        BUILD_ID=$USERNAME-private
    fi
    dir=`dirname $0`/../..
    cd $dir
else
    hg --config extensions.purge= clean --all || exit
fi

DIST=$PWD/nbbuild/dist
rm -rf $DIST/

# XXX should use hg id as the build number; timestamp is useless!

# YYYYMMDDhhmmss timestamp
BUILDNUMBER=`echo $BUILD_ID | tr -d _-` 
BUILDNUM=$BUILD_TAG

ant -Dbuildnum=$BUILDNUM -Dbuildnumber=$BUILDNUMBER verify-libs-and-licenses build-nozip || exit 

# we can't afford to run build-nbms in hudson builds, on deadlock.netbeans.org it takes 6 minutes from a typical 15 min build run
# mkdir -p $DIST/nbms
# ant -Dbuildnum=$BUILD_ID build-nbms -Dmoduleconfig=all -Dbase.nbm.target.dir=${DIST}/nbms || exit

cd nbbuild
rm -f netbeans/nb.cluster.*
# Do not delete. It is needed for tests to determine classpath.
#rm -f netbeans/moduleCluster.properties
# Do not delete. It contains 'hg id' of repository in time of build.
#rm -f netbeans/build_info

BASENAME=netbeans-$BUILDNUM

mkdir -p $DIST/zip/moduleclusters

find netbeans | egrep -v "netbeans/(extra|testtools|php)" | zip -q $DIST/zip/$BASENAME.zip -@ || exit

find netbeans | egrep -v "netbeans/(extra|testtools|xml|mobility|enterprise|visualweb|uml|ruby|soa|cnd|identity|php)" | zip -q $DIST/zip/$BASENAME-java.zip -@ || exit
find netbeans | egrep -v "netbeans/(extra|testtools|xml|enterprise|visualweb|uml|ruby|soa|cnd|identity|php)" | egrep -v "(org-netbeans-modules-mobility-end2end|org-netbeans-modules-mobility-jsr172)" | zip -q $DIST/zip/$BASENAME-mobility.zip -@ || exit
find netbeans | egrep -v "netbeans/(extra|testtools|mobility|uml|ruby|soa|cnd|identity|php)" | zip -q $DIST/zip/$BASENAME-javaee.zip -@ || exit
find netbeans | egrep -v "netbeans/(extra|testtools|xml|java|apisupport|harness|profiler|mobility|enterprise|visualweb|uml|soa|identity|cnd|php)" | zip -q $DIST/zip/$BASENAME-ruby.zip -@ || exit
find netbeans | egrep -v "netbeans/(extra|testtools|xml|java|apisupport|harness|profiler|mobility|enterprise|visualweb|uml|soa|identity|cnd|ruby)" | zip -q $DIST/zip/$BASENAME-php.zip -@ || exit
find netbeans | egrep -v "netbeans/(extra|testtools|xml|java|apisupport|harness|profiler|mobility|enterprise|visualweb|uml|ruby|soa|identity|php)" | zip -q $DIST/zip/$BASENAME-cnd.zip -@ || exit

cd netbeans
expat='extra|testtools'
for c in platform ide java apisupport harness enterprise profiler uml visualweb ruby mobility soa xml cnd identity gsf php; do
    find * | egrep "^$c[0-9]*/" | zip -q $DIST/zip/moduleclusters/$BASENAME-$c.zip -@ || exit
    expat="$expat|$c[0-9]*"
done
find * | egrep -v "^($expat)(/|$)" | zip -q $DIST/zip/moduleclusters/$BASENAME-nb6.0-etc.zip -@ || exit

cp -p $DIST/zip/moduleclusters/$BASENAME-platform.zip $DIST/zip/

cd ../..
generated=nbbuild/build/generated
rm -rf $generated
ant generate-golden-files generate-files-layout index-layer-paths || exit
cp -v nbbuild/netbeans/THIRDPARTYLICENSE-generated.txt $generated/external-libraries.txt || exit
prev_generated=../lastSuccessful/archive/$generated
if [ -d $prev_generated ]; then
    diff -r -U 25 $prev_generated $generated > $generated.diff
    if [ -s $generated.diff ]; then
        (echo "http://deadlock.netbeans.org/hudson/job/$JOB_NAME/$BUILD_NUMBER/changes"; echo "http://deadlock.netbeans.org/hudson/job/$JOB_NAME/$BUILD_NUMBER/artifact/$generated/"; echo; cat $generated.diff) | mail -s "Golden file changes in $JOB_NAME build #$BUILD_NUMBER" api-changes@netbeans.org
    fi
fi

sigtest_jar=`pwd`/../sigtestdev.jar
sigtest_prev=`pwd`/../lastSuccessful/archive/nbbuild/build/sigtests
sigtest_gen=`pwd`/nbbuild/build/sigtests
sigtest_out=nbbuild/build/sigtestscheck

echo SigTest: Checking All Changes
if [ -f $sigtest_jar -a -d $sigtest_prev ]; then
    ant -Dsigtest.basedir=$sigtest_prev -Dsigtest.jar=$sigtest_jar -Dsigtest.check.type=strictcheck -Dsigtest.output.dir=`pwd`/$sigtest_out check-sigtests > $sigtest_gen.log
    ERROR_CODE=$?
    if [ $ERROR_CODE != 0 ]; then
        for report in `pwd`/$sigtest_out/*; do
            sigtest_email=`head -n 1 $report | grep "^email:" | cut -c 8-`
            if [ -z "$sigtest_email" ]; then
              sigtest_email="api-changes@netbeans.org"
            fi
            (
                echo "http://deadlock.netbeans.org/hudson/job/$JOB_NAME/$BUILD_NUMBER/changes"; 
                echo "http://deadlock.netbeans.org/hudson/job/$JOB_NAME/$BUILD_NUMBER/artifact/$sigtest_out/`basename $report`"; 
                echo; tail -n 200 $report
            ) | mail -s "`basename $report` - sigchanges in $JOB_NAME build #$BUILD_NUMBER" $sigtest_email
        done
    fi

fi
echo SigTest: Checking All Changes Done

echo SigTest: Generating Golden Files
if [ -f $sigtest_jar ]; then
    ant -Dsigtest.basedir=$sigtest_gen -Dsigtest.jar=$sigtest_jar gen-sigtests >$sigtest_gen.log 2>&1
    ERROR_CODE=$?
    if [ $ERROR_CODE != 0 ]; then
        (
            echo "http://deadlock.netbeans.org/hudson/job/$JOB_NAME/$BUILD_NUMBER/changes"; 
            echo "http://deadlock.netbeans.org/hudson/job/$JOB_NAME/$BUILD_NUMBER/artifact/$sigtest_out/`basename $report`"; 
            echo; tail -n 200 $sigtest_gen.log
        ) | mail -s "`basename $report` - cannot generate sigtest golden files in $JOB_NAME build #$BUILD_NUMBER" api-changes@netbeans.org

        cat >$sigtest_gen.xml <<KONEC
<?xml version="1.0" encoding="UTF-8"?>
<testsuite errors="0" failures="1" name="org.netbeans.nbbuild.SigTest" tests="1" time="0.0">
    <testcase classname="org.netbeans.GenerateSigTest" name="testSignaturesCanBeGenerated" time="0.0">
        <failure message="Some module provides wrong API" type="junit.framework.AssertionFailedError">
KONEC
        tail -n 50 $sigtest_gen.log >>$sigtest_gen.xml
cat >>$sigtest_gen.xml <<KONEC
        </failure>
        <system-out><![CDATA[
KONEC
        cat $sigtest_gen.log >>$sigtest_gen.xml
        cat >>$sigtest_gen.xml <<KONEC
]]></system-out>
        <system-err></system-err>
    </testcase>
</testsuite>
KONEC
    fi
else 
    echo No $sigtest_jar found!
fi
echo SigTest: Generating Golden Files Done

echo SigTest: Incompatible Changes
sigtest_incompat_gen=nbbuild/build/sigtestsincompat
if ! ant -Dsigtest.output.dir=`pwd`/$sigtest_incompat_gen check-sigtests > `pwd`/$sigtest_incompat_gen.log; then
    (
        echo "http://deadlock.netbeans.org/hudson/job/$JOB_NAME/$BUILD_NUMBER/changes"; 
        echo "http://deadlock.netbeans.org/hudson/job/$JOB_NAME/$BUILD_NUMBER/artifact/$sigtest_incompat_gen"; 
        echo; 
        cat `pwd`/$sigtest_incompat_gen/*
    ) | mail -s "Incompatible API change $JOB_NAME build #$BUILD_NUMBER" api-changes@netbeans.org
fi
echo SigTest: Incompatible Changes Done

###############  Commit validation tests  ##########################
TESTS_STARTED=`date`
# Clean previous test results - if xtest compilation fails, old results stays in workdir and it passes
rm -rf xtest/instance/results
# Different JDK for tests because JVM crashes often (see 6598709, 6607038)
JDK_TESTS=/opt/jdk1.5.0_14
# standard NetBeans unit and UI validation tests
ant commit-validation
# Init application server for tests
sh -x `dirname $0`/initAppserver.sh
# visualweb UI validation tests
# Disabled because permanent failures
#sh -x `dirname $0`/run-vw-sanity.sh
# SOA (BPEL, XSLT) and XML UI validation tests
ant -f xtest/instance/build.xml -Djdkhome=$JDK_TESTS -Dxtest.config=commit-validation-enterprise -Dxtest.instance.name="Enterprise tests" -Dxtest.no.cleanresults=true runtests
# CND UI validation tests
ant -f xtest/instance/build.xml -Djdkhome=$JDK_TESTS -Dxtest.config=commit-validation-cnd -Dxtest.instance.name="CND tests" -Dxtest.no.cleanresults=true runtests
# Profiler UI validation tests
ant -f xtest/instance/build.xml -Djdkhome=$JDK_TESTS -Dxtest.config=commit-validation-profiler -Dxtest.instance.name="Profiler tests" -Dxtest.no.cleanresults=true runtests
# J2EE UI validation tests
ant -f xtest/instance/build.xml -Djdkhome=$JDK_TESTS -Dxtest.config=commit-validation-j2ee -Dxtest.instance.name="J2EE tests" -Dxtest.no.cleanresults=true -D"xtest.userdata|com.sun.aas.installRoot"=/hudson/workdir/jobs/trunk/testappsrv/glassfish runtests
# Mobility UI validation tests
ant -f xtest/instance/build.xml -Djdkhome=$JDK_TESTS -Dxtest.config=commit-validation-mobility -Dxtest.instance.name="Mobility tests" -Dxtest.no.cleanresults=true -Dwtk.dir=/hudson runtests
# UML UI validation tests
ant -f xtest/instance/build.xml -Djdkhome=$JDK_TESTS -Dxtest.config=commit-validation-uml -Dxtest.instance.name="UML tests" -Dxtest.no.cleanresults=true runtests
# Ruby UI validation tests
ant -f xtest/instance/build.xml -Djdkhome=$JDK_TESTS -Dxtest.config=commit-validation-ruby -Dxtest.instance.name="Ruby tests" -Dxtest.no.cleanresults=true runtests
echo TESTS STARTED: $TESTS_STARTED
echo TESTS FINISHED: `date`

# Clean up some stuff dumped in the build by tests:
rm -rf nbbuild/netbeans/testtools
rm -f nbbuild/netbeans/bin/hs_err_*
# Check to see if any other unregistered files remain (if so, delete them now so the problem can be fixed in incremental builds) (cf. #111946):
ant clean-untracked-files

# Do not halt if there were failures, but do collect results for Hudson:
ant commit-validation-junit-format
