#!/bin/bash
ROLE_NAME=provision
DIR=~/.ansible/roles/$ROLE_NAME
GIT_BASE=${GIT_BASE:-https://github.com/moshloop}
inventory=
ARGS=

if [[ "$1" == "-h" || "$1" == "--help" ]]; then
    echo "USAGE: ansible-$ROLE_NAME [any ansible-playbook arguments]"
    echo "Use ansible-$ROLE_NAME install to install the ansible roles only without execution"
    exit
fi
while getopts ":i: " opt; do
  case $opt in
    i)
    inventory=$OPTARG
      ;;
    esac
done

if [[ "$inventory" == "" && -d inventory ]]; then
  ARGS+=" -i inventory"
  inventory="inventory"
fi

ARG1=$1

checkout_role() {
  DIR=~/.ansible/roles/$1
  if [[ ! -e $DIR ]]; then
    git clone $GIT_BASE/ansible-$1.git $DIR
  fi
  pwd=$(pwd)

  if [[ "$ARG1" == "install" ]]; then
    return
  elif [[  "$ARG1" == "update" ]]; then
    cd $DIR
    git fetch --all
    cd $pwd
    return
  fi

  filter="._meta.hostvars | values[] | .ansible_$1_version"
  desired_tag=$( ansible-inventory -i $inventory --list | jq -r "$filter" | head -n1)

  pwd=$(pwd)
  cd $DIR
  current_tag=$(git describe --tags)

  if [[ "$desired_tag" != "null" && "$desired_tag" != "" && "$desired_tag" != "$current_tag" ]]; then
    echo "Checking out $desired_tag"
    git fetch
    git checkout $desired_tag
  fi
  echo "Using ansible-$1"
  git log -n1
  cd $pwd
}

checkout_role "deploy"
checkout_role "provision"

if [[ "$ARG1" == "install" || "$ARG1" == "update" ]]; then
  exit
fi

ln -s $DIR/$ROLE_NAME.yml .ansible-$ROLE_NAME.yml
function finish {
  rm .ansible-$ROLE_NAME.yml
}
trap finish EXIT


ANSIBLE_HASH_BEHAVIOUR=merge
ansible-playbook .ansible-$ROLE_NAME.yml $@