#!/usr/bin/env bash
IFS=$'\n\t'
set -euo pipefail

# Run code linting in a typical GovCMS Drupal project.
# Pass in path to lint, eg ./vendor/bin/lint web/modules/custom.

# Allow path override.
APP_DIR="${APP_DIR:-$PWD}"

# Skip if parent folder does not exist
if [[ ! -d ${APP_DIR}/$* ]]
then
  echo "[info]: Cannot lint ${APP_DIR}/$* - does not exist"
  exit 0
fi

# Lint code.
touch "${APP_DIR}"/"$*"/index.php # Avoid "no files found" error. @see https://github.com/JakubOnderka/PHP-Parallel-Lint/issues/108
"${APP_DIR}"/tests/vendor/bin/parallel-lint --exclude ./tests/vendor -e php,inc,module,theme,install,profile,test "${APP_DIR}"/"$*"
rm "${APP_DIR}"/"$*"/index.php

# Check code standards.
"${APP_DIR}"/tests/vendor/bin/phpcs --standard="${APP_DIR}"/tests/phpcs.xml "${APP_DIR}"/"$*"

# Check code mess.
"${APP_DIR}"/tests/vendor/bin/phpmd "${APP_DIR}"/"$*" text codesize,unusedcode,cleancode
