#!/bin/bash

function usage() {
  echo "usage:"
  echo "  mkvenv [-g|--get] env_name1 [env_name2 ...]"
  echo "  mkvenv (-h|--help)"
}

function help() {
        echo "mkvenv creates and updates Python virtual environments"
        usage
        exit 0
}       

opt=" --no-index --find-links=.local/venvtools"
declare -a vdirs=()

while [ "$#" -gt 0 ]; do
  case "$1" in
    -h) help;;
    -g) opt=""; shift 1;;
    --help) help;;
    --get) opt=""; shift 1;;
    -*) echo "unknown option: $1" >&2; usage; exit 1;;
    *) vdirs=(${vdirs[@]} $1); shift 1;;
  esac
done

if [ ${#vdirs[@]} -eq 0 ]; then
  echo "** Error: Missing the virtual environment name **"
  usage
  exit 1
fi

for i in "${vdirs[@]}"
do
  echo "creating virtual environment $PWD/$i"
  python3 -m venv $i > /dev/null
  echo "updating virtual environment $PWD/$i"
  $i/bin/pip install$opt pip --upgrade setuptools wheel > /dev/null
done
