#!/bin/bash

# - Fix branch to check out

. "${HOME}/.ansible-venv/bin/activate"

GIT_REPO_BASE="https://gitea.admin-a.hpetersenfamily.com/heath/ansible"
GIT_REPO="${GIT_REPO_BASE}.git"
LOG_FILE="/var/log/ansible-pull.sh.log"
LOCK_FILE="/tmp/ansible-pull.sh.lock"

# - Redirect all further output to the log file and create a lock file
exec >>"${LOG_FILE}" 2>&1 9>"${LOCK_FILE}"

# - Log that we've gotten this far
echo
echo "$(basename "${0}"): $(date "+%Y-%m-%d %H:%M:%S")"
echo "----------"

# - If we can't lock the lock file, don't proceed
if ! flock -n 9; then echo "ERROR - Another copy of ansible-pull.sh is already running! Exiting..." ; exit 1 ; fi

# - Do our work
ansible-pull --only-if-changed --url "${GIT_REPO}" --checkout main "${@}"
