#!/bin/sh
#
# @author René Schwaiger <sanssecours@me.com>
# @brief Reformats Shell source code
# @date 07.09.2018
# @tags reformat

SCRIPTS_DIR=$(dirname "$0")
. "${SCRIPTS_DIR}/include-common"

cd "$SOURCE"

if ! which shfmt > /dev/null; then
	printf 2>&1 'Please install `shfmt`\n'
	exit 1
fi

shell_files=$(shfmt -f 'scripts' 'tests/shell')
non_ignored=$(printf "%s\n" "$shell_files" | sed -nE 's/(.*)/'"'"'\1'"'"'/p' | xargs git check-ignore -vn | sed -nE 's/^::[[:blank:]]*(.*)$/\1/p')
printf "%s\n" "$non_ignored" | sed -nE 's/(.*)/'"'"'\1'"'"'/p' | xargs shfmt -s -sr -w
