#!/usr/bin/env bash
# $Id: drush,v 1.6 2009/05/24 16:10:36 grugnog Exp $
#
# This script is a simple wrapper that will run Drush with the most appropriate
# php executable it can find.
#

# Get the absolute path of this executable
ORIGDIR=$(pwd)
SELF_PATH=$(cd -P -- "$(dirname -- "$0")" && pwd -P) && SELF_PATH=$SELF_PATH/$(basename -- "$0")

# Resolve symlinks - this is the equivalent of "readlink -f", but also works with non-standard OS X readlink.
while [ -h $SELF_PATH ]; do
    # 1) cd to directory of the symlink
    # 2) cd to the directory of where the symlink points
    # 3) Get the pwd
    # 4) Append the basename
    DIR=$(dirname -- "$SELF_PATH")
    SYM=$(readlink $SELF_PATH)
    SELF_PATH=$(cd $DIR && cd $(dirname -- "$SYM") && pwd)/$(basename -- "$SYM")
done
cd $ORIGDIR

# Build the path to drush.php.
SCRIPT_PATH=$(dirname $SELF_PATH)/drush.php

# If it is not exported determine and export the number of columns.
if [ -z $COLUMNS ]; then
  export COLUMNS=$(tput cols)
fi

# Special case for MAMP, since it is not the default php on a vanilla install.
if [ -f /Applications/MAMP/bin/php5/bin/php ]; then
  /Applications/MAMP/bin/php5/bin/php $SCRIPT_PATH "$@"
else
  # We check for a command line (cli) version of php, and if found use that.
  /usr/bin/env php-cli -v &> /dev/null
  if [ "$?" = 0 ] ; then
    /usr/bin/env php-cli $SCRIPT_PATH "$@"
  else
    # Alternatively we run with straight php, which works on most other systems.
    /usr/bin/env php $SCRIPT_PATH "$@"
  fi
fi
