#!/usr/bin/env bash

while test $# -gt 0; do
  case "$1" in
    -h|--help)
      echo ""
      echo "$0 [options]"
      echo ""
      echo "This script symlinks a collection to a path on disk and fixes some issues on the way."
      echo ""
      echo "options:"
      echo "   --name <X>   name of the collection"
      echo "   --path <X>   path to the collection on disk"
      echo ""
      echo "misc:"
      echo "-h --help       show brief help"
      echo ""
      exit 0
      ;;
    # options
    --name)
      shift
      COLLECTION_PATH_LOCAL="$(readlink -f ".")/ansible_collections/${1//./\/}"
      shift
      ;;
    --path)
      shift
      COLLECTION_PATH_REMOTE="$(readlink -e "${1}")"
      shift
      ;;

    # misc
    *)
      break
      ;;
  esac
done

CURRENT_PATH="$(readlink -e .)"

if [ ! -f "${CURRENT_PATH}/ansible.cfg" ]; then
  echo "error - please execute this script from the ansible folder of your deployment"
fi

if [ -z ${COLLECTION_PATH_LOCAL:+x} ] ||  [ -z ${COLLECTION_PATH_REMOTE:+x} ]; then
  echo "error - parameters required"
  exit 1
fi

rm -rf "${COLLECTION_PATH_LOCAL}"
ln -fs "${COLLECTION_PATH_REMOTE}" "${COLLECTION_PATH_LOCAL}"
