#!/bin/bash

# Rebuild the bind configuration's file
# with the IP of the slave dns

FLAGFILE="/var/run/alternc/refresh_slave";
TPL="/etc/alternc/templates/bind/slaveip.conf"
TARGET="/var/alternc/bind/slaveip.conf"
TMP=$(mktemp /tmp/slaveip.conf.XXXX)

if [ ! -e "$FLAGFILE" ] ; then 
  # Nothing to do
  exit 0
fi

# Source some functions
. /usr/lib/alternc/functions.sh

# Get the slave IP. Remove the "newline" caracters
val=$(mysql_query "SELECT concat(ip,'::',class,'; ') FROM slaveip;"|tr '\n' ' ')

# Add the slaves to the templates, re-add the missing "/" separator of subnet
cat "$TPL" | sed -e "s/\/\/AUTO-SLAVES\/\//$val/g" -e "s/::/\//g" > "$TMP"

# Activate the new configuration
mv "$TMP" "$TARGET"
chown root:bind "$TARGET"
chmod 640 "$TARGET"

invoke-rc.d bind9 reload

# Remove FLAGSLAVE file
rm -f "$FLAGFILE"
