#!/bin/sh
# Shell wrapper for PHOCOA
#
# This script will do the following:
# - check for PHING_COMMAND env, if found, use it.
#   - if not found assume php is on the path
# - check for PHOCOA_HOME env, if found use it
#   - if not look for it
PHOCOA_RUN_FROM_DIR=`pwd`

# determine PHOCOA_HOME
if [ -z "$PHOCOA_HOME" ] ; then

  PEAR_INSTALL_DIR="@pear_directory@"
  if [ ${PEAR_INSTALL_DIR:0:15} == "@pear_directory" ] ; then
    # is not pear install
    # we never really expect PHOCOA_HOME to be set... we get this info from the path to this executable
    # echo "WARNING: PHOCOA_HOME environment not set. Attempting to guess."

    # try to find PHOCOA
    
    ## resolve links - $0 may be a link to phing's home
    PRG="$0"
    progname=`basename "$0"`

    # need this for relative symlinks
    dirname_prg=`dirname "$PRG"`
    cd "$dirname_prg"
    
    while [ -h "$PRG" ] ; do
      ls=`ls -ld "$PRG"`
      link=`expr "$ls" : '.*-> \(.*\)$'`
      if expr "$link" : '/.*' > /dev/null; then
          PRG="$link"
      else
          PRG=`dirname "$PRG"`"/$link"
      fi
    done
    cd "$PHOCOA_RUN_FROM_DIR"
    
    PHOCOA_HOME=`dirname "$PRG"`/..

    # make it fully qualified
    PHOCOA_HOME=`cd "$PHOCOA_HOME" && pwd`
  else
    # is a pear install
    PHOCOA_HOME="@pear_directory@/phocoa"
  fi
  
  # make it available in PHP via getenv("PHOCOA_HOME")
  export PHOCOA_HOME
fi
# echo "PHOCOA_HOME: $PHOCOA_HOME"

# determine if we're running from a phocoa project dir or "general" (for creating new projects)
# starting with the current directory, go "up" looking for ./conf/webapp.conf. If never found, we're in "general" mode, identified by PHOCOA_PROJECT_CONF=""
while [ `pwd` != '/' ]; do
    if [ -r './conf/webapp.conf' ]; then
        if [ -n "$PHOCOA_PROJECT_CONF" ] && [ "$PHOCOA_PROJECT_CONF" != "`pwd`/conf/webapp.conf" ]; then
            echo "FATAL: PHOCOA_PROJECT_CONF in ENV ($PHOCOA_PROJECT_CONF) is outside of the current project."
            echo "Do you want use the current project's PHOCOA_PROJECT_CONF? [yes]"
            read input
            if [ -z "$input" ] || [ $input == "yes" ]; then
                PHOCOA_PROJECT_CONF=""
            else
                echo "Exiting..."
                exit 1
            fi
        fi
        PHOCOA_PROJECT_CONF="`pwd`/conf/webapp.conf"
        echo "Simulating:\nexport PHOCOA_PROJECT_CONF=${PHOCOA_PROJECT_CONF}"
        break
    fi
    cd ..
done
cd "$PHOCOA_RUN_FROM_DIR"
if [ -z "$PHOCOA_PROJECT_CONF" ]; then
    PHOCOA_PROJECT_CONF=
fi
if [ -z "$PHOCOA_PROJECT_CONF" ]; then
    # echo "PHOCOA_PROJECT_CONF: none"
    PHOCOA_PROJECT_DIR=
    PHOCOA_PROJECT_NAME=
else
    # echo "PHOCOA_PROJECT_CONF: $PHOCOA_PROJECT_CONF"
    # determine phocoa project dir
    PD=`dirname $PHOCOA_PROJECT_CONF`/..
    PHOCOA_PROJECT_DIR=`cd "$PD" && pwd`
    PHOCOA_PROJECT_NAME=`basename $PHOCOA_PROJECT_DIR`
    export PHOCOA_PROJECT_CONF=$PHOCOA_PROJECT_CONF
    # echo "PHOCOA_PROJECT_DIR: $PHOCOA_PROJECT_DIR"
fi


if (test -z "$PHING_COMMAND") ; then
    # echo "WARNING: PHING_COMMAND environment not set. (Assuming phing on PATH)"
    export PHING_COMMAND="phing"
fi

# if no phing target entered, do -list rather than execute the default target
PHOCOA_CMD=$*
if [ -z "$PHOCOA_CMD" ]; then
    PHOCOA_CMD=-list
fi

CMD="$PHING_COMMAND -f $PHOCOA_HOME/phing/build.xml -Dusing.phocoa.make=true -Dphocoa.pwd=$PHOCOA_RUN_FROM_DIR -Dphocoa.dir=$PHOCOA_HOME -Dphocoa.project.name=$PHOCOA_PROJECT_NAME -Dphocoa.project.dir=$PHOCOA_PROJECT_DIR $PHOCOA_CMD"
echo $CMD
$CMD
