#!/bin/bash
# Prepare this node for being used as a base for a hard disk clonable image.
# © 2013, Multapplied Networks, Inc.

# Allow extended globbing
shopt -s extglob

if systemctl --quiet is-active bonding ; then
    echo "Bonding is running; run 'systemctl stop bonding' before continuing."
    exit 1
fi

cat <<EOF
This will remove all bonding configuration and logs from this device.

There is no recovery from this. Only run this command if you are preparing
this node to be used as a base image for hard drive cloning

EOF

read -p "Are you sure? This cannot be undone. (y/N) " confirm
case ${confirm} in
    [Yy]* ) ;;
    [Nn]* ) exit ;;
    *     ) exit ;;
esac

# Run the deconfigure script, keep bonding down
/usr/sbin/bonding-deconfigure -n -q -d

# Remove the udev net rules, so the new interfaces get named properly
rm -f /etc/udev/rules.d/70-persistent-net.rules

# Remove hooks
rm -f /etc/bonding/addressing.d/*/*
rm -f /etc/bonding/bond.d/*/*
rm -f /etc/bonding/connectedip.d/*/*
rm -f /etc/bonding/cpenatip.d/*/*
rm -f /etc/bonding/interface.d/*/*
rm -f /etc/bonding/leg.d/*/*
rm -f /etc/bonding/privatewan-router-space.d//*/*
rm -f /etc/bonding/route.d/*/*
rm -f /etc/bonding/shaping.d/all/*
rm -f /etc/bonding/shaping.d/default/!(20_qos)
rm -f /etc/bonding/tun.d/*/*

# Remove custom nftables
rm -f /etc/bonding/nftables/*

# Remove custom bird configs
rm -f /etc/bonding/bird/*

# Reset the hostname to something generic
echo "default-bonder" > /etc/hostname
service hostname.sh start &> /dev/null

# Remove SSH keys - They will be generated by a drop-in for ssh.service next
# time it starts
rm -f /etc/ssh/ssh_host_*

# Clear out the bash history, and ask the user to do the same
rm -f "${HISTFILE}"
unset HISTFILE
echo "To avoid leaving any traces of your bash history, please run:"
echo "unset HISTFILE"
