#!/usr/bin/env bash

#
# Pre-commit Tools for Acquia Cloud projects.
#
# Downloads and configures tools that help standardise developer workflow. This
# script should be added to the root directory of a project and should be
# committed to the project to allow developers to quickly get setup.
#
# @see https://github.com/steveworley/commit-tools
#

# CONFIGURAITON:
# -------------------------------------------
UPSTREAM=master
TASKS=(code-review.sh tests.sh)
PREFIX=PROJ # Used for commit-msg.
LENGTH=15 # Used for commit-msg.
# -------------------------------------------

# Determine the root directory of the git repository.
ROOT="$( cd "$( git rev-parse --show-cdup )" && pwd )"

# Test if the setup.lock file exists and abort.
if [ -f "$ROOT/setup.lock" ]; then
  echo "Setup has been run"
  exit 0
fi

# Fetch composer.json from a GIST.
function fetch_composer_json() {
  echo "Fetching composer.json"
  wget -O $1/composer.json https://raw.githubusercontent.com/steveworley/commit-tools/master/composer.json
}

# Update configuration when setup is run.
#
# First run of this program when command line arguments are passed in should
# update the configuration variables such that subsequent runs of setup for a
# project should not need to know the configurations for the project.
#
# @param $1 PATTERN
#   sed regex compatible string / delimiters
# @param $2 REPLACE
#   sed regex compatible string / delimiters
# @param $3 LINE
#   Line number to be used to limit the replacement.
function update_config() {
  file="$( cd "$( git rev-parse --show-cdup )" && pwd )/$( basename $0)"
  sed -i '' -e "${3}s/$1/$2/g" "$file"
}

# Test to see if the directory structure matches the Acquia Cloud expectation.
if [ ! -d "$ROOT/docroot" ]; then
  echo "Unable to locate Drupal. This needs to be run from an Acquia directory configuration."
  exit 1
fi

# UPSTREAM has been passed into the program
if [ $1 ] && [ "$UPSTREAM" == "master" ]; then
  UPSTREAM=$1
  update_config "UPSTREAM=master" "UPSTREAM=$UPSTREAM" 15
fi

# LENGTH has been passed into the program
if [ $2 ] && [ "$PREFIX" == 'PROJ' ]; then
  PREFIX=$2
  update_config "PREFIX=PROJ" "PREFIX=$PREFIX" 17
fi

# LENGTH has been passed into the program
if [ $3 ] && [ "$LENGTH" == 15 ]; then
  LENGTH=$3
  update_config "LENGTH=15" "LENGTH=$LENGTH" 18
fi

# Test php is available (should be if terminal is opened with DD)
php -v > /dev/null 2>&1
PHP_IS_INSTALLED=$?

if [ $PHP_IS_INSTALLED -ne 0 ]; then
  echo "Unable to find PHP. Please run from terminal with php on path"
  exit 1
fi

# Test to see if composer.json has been downloaded.
if [ ! -f "$ROOT/composer.json" ] || [ ! -s "$ROOT/composer.json" ]; then
  fetch_composer_json $ROOT
fi

# Update the gitignore.
IGNORE_VENDOR="vendor"
IGNORE_COMPOSER="composer.phar"
IGNORE_LOCK="setup.lock"
echo "" >> "$ROOT/.gitignore"
echo "$IGNORE_VENDOR" >> "$ROOT/.gitignore"
echo "$IGNORE_COMPOSER" >> "$ROOT/.gitignore"
echo "$IGNORE_LOCK" >> "$ROOT/.gitignore"

# Test if Composer is installed
if [ ! -f "$ROOT/composer.phar" ]; then
  echo "Installing composer..."
  curl -sS https://getcomposer.org/installer | php;
fi

# Test if Composer is installed
$ROOT/composer.phar -v > /dev/null 2>&1
COMPOSER_IS_INSTALLED=$?

if [ $COMPOSER_IS_INSTALLED -ne 0 ]; then
  # Composer could not be installed... prompt to install manually and retry.
  echo "Unable to install Composer. Make sure composer.phar is available from project root."
  exit 1
fi

# Test to see if dependencies have been installed.
if [ ! -d "$ROOT/vendor" ]; then
  echo "Installing dependencies..."
  $ROOT/composer.phar install

  # Register Drupal CS standards.
  PHPCS="$ROOT/vendor/bin/phpcs"
  $PHPCS --config-set installed_paths $ROOT/vendor/drupal/coder/coder_sniffer
fi

# Precommit hook.
PRECOMMIT=$ROOT/.git/hooks/pre-commit
if [ ! -f $PRECOMMIT ]; then
  echo "Setting up git hooks..."

  # Fetch the pre-commit bootstrap script and set permissions.
  wget -O $PRECOMMIT https://raw.githubusercontent.com/steveworley/commit-tools/master/pre-commit
  sed -i '' -e "s|UPSTREAM=master|UPSTREAM=$UPSTREAM|g" "$PRECOMMIT"
  chmod +x $PRECOMMIT
fi

# Precommit tasks.
TASKDIR=$ROOT/.git/hooks/tasks
if [ ! -d $TASKDIR ]; then
  mkdir $TASKDIR
  # List of commands that we want to include as hooks to run for this project
  GIT_URL=https://raw.githubusercontent.com/steveworley/commit-tools/master/tasks

  for task in ${TASKS[@]}
  do
    wget -O "$TASKDIR/$task" "$GIT_URL/$task"
  done
fi

# Add commit message validation.
COMMITMSG=$ROOT/.git/hooks/commit-msg
if [ ! -f $COMMITMSG ]; then
  # Download the commit message hook.
  wget -O $COMMITMSG https://raw.githubusercontent.com/steveworley/commit-tools/master/commit-msg

  # Update with project settings.
  sed -i '' -e "s|LENGTH=#replace#|LENGTH=$LENGTH|g" "$COMMITMSG"
  sed -i '' -e "s|PREFIX=#replace#|PREFIX=$PREFIX|g" "$COMMITMSG"
  chmod +x $COMMITMSG
fi

# Complete!
echo "Setup has finished."
touch $ROOT/setup.lock
exit 0