#!/usr/bin/env bash

# Cause the script to exit if a single command fails
set -e  # o pipefail -v

usage()
{
  cat << USAGE >&2
usage: $(basename "$0") [-h] [-j NUM_JOBS] [-l LINE_LENGTH] [files [files ...]]

Python code formatter

positional arguments:
files                    files that need to be checked, all files\
 with \`.py\` extension will be checked if no files are specified

optional arguments:
  -h, --help             show this help message and exit
  -j, --num-jobs JOBS    number of subprocesses to use to run checks in
  -l, --line-length INT  maximum length that any line may be
USAGE
  exit 0
}


# ~ ~ ~ ~ ~ environment variables ~ ~ ~ ~ ~

FILES=""
LINE_LENGTH=99
NUM_JOBS=0

while (( "$#" )); do
  case "$1" in
    -h|--help)
      usage
      ;;
    -j|--num-jobs)
      NUM_JOBS=$2
      shift 2
      ;;
    -l|--line-length)
      LINE_LENGTH=$2
      shift 2
      ;;
    -*|--*=) # unsupported flags
      echo "Error: Unsupported flag $1" >&2
      exit 1
      ;;
    *) # preserve positional arguments
      FILES="${FILES} $1"
      shift
      ;;
  esac
done

# check all python files if `FILES` unspecified
if [[ -z "${FILES}" ]]; then
  # check whole git repo if possible
  if git rev-parse --git-dir > /dev/null 2>&1; then
    ROOT="$(git rev-parse --show-toplevel)"
    builtin cd "${ROOT}" || exit 1
  fi

  FILES=.
fi


# ~ ~ ~ ~ ~ code formatting ~ ~ ~ ~ ~
# fix imports order with isort
LINE_LENGTH="${LINE_LENGTH}" catalyst-codestyle-isort \
  --apply \
  --jobs "${NUM_JOBS}" \
  -- ${FILES}

# fix codestyle with black
black --line-length "${LINE_LENGTH}" -- ${FILES}
