#!/bin/bash

SCRIPT="${0}"
SCRIPT_DIR=$(cd $(dirname ${SCRIPT}) && pwd)

LOG=/tmp/$(basename ${0}).log

function die {
  echo 1>&2 "${1}"
  log "${1}"
  exit 1
}

function log {
  echo "${1}" | tee -a ${LOG}
}

if [[ $(whoami) != "{{ admin_user }}" ]] ; then
  if [[ $(whoami) == "root" ]] ; then
    sudo -H -u "{{ admin_user }}" ${0}
    exit $?
  fi
  die "${0} must be run as {{ admin_user }}"
fi

ANSIBLE_USER_HOME=$(cd ~{{ admin_user }} && pwd)

ANSIBLE_SNAPSHOT="/home/bubble/ansible.tgz"

cd ${ANSIBLE_USER_HOME} \
  && tar czf ${ANSIBLE_SNAPSHOT} ./ansible \
  && chmod 400 ${ANSIBLE_SNAPSHOT} \
  && chown bubble ${ANSIBLE_SNAPSHOT} \
  || die "Error creating ansible snapshot"
