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

# Usage: make_classpath DIR
# Finds jars in lib/, and class files in classes/ and build/classes
# If CP is already set, include that as well.

DIRROOT="$1"

if [ "$DIRROOT" = "" ]
then
    echo "No directory given" 1>&2
    exit 1
    fi

# remove any trailing /
DIRROOT=${DIRROOT%/}

LIBDIR="$DIRROOT/lib"
# List
CPDIR1="$DIRROOT/classes"
CPDIR2="$DIRROOT/target/classes"
CPDIR3="$DIRROOT/target/classes-eclipse"
ETCDIR="$DIRROOT/etc"

# Cygwin - on Windows, the Java separator is ;
# Alternative: Form in UNIX style, turn into windows form at the end.

CYGWIN=0
SEP=':'
if [ "$OSTYPE" = "cygwin" ]
then
    CYGWIN=1
    SEP=';'
    fi

# CP is the variable collecting the path/
# It may already have a value.

CP="${CP:-}"

# Append any jars in the lib/ directory

for jar in "$LIBDIR"/*.jar
  do
  # Check for no expansion
  [ -e "$jar" ] || break
  # Check not sources and no javadoc.
  [ "${jar/sources}" = "${jar}" ] || continue
  [ "${jar/javadoc}" = "${jar}" ] || continue
  #echo "Path: $jar"
  [ "$CP" != "" ] && CP="${CP}${SEP}"
  CP="${CP}$jar"

##  # Suggested:
##   if [ "$CYGWIN" = 1 ]
##   then
##     CP="${CP}$(cygpath -wp "$jar")"
##   else
##     CP="${CP}$jar"
##   fi
## 
done

# Prepend any classes/ directory
# As it's "prepend", we need to do it in reverse.
for dir in "$CPDIR3" "$CPDIR2" "$CPDIR1"
do
  if [ -e "$dir" ]
  then
      [ "$CP" != "" ] && CP="${SEP}${CP}"
      CP="${dir}$CP"
  fi
done

# Add DIRROOT
#CP="${CP}${SEP}${DIRROOT}"

## if [ "$CYGWIN" = 1 ]
## then
##     CP="$(cygpath -w "$CP")"
##     fi

echo "$CP"
