#!/usr/bin/env bash
#
# Summary: Install a PHP version using the php-build plugin
#
# Usage: phpenv install [--ini|-i <environment>] <version>
#        phpenv install [--ini|-i <environment>] <definition-file>
#        phpenv install -l|--list
#        phpenv install -V|--version
#
#  -l/--list     List all available versions
#  -V/--version  Show version of php-build
#
# For detailed information on installing PHP versions with
# php-build, including a list of environment variables for adjusting
# compilation, see: https://github.com/php-build/php-build
#
set -e
[ -n "$PHPENV_DEBUG" ] && set -x

# Provide phpenv completions
if [ "$1" = "--complete" ]; then
  echo --list
  echo --version
  exec php-build --definitions
fi

usage() {
  phpenv-help install 2>/dev/null
  [ -z "$1" ] || exit "$1"
}

definitions() {
  local query="$1"
  php-build --definitions | $(type -p ggrep grep | head -1) -F "$query" || true
}

indent() {
  sed 's/^/  /'
}

if [ -z "$PHPENV_ROOT" ]; then
  PHPENV_ROOT="${HOME}/.phpenv"
fi

if [[ "$1" == '--help' ]] || [[ "$1" == '-h' ]]; then
  usage 0
fi

if [[ "$1" == '--list' ]] || [[ "$1" == '-l' ]]; then
  echo "Available versions:"
  definitions | indent
  exit
fi

if [[ "$1" == '--version' ]] || [[ "$1" == '-V' ]]; then
  exec php-build --version
fi

ENVIRONMENT="production"
if [[ "$1" == '--ini' ]] || [[ "$1" == '-i' ]]; then
  ENVIRONMENT="$2"
  shift 2
fi

DEFINITION="$1"
case "$DEFINITION" in
"" | -* )
  usage 1
  ;;
esac

VERSION_NAME="${DEFINITION##*/}"
PREFIX="${PHPENV_ROOT}/versions/${VERSION_NAME}"

php-build --ini "$ENVIRONMENT" "$DEFINITION" "$PREFIX"
phpenv rehash
