#!/usr/bin/env bash

# Quick and dirty way to take a requirements.txt and generate a canonical
# version with all transitive dependencies locked.

set -euo pipefail

requirements_file="$1"
python="${2:-python2}"

# Otherwise: `env/bin/activate: line 57: PS1: unbound variable`.
PS1='something'

cd "$(mktemp --directory)"

virtualenv env --python="$(which "$python")"
source env/bin/activate
pip install --requirement "$requirements_file"
pip freeze > requirements.txt

if ! git diff --no-index "$requirements_file" requirements.txt; then
  >&2 echo Requirements files differ! Updated one: "$(pwd)"/requirements.txt
  exit 1
else
  echo Requirements files match!
fi
