#!/bin/bash

#   Copyright 2001-2004 The Apache Software Foundation
#
#   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.

if test "$1" = "-cp"; then
  shift
  export CP="$1"
  shift
fi

test -n "$1" && SOURCE=$1
test -z "$1" && SOURCE=`hostname | sed s/[.].*//`.xml

# ----- Cygwin Unix Paths Setup -----------------------------------------------

# Cygwin support.  $cygwin _must_ be set to either true or false.
case "`uname`" in
  CYGWIN*) cygwin=true ;;
  *) cygwin=false ;;
esac

# For Cygwin, ensure paths are in UNIX format before anything is touched
if $cygwin ; then
  export CLASSPATH=`cygpath --path --unix "$CLASSPATH"`
  if test -n "$XALAN"; then
    export CLASSPATH=$XALAN/bin/xml-apis.jar:$XALAN/bin/xalan.jar:$CLASSPATH
    export CLASSPATH=$XALAN/bin/xerces.jar:$CLASSPATH
  fi
  export CLASSPATH=.:jenny.jar:$CLASSPATH
  export CLASSPATH=`cygpath --path --windows "$CLASSPATH"`

  if test "$OSTYPE" = "" ; then
  	export OSTYPE="cygwin"
  fi

  JSOURCE=`cygpath -a -w -p "$SOURCE"`
else
  if test -n "$XALAN"; then
    export CLASSPATH=$XALAN/bin/xml-apis.jar:$XALAN/bin/xalan.jar:$CLASSPATH
    export CLASSPATH=$XALAN/bin/xerces.jar:$CLASSPATH
  fi
  export CLASSPATH=.:jenny.jar:$CLASSPATH
  JSOURCE=$SOURCE
fi

test -d work && rm -rf work
mkdir work
test -d cache || mkdir cache

# ********************************************************************

echo Merging projects into workspace
rm -rf classes
mkdir classes
javac -d classes java/*.java || export FAIL=1
jar cf jenny.jar -C classes . || export FAIL=1
echo
java -classpath "$CLASSPATH" Jenny $JSOURCE || export FAIL=1

# ********************************************************************

echo Generate checkout instructions
test -n "$FAIL" || \
java org.apache.xalan.xslt.Process -xml -in work/merge.xml -xsl stylesheet/update.xsl -out work/update.xml || \
export FAIL=1

echo Applying web site stylesheet
test -n "$FAIL" || \
java org.apache.xalan.xslt.Process -xml -in work/update.xml -xsl stylesheet/jakarta.xsl -out work/updatesite.xml || \
export FAIL=1

echo Generating update script
test -n "$FAIL" || \
java org.apache.xalan.xslt.Process -text -in work/updatesite.xml -xsl stylesheet/bash.xsl -out work/update.sh -PARAM cmd-prefix "$CP" -PARAM os-type "$OSTYPE" || \
export FAIL=1

# ********************************************************************

echo Generate build instructions
test -n "$FAIL" || \
java org.apache.xalan.xslt.Process -xml -in work/merge.xml -xsl stylesheet/build.xsl -out work/build.xml || \
export FAIL=1

echo Applying web site stylesheet
test -n "$FAIL" || \
java org.apache.xalan.xslt.Process -xml -in work/build.xml -xsl stylesheet/jakarta.xsl -out work/buildsite.xml || \
export FAIL=1

echo Generating build script
test -n "$FAIL" || \
java org.apache.xalan.xslt.Process -EDUMP -text -in work/buildsite.xml -xsl stylesheet/bash.xsl -out work/build.sh -PARAM cmd-prefix "$CP" -PARAM os-type "$OSTYPE" || \
export FAIL=1

# ********************************************************************

echo Generate crossreference data
test -n "$FAIL" || \
java org.apache.xalan.xslt.Process -xml -in work/merge.xml -xsl stylesheet/xref.xsl -out work/xref.xml || \
export FAIL=1

echo Applying web site stylesheet
test -n "$FAIL" || \
java org.apache.xalan.xslt.Process -xml -in work/xref.xml -xsl stylesheet/jakarta.xsl -out work/xrefsite.xml || \
export FAIL=1

echo Generating xref script
test -n "$FAIL" || \
java org.apache.xalan.xslt.Process -text -in work/xrefsite.xml -xsl stylesheet/bash.xsl -out work/xref.sh || \
export FAIL=1

# ********************************************************************

echo Generate script to publish all xml source files
test -n "$FAIL" || \
java org.apache.xalan.xslt.Process -text -in work/merge.xml -xsl stylesheet/bash.xsl -out work/puball.sh || \
export FAIL=1

echo Generate template for publishing an xml source file
test -n "$FAIL" || \
java org.apache.xalan.xslt.Process -xml -in work/merge.xml -xsl stylesheet/publish.xsl -out work/publish.xml || \
export FAIL=1

echo Applying web site stylesheet
test -n "$FAIL" || \
java org.apache.xalan.xslt.Process -xml -in work/publish.xml -xsl stylesheet/jakarta.xsl -out work/pubsite.xml || \
export FAIL=1

echo Generating publish script
test -n "$FAIL" || \
java org.apache.xalan.xslt.Process -text -in work/pubsite.xml -xsl stylesheet/bash.xsl -out work/publish.sh || \
export FAIL=1

echo Generate editing instructions
test -n "$FAIL" || \
java org.apache.xalan.xslt.Process -text -in work/merge.xml -xsl stylesheet/sedmap.xsl -out work/map.pl || \
export FAIL=1

echo Generate local deliver script
test -n "$FAIL" || \
java org.apache.xalan.xslt.Process -text -in work/merge.xml -xsl stylesheet/deliver-local.xsl -out work/deliver-local.sh || \
export FAIL=1

echo Generate naglist
test -n "$FAIL" || \
java org.apache.xalan.xslt.Process -text -in work/merge.xml -xsl stylesheet/nag.xsl -out work/naglist || \
export FAIL=1

echo Generate move instructions for cached files
test -n "$FAIL" || \
java org.apache.xalan.xslt.Process -xml -in work/merge.xml -xsl stylesheet/move.xsl -out work/move.xml || \
export FAIL=1

echo Generate move script for cached files
test -n "$FAIL" || \
java org.apache.xalan.xslt.Process -text -in work/move.xml -xsl stylesheet/bash.xsl -out work/move.sh || \
export FAIL=1

echo Generating publish script for javadocs
test -n "$FAIL" || \
java org.apache.xalan.xslt.Process -text -in work/merge.xml -xsl stylesheet/pubdocs.xsl -out work/pubdoc.pl || \
export FAIL=1

echo Generating publish script for junit reports
test -n "$FAIL" || \
java org.apache.xalan.xslt.Process -text -in work/merge.xml -xsl stylesheet/pubreport.xsl -out work/pubreport.pl || \
export FAIL=1

# **** publish ***
if test -z "$FAIL"; then
  echo
  echo Publishing $SOURCE
  cd work
  chmod +x *.sh
  bash puball.sh ../$SOURCE
  cd ..
fi

echo saving cached files
test -n "$FAIL" || bash work/move.sh

echo
echo Checking for local dependencies in $JSOURCE
echo
java -classpath "$CLASSPATH" LocalCheck $JSOURCE
echo

test -z "$FAIL" || echo "*** FAILED ***"

test -z "$FAIL" || exit 1
