#!/bin/sh
# Wrapper around checkbashisms/shellcheck/whatever other shell checkers I can find
#
# Copyright (C) 2016 Austin English
#
# This software comes with ABSOLUTELY NO WARRANTY.
#
# This is free software, placed under the terms of the GNU Lesser
# Public License version 2.1 (or later), as published by the Free
# Software Foundation. Please see the file COPYING for details.

set -e
set -x

w_die() {
    echo "$* failed"
    exit 1
}

w_try() {
    "$@"
    status=$?
    if test $status -ne 0
    then
        w_die "Note: command $* returned status $status.  Aborting."
    fi
}

if ! command checkbashisms > /dev/null 2>&1 ; then
    w_die "checkbashisms must be installed!"
elif ! command shellcheck --version > /dev/null 2>&1 ; then
    w_die "shellcheck must be installed!"
elif [ ! -f Makefile ] ; then
    w_die "$0 should be run from the top of the source tree"
fi

# Different versions can give different results:
echo "======================== Begin checkbashisms version info ==========================="
checkbashisms --version
echo "======================== End checkbashisms version info ==========================="

echo "======================== Begin shellcheck version info ==========================="
shellcheck --version
echo "======================== End shellcheck version info ==========================="

# Use git ls-files if available, this prevents 'finding' scripts that aren't checked into git.
# E.g., if patching foo fails, then foo.orig would also be 'found'.
# The find fallback is for non git users, e.g., distros packaging winetricks or end users
# running shell-checks from a tarball download.
if [ -d .git ] ; then
    files_to_check="$(git ls-files | xargs file | grep -e 'POSIX shell script' | cut -d : -f1)"
else
    files_to_check="$(find . -type f -exec file {} \; | grep -e 'POSIX shell script' | cut -d : -f1)"
fi

for shellscript in $files_to_check
do
    echo "Checking ${shellscript} for bashisms:"
    w_try checkbashisms --posix "${shellscript}"

    echo "Checking ${shellscript} with shellcheck:"
    w_try shellcheck -s sh "${shellscript}"
done
