#!/bin/bash

# This script is a wrapper for calling the ANT with hostint tasks. 
# The ANT hostinit tasks replace some functions of the hostinit
# ccm command (ccm hostinit .....), implemented by 
# com/arsdigita/packaging/Hostinit.java class
# Specifically it copies addon packages into an existing aplaws
# installation and checks for prerequisites and consistency condition.

# Variables for directories.
# The path are relative to the bin directory of the CCM installation. This
# means that this script has to be executed from the bin directory. If you
# want to use this script from another directory, or include it into your 
# PATH, you have to adjust the paths.
CCM_LIB_DIR="../lib"
CCM_HOME_DIR="../.."

# We need CCM_REPO environment variable to access the source dir.
if [[ -z "$CCM_REPO" ]] ;  then
  echo CCM_REPO not set. Guessing ... 
  
  # If we are running Aplaws Appl Edition it is in base dir.
  if [ -d ../../../../ccm-addons ] 
    then
      #Convert to absolute path:
     CCM_REPO=`cd ../../../../ccm-addons; pwd`
      echo ===================================================
      echo Using CCM_REPO = $CCM_REPO
      echo If this doesn\'t work use
      echo "	export CCM_REPO=/path/to/repo     "
      echo to point us to the correct location and run 
      echo ccm-hostinit again or execute
      echo "	CCM_REPO=/path/to/repo ccm-hostinit  [task]  "
      echo again
      echo ===================================================
      echo
    else
      echo
      echo ===================================================
      echo Environment variable CCM_REPO not set. We need
      echo to know the location of the source repo directory.
      echo So use 
      echo "	export CCM_REPO=/path/to/repo     "
      echo to point us to the correct location and run 
      echo ccm-hostinit again or execute
      echo "	CCM_REPO=/path/to/repo sh ccm-hostinit  [task]  "
      echo again
      echo ===================================================
      echo
      exit 1
  fi
fi
#CATALINA_LIB_DIR="${CATALINA_HOME}/lib"

# Script logic starts here
# echo "checking if all paths are correct..."
files=$(ls ${CCM_LIB_DIR}/ccm-core*.jar 2> /dev/null | wc -l)
if [ "$files" == "0" ]
then
    echo "Error: CCM_LIB_DIR is invalid \(no ccm-core*.jar file\(s\) in CCM_LIB_DIR\)."
    exit 1
fi

if [ ! -d ${CCM_HOME_DIR}/WEB-INF ]
then
    echo "Error: CCM_HOME_DIR path is invalid \(no WEB-INF directory in CCM_HOME_DIR\)."
    exit 1
fi

#Convert to absolute path:
CCM_HOME=`cd ${CCM_HOME_DIR}; pwd`
ANT_HOME_DIR="./libexec/ant"
ANT_HOME=`cd ${ANT_HOME_DIR}; pwd`
export  ANT_HOME

TASK="$1"
shift

if [ "$1" = "--packagefile" ]
  then
    # Absolute path of current dir
    MY_DIR=`pwd`
    # Construct absolute (canonical) filename
    FILENAME=${MY_DIR}/$2
    exec libexec/ant/bin/ant  ${TASK} -quiet  -f libexec/build.xml -Dccm.home.dir=${CCM_HOME} -Dccm.repo.dir=${CCM_REPO} -Dccm.packages.filename="${FILENAME}"
  else
    PACKAGES="$@" 
    exec libexec/ant/bin/ant  ${TASK} -quiet  -f libexec/build.xml -Dccm.home.dir=${CCM_HOME} -Dccm.repo.dir=${CCM_REPO} -Dccm.packages="${PACKAGES}"
fi

