#!/bin/sh

function getPipfile() {
  DIR="$(dirname "$1")"
  if [[ $DIR == "/" ]]; then exit; fi
  PIPFILE="$DIR/Pipfile"
  if [[ -e "$PIPFILE" ]]; then
    echo "$PIPFILE"
  else
    getPipfile "$DIR"
  fi
}

SCRIPT="$(realpath "$1")"
shift
PIPFILE=$(getPipfile "$SCRIPT")

if [[ -n "$PIPFILE" ]]; then
  PIPENV_PIPFILE="$PIPFILE" pipenv run python "$SCRIPT" "$@"
else
  echo "Could not locate the Pipfile for $SCRIPT."
  exit 1
fi
