#!/bin/bash

echo "!!!WARNING!!! This script will create the MASTER DISK"
echo ""
echo "Are you sure about it?"
echo "Starting in 5 seconds, press CTRL+C to abort."
echo ""

sleep 5

echo -n "DELETING:"
echo -n " LOGS"
( find /data/var/log/khomp -type f -delete )
echo -n " CDR"
( find /data/var/local/ftp/cdr -type f -delete )
echo -n " CORE_FILES"
( find /data/var/tmp/khomp -type f -delete )
echo " DONE"

echo -n "CHANGING:"
echo -n " UDEV_ETH1"
cat > /etc/udev/rules.d/70-persistent-net.rules.in << EOT
# This file was automatically generated by the /lib/udev/write_net_rules
# program, run by the persistent-net-generator.rules rules file.
#
# You can modify it, as long as you keep each rule on a single
# line, and change only the value of the NAME= key.

# Dummy ethernet
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="02:00:00:ff:ff:ff", ATTR{dev_id}=="0x0", ATTR{type}=="1", KERNEL=="eth*", NAME="eth0"
EOT
mv -f /etc/udev/rules.d/70-persistent-net.rules.in /etc/udev/rules.d/70-persistent-net.rules
echo -n " devices.yaml"
cat > /etc/khomp/config/devices.yaml.in << EOT
#Khomp configuration file
#Please note that indentation denotes scope but TABs are not permitted
#All fields are case sensitive
#Generated by: kgateway-master-disk

---
"Devices": []

EOT
mv -f /etc/khomp/config/devices.yaml.in /etc/khomp/config/devices.yaml
echo -n " klog.cfg"
sed 's/FullLog=true/FullLog=false/g' < /etc/khomp/config/klog.cfg > /etc/khomp/config/klog.cfg.in
mv -f /etc/khomp/config/klog.cfg.in /etc/khomp/config/klog.cfg
sed 's/MaxLogFileSize=.*/MaxLogFileSize=104857600/g' < /etc/khomp/config/klog.cfg > /etc/khomp/config/klog.cfg.in
mv -f /etc/khomp/config/klog.cfg.in /etc/khomp/config/klog.cfg
sed 's/MaxTotalLogSize=.*/MaxTotalLogSize=5368709120/g' < /etc/khomp/config/klog.cfg > /etc/khomp/config/klog.cfg.in
mv -f /etc/khomp/config/klog.cfg.in /etc/khomp/config/klog.cfg
echo -n " kgateway-firstboot"
insserv kgateway-firstboot
echo -n " interfaces"
sed 's/address 10.3.40.11/address 10.10.10.10/g' < /etc/network/interfaces > /etc/network/interfaces.in
mv -f /etc/network/interfaces.in /etc/network/interfaces
echo " DONE"

echo "CLEANUP"
[ -f /usr/sbin/kgatweay-master-disk ] && rm /usr/sbin/kgatweay-master-disk

echo "POWEROFF"

shutdown -h -P now
