#!/bin/bash
# Afrihost - update dnscluster config formatting to new cpanel formati
confdir="/var/cpanel/cluster/root/config"
fixtmp="/var/cpanel/cluster/root/config/dnscluster.afrihost.com.tmp"

removeunsupported () {
        sed -i 's/user=//'  $oldclusterfile
        sed -i 's/pass=//' $oldclusterfile
        sed -i 's/host=//' $oldclusterfile
        sed -i '/debug=0/d' $oldclusterfile
        sed -i '/version/d' $oldclusterfile
        sed -i '/module=/d' $oldclusterfile
}

# Make a backup of original /var/cpanel/cluster/root/config files
mkdir -p /backup/dnscluster_original 2>/dev/null
rsync -avP $confdir/* /backup/dnscluster_original

sed -i -e 's/196.33.227.30/dnscluster.afrihost.com/g' /var/cpanel/cluster/root/config/*

find /var/cpanel/cluster/root/config -name \*196.33.227.30\* -o -name \*197.242.150.88\* |
                while read pfile ; do
                        echo "mv $pfile `echo $pfile |sed 's/196.33.227.30/dnscluster.afrihost.com/'`";
                        echo "mv $pfile `echo $pfile |sed 's/197.242.150.88/dnscluster.afrihost.com/'`";
                done | bash 2>/dev/null

if egrep -q "user=|host=|version|module=|debug=" /var/cpanel/cluster/root/config/{197.242*,196.33.227*,dnscluster.afrihost.com*} 2>/dev/null ; then
        echo "====== INCORRECT FORMAT FILES FOUND - REMOVING AND CORRECTING NOW ======"
        rm -fv $confdir/*-state.json >/dev/null
        find  /var/cpanel/cluster/ | grep cache  | xargs -i rm -fv {} >/dev/null
        egrep -l "user=|host=|version|module=|debug=" /var/cpanel/cluster/root/config/{197.242*,196.33.227*,dnscluster.afrihost.com*} 2>/dev/null |
                while read oldclusterfile ; do
                removeunsupported
                sed -i -e 's/196.33.227.30/dnscluster.afrihost.com/g' /var/cpanel/cluster/root/config/*
        done
        else
echo -e "\n====== CORRECT FORMAT FILES FOUND ======
Backups made to /backup/dnscluster_original/ of /var/cpanel/cluster/root/config/*
If you get additional errors please copy these and send to us in your ticket to support@afrihost.com or hosting@afrihost.com"
fi

# Set to write-only
grep -rl    sync   /var/cpanel/cluster/root/config/ | xargs -i echo "sed -i 's/sync/write-only/g' {} " | bash
