#!/bin/sh

# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.

if [ -z $repository && -n $1 ]; then
    repository = $1
fi

if [ -z $action && -n $2]; then
    action = $2
fi

if [ -z $push_username && -n $3 ]; then
    push_username = $3
fi

if [ -z $push_password && -n $4 ]; then
    push_password = $4
fi

[ "$action" = "build" ] || [ "$action" = "sync" ] || exit 101

[ -n "$push_username" ] || exit 102
[ -n "$push_password" ] || exit 103

[ -n "$repository" ] || exit 104

ANT_OPTS="-Xmx512m"
export ANT_OPTS
HGMERGE=merge
export HGMERGE

rm -rf real.workspace
hg clone . real.workspace || exit 1
cd real.workspace

testmodule() {
    ant -f $1/build.xml test -Dtest-unit-sys-prop.ignore.random.failures=true
}

if [ "$action" = "build" ]; then
#    hg fetch http://hg.netbeans.org/main || exit 1
#    hg pull http://hg.netbeans.org/main || exit 1
#    if [ `hg heads --template '{node|short}\n' | wc -l` = 1 ]
#    then
#       hg up || exit 6
#    else
#       hg merge || exit 7
#       hg ci -m 'Automated merge' || exit 8
#    fi

    ant build || exit 2
    ant commit-validation # || exit 3
    testmodule openide.util  || exit 4
    testmodule openide.modules || exit 4
    testmodule openide.filesystems || exit 4
# masterfs is currently broken, Radek wants me to disable it until 6.1M2:
#    ant -f masterfs|| exit 4
    testmodule openide.nodes|| exit 4
    testmodule openide.options || exit 4
    testmodule openide.dialogs || exit 4
    testmodule openide.awt || exit 4
    testmodule openide.windows || exit 4
    testmodule core.startup # || exit 4
#    testmodule openide.io || exit 4
    if [ `hg out --template '{node|short}\n' http://hg.netbeans.org/main | wc -l` > 0 ]; then
        echo No outgoing changes
#        exit 0
    fi

    for i in 1 2 3; do
       hg pull http://hg.netbeans.org/main || exit 5
       if [ `hg heads --template '{node|short}\n' | wc -l` = 1 ]
       then
           hg up || exit 6
       else
           hg merge || exit 7
           hg ci -m 'Automated merge' || exit 8
       fi
       hg out http://hg.netbeans.org/main
       hg in http://hg.netbeans.org/main
       hg push https://"$push_username":"$push_password"@hg.netbeans.org/main && exit 0
    done
    exit 5
fi

if [ "$action" = "sync" ]; then
    HEADS=`hg heads --template "{node}\n" | wc -l`
    if [ $HEADS = 2 ]; then
      hg merge
      hg ci -m "Automated merge"
      hg up -C
    fi
    hg fetch http://hg.netbeans.org/$repository
    hg push https://"$push_username":"$push_password"@hg.netbeans.org/$repository
    exit 0
fi




