#!/usr/bin/env bash
#
# blame(ken)
#
# this runs "mvn verify sonar:sonar" with appropriate environment, params, AWS credentials, etc...
#
# you must export SONAR_TOKEN in your default shell config from https://sonarcloud.io/account after joining Sonarqube organization "netfoundry"
#

set -e          # raise exception if error
set -u          # raise exception if unset
set -o pipefail # raise exceptions in pipes

BASENAME=$(basename $0)
WORKDIR=$(cd $(dirname $0);pwd -P;)
cd $WORKDIR

unset   AWS_SECURITY_TOKEN \
        AWS_SESSION_TOKEN \
        AWS_ACCESS_KEY \
        AWS_SECRET_KEY \
        AWS_ACCESS_KEY_ID \
        AWS_SECRET_ACCESS_KEY \
        AWS_SHARED_CREDENTIALS_FILE \
        AWS_CREDENTIAL_PROFILES_FILE

export  AWS_CREDENTIAL_PROFILES_FILE=$WORKDIR/aws-credentials \
        SPRING_PROFILES_ACTIVE=test

# if the standard var SONAR_TOKEN is null then try to find the token in
# a non-standard var SONAR_ACCESS_KEY
[[ ! -z ${SONAR_TOKEN:=${SONAR_ACCESS_KEY:-}} ]] || {
        echo "ERROR: SONAR_TOKEN is not defined"
        exit 1
}

[[ $# -gt 0 ]] && {
        echo "WARN: Passing Maven options: $*" >&2
}
mvn -B verify org.sonarsource.scanner.maven:sonar-maven-plugin:sonar \
    -Dsonar.projectKey=io.netfoundry:clientsync \
    -Dsonar.host.url=https://sonarcloud.io \
    -Dsonar.java.pmd.reportPaths="target/dependency-check-report-pmd.xml" \
    -Dsonar.branch.name="$(git rev-parse --abbrev-ref HEAD)" \
    -Dsonar.login="$SONAR_TOKEN" \

$@
