#!/bin/sh
# Copyright (c) 2005, Jilles Tjoelker
# All rights reserved.
#
# Redistribution and use in source and binary forms, with
# or without modification, are permitted provided that the
# following conditions are met:
#
# 1. Redistributions of source code must retain the above
#    copyright notice, this list of conditions and the
#    following disclaimer.
# 2. Redistributions in binary form must reproduce the
#    above copyright notice, this list of conditions and
#    the following disclaimer in the documentation and/or
#    other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
# CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
# WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
# PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
# USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
# OF SUCH DAMAGE.
#
# Simple script to update ircd.conf for dynamic IPs (e.g. from cron)
# This makes some assumptions only valid for ircd-hybrid.
# You are expected to understand yourself what you need to change here.

cd /home/ircd/hybrid/etc || exit 1

box1ip=$(host -t a dyndns.host.name.1|awk '/has address/ {print $4}')
if [ -z "$box1ip" ]; then
	echo "Failed to resolve dyndns.host.name.1" >&2
	exit 1
fi

box2ip=$(host -t a dyndns.host.name.2|awk '/has address/ {print $4}')
if [ -z "$box2ip" ]; then
	echo "Failed to resolve dyndns.host.name.2" >&2
	exit 1
fi

(
echo '### ---------------------------------------------------------'
echo '### DO NOT EDIT -- This file is generated from ircd.conf.tmpl'
echo '### Edit ircd.conf.tmpl instead'
echo '### ---------------------------------------------------------'
sed -e "s/%%BOX1IP%%/$box1ip/g" -e "s/%%BOX2IP%%/$box2ip/g" ircd.conf.tmpl
) > ircd.conf.new
cmp ircd.conf ircd.conf.new || (cp ircd.conf.new ircd.conf && kill -HUP `cat ircd.pid`)
