#!/usr/bin/env bash
# Licensed to the StackStorm, Inc ('StackStorm') under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License.  You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

#
# Script which runs pylint on all the Python files in a particular pack.
#

if hash greadlink 2>/dev/null; then
    SCRIPT_PATH=$(dirname "$(greadlink -f "$0")")
else
    SCRIPT_PATH=$(dirname "$(readlink -f "$0")")
fi

source ${SCRIPT_PATH}/common.sh

if [ $# -ne 1 ]; then
    echo "Usage: ${0} <path to pack directory>"
    exit 1
fi

# If "1" it will be assumed various dependencies are already installed and won't
# be installed again (e.g. when running on StackStorm Exchange Circle CI)
ST2_INSTALL_DEPS=${ST2_INSTALL_DEPS:-1}

PACK_DIR=$1
PACK_NAME=$(basename ${PACK_DIR})

if [ ! -d ${PACK_DIR} ]; then
    echo "Pack directory \"${PACK_DIR}\" doesn't exist"
    exit 2
fi

PACK_REQUIREMENTS_FILE="${PACK_DIR}/requirements.txt"
PACK_TESTS_REQUIREMENTS_FILE="${PACK_DIR}/requirements-tests.txt"
PYTHON_BINARY=`which python`

# Note: We assume this script is running inside a virtual environment into which we install the
# the pack dependencies. This way pylint can also correctly introspect all the dependency,
if [[ ${PYTHON_BINARY} != *"virtualenv/bin/python" ]]; then
    echo "Script must run under a virtual environment which is created in the Make target"
    exit 2
fi

ST2_REPO_PATH=${ST2_REPO_PATH:-/tmp/st2}
ST2_COMPONENTS=$(get_st2_components)
# Add in actions/lib directory to account for "packs.enable_common_libs" config option
PACK_PYTHONPATH="$(join ":" ${ST2_COMPONENTS}):${PACK_DIR}/actions/lib"

echo "Running pylint on pack: ${PACK_NAME}"

if [ ! -d "${PACK_DIR}/actions" -a ! -d "${PACK_DIR}/sensors" -a ! -d "${PACK_DIR}/etc" ]; then
    echo "skipping pack without any actions, sensors and 3rd party scripts"
    exit 0
fi

PYTHON_FILE_COUNT=$(find ${PACK_DIR}/* -maxdepth 1 -name "*.py" -type f | wc -l)

if [ "${PYTHON_FILE_COUNT}" == "0" ]; then
    echo "Skipping pack with no Python files"
    exit 0
fi

if [ "${ST2_INSTALL_DEPS}" = "1" ]; then
    # Install per-pack dependencies
    pip install --cache-dir ${HOME}/.pip-cache -q -r ${REQUIREMENTS_DIR}requirements-dev.txt

    # Install test dependencies
    pip install --cache-dir ${HOME}/.pip-cache -q -r ${REQUIREMENTS_DIR}requirements-pack-tests.txt

    # Install pack dependencies
    if [ -f ${PACK_REQUIREMENTS_FILE} ]; then
        echo "Installing pack requirements from ${PACK_REQUIREMENTS_FILE}..."
        pip install --cache-dir ${HOME}/.pip-cache -q -r ${PACK_REQUIREMENTS_FILE}
    fi

    # Install pack test dependencies (if any)
    if [ -f ${PACK_TESTS_REQUIREMENTS_FILE} ]; then
        echo "Installing pack test requirements from ${PACK_TESTS_REQUIREMENTS_FILE}..."
        pip install --cache-dir ${HOME}/.pip-cache -q -r ${PACK_TESTS_REQUIREMENTS_FILE}
    fi
fi

export PYTHONPATH=${PACK_PYTHONPATH}:${PYTHONPATH}

echo "PYTHONPATH=${PYTHONPATH}"
echo "PYTHON_BINARY=${PYTHON_BINARY}"

if [ -d ${PACK_DIR}/actions ]; then
    ACTION_PYTHON_FILES_COUNT=$(find ${PACK_DIR}/actions -name "*.py" -printf "." -print0 | wc -c)
else
    ACTION_PYTHON_FILES_COUNT=0
fi

if [ ${ACTION_PYTHON_FILES_COUNT} -gt 0 ]; then
    find ${PACK_DIR}/actions -name "*.py" -print0 | xargs -0 ${PYTHON_BINARY} -m pylint -E --rcfile=${CONFIG_DIR:-./lint-configs/}python/.pylintrc && echo "--> No pylint issues found in actions." || exit $?
fi

if [ -d ${PACK_DIR}/sensors ]; then
    SENSOR_PYTHON_FILES_COUNT=$(find ${PACK_DIR}/sensors -name "*.py" -printf "." -print0 | wc -c)
else
    SENSOR_PYTHON_FILES_COUNT=0
fi

if [ ${SENSOR_PYTHON_FILES_COUNT} -gt 0 ]; then
    find ${PACK_DIR}/sensors -name "*.py" -print0 | xargs -0 ${PYTHON_BINARY} -m pylint -E --rcfile=${CONFIG_DIR:-./lint-configs/}python/.pylintrc && echo "--> No pylint issues found in sensors." || exit $?
fi

if [ -d ${PACK_DIR}/etc ]; then
    ETC_PYTHON_FILES_COUNT=$(find ${PACK_DIR}/etc -name "*.py" -printf "." -print0 | wc -c)
else
    ETC_PYTHON_FILES_COUNT=0
fi

if [ ${ETC_PYTHON_FILES_COUNT} -gt 0 ]; then
    find ${PACK_DIR}/etc -name "*.py" -print0 | xargs -0 ${PYTHON_BINARY} -m pylint -E --rcfile=${CONFIG_DIR:-./lint-configs/}python/.pylintrc && echo "--> No pylint issues found in etc." || exit $?
fi
