#!/usr/bin/env bash
#
#   Licensed 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.

set +x

BASEDIR=/tmp/update-master-docs
mkdir -p $BASEDIR

SVNDIR=$BASEDIR/svn

GITDIR=$BASEDIR/git

rm -rf $SVNDIR
rm -rf $GITDIR

svn checkout https://svn.apache.org/repos/asf/bookkeeper/site/trunk/content/docs/master $SVNDIR
svn checkout https://github.com/apache/bookkeeper/trunk $GITDIR

mvn -f $GITDIR/pom.xml -Dnotimestamp javadoc:aggregate

rsync -avP --exclude=.svn $GITDIR/doc/ $SVNDIR/
rsync -avP --exclude=.svn $GITDIR/target/site/apidocs/ $SVNDIR/apidocs/
svn add --force $SVNDIR/*

CHANGES=$(svn status $SVNDIR| awk 'BEGIN { COUNT = 0; } /^[A|M]/ { COUNT = COUNT + 1; } END { print COUNT }')
if [ $CHANGES -gt 0 ]; then
    svn commit -m "Syncing website with master documentation" $SVNDIR
    echo
    echo "Now go review on http://bookkeeper.staging.apache.org, and publish if it's ok"
    echo
else
    echo "No changes, submitting nothing"
fi
