#!/usr/bin/env bash
#
# Run ansible
set -euo pipefail
IFS=$'\n\t'
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
cd "$DIR"

function die {
    code="$1"
    shift 1
    (>&2 echo "${@}")
    exit "$code"
}

target="$1"
shift 1

if [ "$target" = "homeserver" ]; then
    ansible-playbook -v -i environments/home playbook.yaml --diff --limit homeserver "${@}"
elif [ "$target" = "odolinux" ]; then
    ansible-playbook -v -i environments/laptop playbook.yaml --diff --limit odolinux "${@}"
elif [ "$target" = "odofreebsd" ]; then
    ansible-playbook -v -i environments/laptop playbook.yaml --diff --limit odofreebsd "${@}"
elif [ "$target" = "odowork" ]; then
    ansible-playbook -v -i environments/laptop playbook.yaml --diff --limit odowork "${@}"
elif [ "$target" = "jail_nat_dhcp" ]; then
    ansible-playbook -v -i environments/jail playbook.yaml --diff --limit nat_dhcp "${@}"
elif [ "$target" = "jail_homeserver_nat_dhcp" ]; then
    ansible-playbook -v -i environments/jail playbook.yaml --diff --limit homeserver_nat_dhcp "${@}"
elif [ "$target" = "sftp" ]; then
    ansible-playbook -v -i environments/jail playbook.yaml --diff --limit sftp "${@}"
elif [ "$target" = "certificate" ]; then
    ansible-playbook -v -i environments/jail playbook.yaml --diff --limit certificate "${@}"
elif [ "$target" = "bastion" ]; then
    ansible-playbook -v -i environments/jail playbook.yaml --diff --limit bastion "${@}"
elif [ "$target" = "momlaptop" ]; then
    ansible-playbook -v -i environments/jail playbook.yaml --diff --limit momlaptop "${@}"
elif [ "$target" = "vm_poudriereodo" ]; then
    ansible-playbook -v -i environments/vm playbook.yaml --diff --limit poudriereodo "${@}"
elif [ "$target" = "vm_poudrieremrmanager" ]; then
    ansible-playbook -v -i environments/vm playbook.yaml --diff --limit poudrieremrmanager "${@}"
elif [ "$target" = "mrmanager" ]; then
    ansible-playbook -v -i environments/colo playbook.yaml --diff --limit mrmanager "${@}"
elif [ "$target" = "jail_mrmanager_nat_dhcp" ]; then
    ansible-playbook -v -i environments/jail playbook.yaml --diff --limit mrmanager_nat_dhcp "${@}"
elif [ "$target" = "jail_admin_git" ]; then
    ansible-playbook -v -i environments/jail playbook.yaml --diff --limit admin_git "${@}"
elif [ "$target" = "jail_public_dns" ]; then
    ansible-playbook -v -i environments/jail playbook.yaml --diff --limit public_dns "${@}"
else
    die 1 "Unrecognized target"
fi
