#!/bin/bash

#set -x

# Function to help us shorten classpath by finding relative path to jar file
# If it backs up to the root ('/') bail and return the absolute path.
relpath()
{
   local s d b
   s=$(cd ${1%%/};pwd); d=$(cd $2;pwd); b=;
   while [ "${d#$s/}" == "${d}" ] && [ "$s" != "/" ]
      do s=$(dirname $s); b="../${b}"; done;
   if [ "$s" == "/" ]; then echo $2; else echo ${b}${d#$s/}; fi
}

# defaults
cfgdir=$(pwd)
fwdlib="p2j.jar"
appname="hotel"

# First parameter is appname, if passed. Otherwise the default above
if [ ".$1." != ".." ]; then
   appname="$1"
fi
appjar="${appname}.jar"

# Second parameter is the path to the config, if passed. Otherwise the default above
# It is where the script that called this helper resides.
if [ ".$2." != ".." ]; then
   cfgdir="$(cd "$(dirname "$2")"; pwd)/$(basename "$2")"
fi

# Optional AppCDS (client) mode: when invoked with --appcds[=<type>] we use the
# curated classpath published by deploy_appcds.sh next to the <type>.jsa archive
# (see appcds/client/<type>.cpath). That file holds the exact classpath the
# archive was dumped against, so returning it verbatim keeps the client runtime
# classpath identical to the archive's. Only the client (ClientDriver) passes
# --appcds; the server (FwdLauncher) does not, so it keeps using the full FWD
# library set located by the rules below. If no curated file is found we fall
# through to those same rules.
appcds_mode=
appcds_type="default"
for arg in "$@"; do
   case "$arg" in
      --appcds)   appcds_mode=true ;;
      --appcds=*) appcds_mode=true; appcds_type="${arg#--appcds=}" ;;
   esac
done
if [ -n "$appcds_mode" ]; then
   for cand in "/opt/${appname}/appcds/client/${appcds_type}.cpath" \
               "${cfgdir}/../appcds/client/${appcds_type}.cpath"; do
      if [ -f "$cand" ]; then
         appcds_cpath=$(cat "$cand")
         if [ -n "$appcds_cpath" ]; then
            # Terminate with a colon to match the non-AppCDS output below.
            echo "${appcds_cpath}:"
            exit 0
         fi
      fi
   done
fi

# Setup classpath. First get the FWD jars and libraries, then the application's.
# For FWD, use p2j.jar as the determining factor. Rules:
# 1) Look for FWD_LIB, honor build directory
# 2) One directories above the cfgdir and in the lib directory from there ($cfgdir/../lib)
# 3) Two directories above our current directory, and in the p2j/lib directory from there (../../p2j/lib)
# 4) Two directories above our current directory, and in the p2j/build/lib directory from there (../../p2j/build/lib)
# 5) A well-known location
# Use the canonical form.
if [ -n "$FWD_LIB" ]; then
   #echo "Rule 1" > /tmp/x.x
   use_fwd_lib=true
   if [ -f "${FWD_LIB}/build/lib/${fwdlib}" ]; then
      full_fcpath=${FWD_LIB}/build/lib
   else
      full_fcpath=${FWD_LIB}/lib
   fi
elif [ -f "${cfgdir}/../lib/${fwdlib}" ]; then
   #echo "Rule 2" >> /tmp/x.x
   full_fcpath=$(cd "${cfgdir}/../lib"; pwd)
elif [ -f "../../p2j/lib/${fwdlib}" ]; then
   #echo "Rule 3" >> /tmp/x.x
   full_fcpath=$(cd "../../p2j/lib"; pwd)
elif [ -f "../../p2j/build/lib/${fwdlib}" ]; then
   #echo "Rule 4" >> /tmp/x.x
   full_fcpath=$(cd "../../p2j/build/lib"; pwd)
elif [ -f "/opt/fwd/lib/${fwdlib}" ]; then
   #echo "Rule 5" >> /tmp/x.x
   full_fcpath="/opt/fwd/lib"
else
   echo "Unable to locate FWD libraries. Is FWD_LIB set? Exiting."
   exit 1
fi
#echo "full_fcpath=$full_fcpath" >> /tmp/x.x

# Find jars, and attempt to use relative paths so as to keep it as short as possible unless FWD_LIB is specified.
[ -z "$use_fwd_lib" ] && fwdjars=$(relpath $(pwd) $full_fcpath) || fwdjars=$full_fcpath

#echo "Adding FWD jars in" $fwdjars
for each in $(find $fwdjars/ -name "*.jar"|sort);
do
   if [ -z $cpath ]; then
      cpath=$each
   else
      cpath=$cpath:$each
   fi
done

# Now look for application libraries.
# Get a relative path, so as to keep the classpath as short as possible
# Start with one directory up, then ../lib, then ../../lib or the cfgpath
if [ -d "../lib" ]; then
   full_cpath=$(cd "../lib"; pwd)
elif [ -d "../../lib" ]; then
   full_cpath=$(cd "../../lib"; pwd)
else
   full_cpath=${cfgdir}
fi
if [ -f ${full_cpath}/${appjar} ]; then
   full_cpath=$full_cpath
elif [ -f "${cfgdir}/../lib/${appjar}" ]; then
   full_cpath=$(cd "${cfgdir}/../lib"; pwd)
elif [ -f "${cfgdir}/../../lib/${appjar}" ]; then
   full_cpath=$(cd "${cfgdir}/../../lib"; pwd)
elif [ -f "/opt/${appname}/lib/${appjar}" ]; then
   full_cpath="/opt/${appname}/lib"
else
   echo "Unable to locate $appname libraries. Exiting."
   exit 1
fi
#echo "full_cpath=$full_cpath" >> /tmp/x.x
rel_path=$(relpath $(pwd) $full_cpath)
#echo "rel_path=$rel_path" >> /tmp/x.x

if [ "$fwdjars" != "$rel_path" ]; then
   #echo "Adding application jars in $rel_path since fwdjars=$fwdjars" >> /tmp/x.x
   for each in $(find $rel_path/ -name "*.jar");
   do
      cpath=$each:$cpath
   done
#else
#   echo "Didn't add application jars in $rel_path since fwdjars=$fwdjars" >> /tmp/x.x
fi
# Terminate with a colon
cpath=${cpath}:

echo $cpath

