#!/bin/sh
#
# This script can be used instead of git pull when updating from
# a remote repo to make sure the metadata matches what is stored in
# the repo.
# It will do a git pull, show a list of changes to be made to
# the metadata, and after getting confirmation, apply the changes.

DO_MTIME=yes
MSFILE=".metadata"
umask 0077

[ "$DO_MTIME" = "yes" ] && MSFLAGS="-m"

exit_on_fail() {
	"$@"
	if [ $? -ne 0 ]; then
		echo "Failed to execute: $@" >&2
		exit 1
	fi
}

exit_on_fail \
	git pull "$@"

if [ ! -e "$MSFILE" ]; then
	echo "\"$MSFILE\" missing" >&2
	exit 1
fi

echo "Going to apply the following metadata changes" >&2

metastore -c $MSFLAGS -f "$MSFILE" >&2

printf "%s" "Ok to apply? (y/n): " >&2
read REPLY
echo ""

if [ "$REPLY" != "y" ]; then
	echo "Aborted" >&2
	exit 1
fi

exit_on_fail \
	metastore -a $MSFLAGS -e -E -f "$MSFILE"

exit 0
