#!/bin/bash

DEFAULT_DIRS=(
	wlauto
	wlauto/external/daq_server/src/daqpower
)

target=$1

compare_versions() {
    if [[ $1 == $2 ]]; then
        return 0
    fi

    local IFS=.
    local i ver1=($1) ver2=($2)

    for ((i=${#ver1[@]}; i<${#ver2[@]}; i++)); do
        ver1[i]=0
    done

    for ((i=0; i<${#ver1[@]}; i++)); do
        if [[ -z ${ver2[i]} ]]; then
            ver2[i]=0
        fi
        if ((10#${ver1[i]} > 10#${ver2[i]})); then
            return 1
        fi
        if ((10#${ver1[i]} < 10#${ver2[i]})); then
            return 2
        fi
    done

    return 0
}

pylint_version=$(python -c 'from pylint.__pkginfo__ import version; print version')
compare_versions $pylint_version "1.5.1"
result=$?
if [ "$result" == "2" ]; then
	echo "ERROR: pylint version must be at least 1.5.1; found $pylint_version"
	exit 1
fi

THIS_DIR="`dirname \"$0\"`"
if [[ "$target" == "" ]]; then
	pushd $THIS_DIR/.. > /dev/null
	for dir in "${DEFAULT_DIRS[@]}"; do
		pylint --rcfile extras/pylintrc $dir
	done
	popd > /dev/null
else
	pylint --rcfile $THIS_DIR/../extras/pylintrc $target
fi
