#!/bin/bash

# post-merge hook for Debian systems
#
# 1. copy to .git/hooks
# 2. to test run ".git/hooks/post-merge"
# 3. create cronjob running "git pull -q -C /etc/tinc/icvpn/"
#

PATH=/usr/sbin:$PATH

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

BASE=$(git rev-parse --show-toplevel)
TINCCFG=$BASE/tinc.conf
test -w $TINCCFG || fail "ERR: $TINCCFG is not writeable"

sed -i '/^ConnectTo/d' $TINCCFG

while read HOST; do
  # skip hosts without address
  grep -iq '^Address' -- hosts/"$HOST" || continue

  echo "ConnectTo = $HOST" >> $TINCCFG
done < metanodes

if hash tinc 2>/dev/null; then
    # prefer the command line interface that comes with tinc1.1 if it exists
    tinc -n icvpn reload
else
    invoke-rc.d tinc reload icvpn
fi

exit 0