#!/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 up -C . # in case previous build mistakenly modified some source files
    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)" | zip -q $DIST/zip/$BASENAME.zip -@ || exit

find netbeans | egrep -v "netbeans/(extra|testtools|xml|mobility|enterprise|visualweb|uml|ruby|soa|cnd|identity|php|groovy|webcommon)" | zip -q $DIST/zip/$BASENAME-javase.zip -@ || exit
find netbeans | egrep -v "netbeans/(extra|testtools|uml|ruby|soa|cnd|identity|php)" | zip -q $DIST/zip/$BASENAME-java.zip -@ || exit
find netbeans | egrep -v "netbeans/(extra|testtools|xml|java|apisupport|harness|profiler|mobility|enterprise|visualweb|uml|soa|identity|cnd|php|groovy)" | 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|groovy)" | 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|groovy|webcommon)" | zip -q $DIST/zip/$BASENAME-cnd.zip -@ || exit

cd netbeans
expat='extra|testtools'
for c in platform ide java apisupport harness enterprise profiler visualweb ruby mobility soa xml cnd identity gsf php groovy; 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_prev=`pwd`/../lastSuccessful/archive/nbbuild/build/sigtest/snapshot

echo SigTest: Checking All Changes
ant -Dsigtest.basedir=$sigtest_prev check-sigtests
for report in `pwd`/nbbuild/build/sigtest/check-snapshot/*; do
  if ! expr "$report" : ".*/TESTS-.*" && grep 'failures="1"' $report >/dev/null; then
    sigtest_email=`cat $report | grep "^email:" | grep -v nbm.domain | cut -c 8-`
    if [ -z "$sigtest_email" ]; then
      sigtest_email="api-changes@netbeans.org"
    fi
    content=`cat $report`
    (
        echo "Code changes:";
        echo "http://deadlock.netbeans.org/hudson/job/$JOB_NAME/$BUILD_NUMBER/changes"; 
        echo "API changes:";
        echo "http://deadlock.netbeans.org/hudson/job/$JOB_NAME/$BUILD_NUMBER/artifact/nbbuild/build/sigtest/check-snapshot/html/index.html"; 
        echo "Failure output is available as StdErr output of sigtest test for appropriate module.";
#                echo "http://deadlock.netbeans.org/hudson/job/$JOB_NAME/$BUILD_NUMBER/artifact/$sigtest_out/`basename $report`"; 
        echo; 
        expr "$content" : ".*CDATA\[\(.*\)\]\].*"
    ) | mail -s "`basename $report` - sigchanges in $JOB_NAME build #$BUILD_NUMBER" $sigtest_email
  fi
done
echo SigTest: Checking All Changes Done

echo SigTest: Generating Golden Files
ant gen-sigtests || exit 1
echo SigTest: Generating Golden Files Done

echo SigTest: Incompatible Changes
ant check-sigtests-release -Dfail.on.error=false
echo SigTest: Incompatible Changes Done

###############  Commit validation tests  ##########################
TESTS_STARTED=`date`
TESTS_RESULT_DIR="`pwd`/nbbuild/build/test/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
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
ant -f soa.kit/build.xml -Dtest.config=uicommit -Dbuild.test.qa-functional.results.dir=$TESTS_RESULT_DIR -Dcontinue.after.failing.tests=true test
ant -f xml.schema/build.xml -Dtest.config=uicommit -Dbuild.test.qa-functional.results.dir=$TESTS_RESULT_DIR -Dcontinue.after.failing.tests=true test
# 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
ant -f mobility.project/build.xml -Dtest.config=uicommit -Dtest-qa-functional-sys-prop.wtk.dir=/hudson -Dbuild.test.qa-functional.results.dir=$TESTS_RESULT_DIR -Dcontinue.after.failing.tests=true test
# 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
# Ruby UI, Profiler UI and CND UI validation tests
ant -f ide.kit/build.xml -Dtest.config=uicommit -Dbuild.test.qa-functional.results.dir=$TESTS_RESULT_DIR -Dcontinue.after.failing.tests=true -Dtest-qa-functional-sys-prop.com.sun.aas.installRoot=/hudson/workdir/jobs/trunk/testappsrv/glassfish test
# PHP UI validation tests
ant -f php.editor/build.xml -Dtest.config=uicommit -Dbuild.test.qa-functional.results.dir=$TESTS_RESULT_DIR -Dcontinue.after.failing.tests=true test
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
# Make sure the build did not modify any files, or produce any nonignorable files.
test -z "`hg st | tee /dev/stderr`" || exit
