#!/bin/bash
#
# A install script for Micetro on BDDS
#


# If neither dhcpd.conf or named.conf do exist we assume this is an
# installation on a clean BDDS.
CLEAN_INSTALL=true
if [ -s "/replicated/etc/dhcpd.conf" ] || [ -s "/replicated/jail/named/etc/named.conf" ]; then
    CLEAN_INSTALL=false
fi


# Disable and mask CommandServer to prevent start on reboot.
/usr/local/bluecat/PsmClient node set command-server-enable=0
systemctl mask cs


# Add firewall rules for Micetro services.
MANAGEMENT_INTERFACE=`PsmClient node get management-interface | grep get-notify | cut -d '=' -f2`
sed -i "s/eth[0|2]/$MANAGEMENT_INTERFACE/g" mm-rules.iptables
/usr/local/bluecat/custom_fw_rules --import-rules ./mm-rules.iptables

# Ensure that named-checkconf can call chroot to validate the 
# config in the chroot jail.
for path in "/usr/local/sbin/named-checkconf" "/usr/local/bin/named-checkconf"; do
    if [ -e "$path" ]; then
        chown named "$path"
        chmod +s "$path"
        setcap cap_sys_chroot+ep "$path"
        break
    fi
done

# Ensure we have correct ownership in jail.
chown -R named:named /replicated/jail/named/usr
chown -R named:named /replicated/jail/named/var

# Create dhcpd.conf and named.conf
./create_dhcpd_conf
./create_named_conf


# Copy additional files needed.
cp -r source/* /

# Make sure we have to correct privileges on the files.
chmod 755 /usr/local/mm/*
chmod 644 /usr/local/mm/*.conf


if [ "$CLEAN_INSTALL" == true ]; then
    # If this is a clean install then by default we should enable SNMP.

    /usr/local/bluecat/PsmClient -f ./snmp_conf.json
    /usr/local/bluecat/PsmClient node set snmp-enable=1
fi
