#!/usr/bin/env sh

set -eu

if command -v shellcheck >/dev/null ; then
    exec shellcheck "$@"
fi

scversion=v0.8.0

bindir=$( cd "${0%/*}" && pwd )
targetbin=$( cd "$bindir"/.. && pwd )/target/bin
scbin=$targetbin/.shellcheck-$scversion
if [ ! -f "$scbin" ]; then
  if [ "$(uname -s)" = Darwin ]; then
    file=darwin.x86_64.tar.xz
  elif [ "$(uname -o)" = Msys ]; then
    # TODO: work on windows
    file=zip
  else
    case $(uname -m) in
      x86_64) file=linux.x86_64.tar.xz ;;
      arm) file=linux.aarch64.tar.xz ;;
    esac
  fi

  mkdir -p "$targetbin"
  "$bindir"/scurl "https://github.com/koalaman/shellcheck/releases/download/$scversion/shellcheck-${scversion?}.$file" | tar -OxJv "shellcheck-${scversion}/shellcheck" > "$scbin"
  chmod +x "$scbin"
fi

"$scbin" "$@"
