#!/usr/bin/env bash
###################################################################
#
# PHP QA Pipeline
#
# Usage:
#
# Standard:
# ./bin/qa
#
# Run all PHPUnit tests:
# phpUnitQuickTests=0 ./bin/qa

SOURCE="${BASH_SOURCE[0]}"
while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink
  DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
  SOURCE="$(readlink "$SOURCE")"
  [[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE" # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located
done
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
cd $DIR;
set -e
set -u
set -o pipefail
standardIFS="$IFS"
IFS=$'\n\t'
echo "
===========================================
$(hostname) $0 $@
===========================================
"


#### OPTIONS ###################################################

source ./../includes/options.inc.bash


#### FUNCTIONS #################################################

phpBinPath=$(which php)
source ./../includes/functions.inc.bash


#### CONFIG ##################################################

# If a CI variable is set, we use that, otherwise default to false.
# Travis-CI sets a CI variable. You can eaisly set this in any other CI system
# The value should the the string 'true' if this is CI
CI=${CI:-'false'}

# project root directory
if [[ -d $DIR/../vendor ]]
then
    projectRoot="$(readlink -f $DIR/../)"
else
    projectRoot="$(readlink -f $DIR/../../../../)"
fi

# the path in the project to check for config
projectConfigPath="$projectRoot/qaConfig/"

# Which platform is the code to be tested
platform="$(detectPlatform)"
echo "$platform platform detected"

echo "

Checking for Xdebug
-------------------
"
if [[ "" == "$($phpBinPath -i | grep xdebug)" ]]
then
    xdebugEnabled=0
    echo "Xdebug is not enabled - infection and coverage not available"
else
    xdebugEnabled=1
    echo "Xdebug is enabled"
fi


echo "

Setting Project Paths
---------------------
"
runTool setPaths

echo "

Setting phpqa Config
--------------------
"
runTool setConfig

if [[ "$specifiedPath" != "" ]]
then
    echo "Only scanning the specified path $specifiedPath"
    pathsToCheck=()
    pathsToCheck+=($projectRoot/$specifiedPath)
fi

#### PRE PROCESS #############################################

hasBeenRestarted="false"

# override configs with project configs
overridePath="$projectConfigPath/qaConfig.inc.bash"

if [[ -f "$overridePath" ]]
then
    echo "Found override config at $overridePath"
    source $overridePath
fi

cd "$projectRoot";

runTool prepareDirectories


if [[ -f $projectConfigPath/hookPre.bash ]]
then
    echo "

Running Pre Hook $projectConfigPath/hookPre.bash
------------------------------------------------
"
    source $projectConfigPath/hookPre.bash
fi

#### PROCESS #################################################

if [[ "" != "$singleToolToRun" ]]
then
    echo "

Running Single Tool: $singleToolToRun
------------------------
"
    runTool "$singleToolToRun"
    exit $?
fi

echo "

Running All Linting Tools
=========================

"

runTool allLintingTools

echo "

Running All Static Analysis Tools
=================================

"
runTool allStaticAnalysisTools


echo "

Checking if Uncommitted Changes
------------------------------
"
checkForUncommittedChanges $specifiedPath

echo "

Running All Coding Standards Tools (That Might Make Code Changes)
=================================

"
runTool allCodingStandardsTools


echo "

Running All Testing Tools
=========================

"
runTool allTestingTools


echo '


###############################
#                             #
#      ALL TESTS PASSING      #
#                             #
#            _                #
#           /(|               #
#          (  :               #
#         __\  \  _____       #
#       (____)  `|            #
#      (____)|   |            #
#       (____).__|            #
#        (___)__.|_____       #
#                             #
#                             #
###############################


'
echo "
Statistics:
"
runTool phploc
set +x

if [[ -f $projectConfigPath/hookPost.bash ]]
then
    echo "

Running Post Hook $projectConfigPath/hookPost.bash
------------------------------------------------
"
    source $projectConfigPath/hookPost.bash
fi

if [[ "false" != "$hasBeenRestarted" ]]
then
    echo "

####################################################################

WARNING - RAN WITH RETRIES

As you have restarted steps in this run,
you should run the whole process again to be sure everything is fine

####################################################################
"
fi

echo "
===========================================
$(hostname) $0 $@ COMPLETED
===========================================
"
