#!/bin/bash

# checks the repository state of /etc
# should be installed with
# echo 'test -n "$PS1" -a -x /usr/sbin/virtshaus-checketc && /usr/sbin/virtshaus-checketc || true' >> /root/.bashrc

fatal() {
    echo "$@" 1>&2
    exit 1
}

cd /etc

# 1. is /etc managed by etckeeper (or rather: git)
test -d "/etc/.git/" || fatal "/etc not under git control"
# 2. are there any uncommitted changes in /etc
git diff-index --quiet HEAD -- || fatal "uncomitted changes in /etc"
# 3. has the latest commit been an autocommit?
git log -n1 | grep daily >/dev/null && fatal "autocommitted change in /etc"
# 4. is the repo in sync with the remote?
test "x$(git rev-parse @)" !=  "x$(git rev-parse @{u})" && fatal "/etc repository out of sync with upstream"
