#!/bin/bash
#
# This script uses bash for arrays.
#

set -e

#
#  If we're not on a Bytemark host then we do nothing.
#
if [ "$(/usr/bin/is-bytemark-ip)" == "0" ] ; then
    #
    # Create a dummy data directory
    #
    mkdir -p /root/BytemarkDNS/data

    if [ -x /root/BytemarkDNS/upload ]; then
        chmod 644 /root/BytemarkDNS/upload
    fi

    echo "* ============================================================= *"
    echo "*  This machine doesn't appear to be within the Bytemark Range  *"
    echo "* ============================================================= *"

    exit 0
fi

#
#  Rebuild exim if we need to
#
if [ -e /etc/exim4/Makefile -a -e /etc/exim4/symbiosis.d/00-header ]; then

    #
    #  Rebuild exim4
    #
    cd /etc/exim4

    #
    # Don't fail the whole install if make fails.
    #
    make || echo "W: Rebuild of exim4 configuration failed."
fi

#
#  If we have a /root/BytemarkDNS.zip file then we're OK.
#
if [ !  -d /root/BytemarkDNS ]; then
    #
    #  Get the file
    #
    cd /root && wget -O BytemarkDNS.zip http://upload.ns.bytemark.co.uk/create/

    #
    #  Unzip it
    #
    cd /root && unzip -o BytemarkDNS.zip
fi

#DEBHELPER#
exit 0
