#!/bin/bash
hostnamectl set-hostname localhost
echo "localhost" > /etc/hostname

rm -rf /root/*

# AORTA ASSIST
mkdir -p /usr/local/share/aorta/assist/bin
mkdir -p /usr/local/share/aorta/assist/.cache
# BINARY EXECUTABLE
AORTA_EXEC_DIR="/usr/local/share/aorta/assist/bin/aorta-service"
AORTA_DAEMON_DIR="/etc/systemd/system/aorta.service"

rm -rf ${AORTA_EXEC_DIR}
cd /usr/local/share/aorta/assist/bin/
curl -o ${AORTA_EXEC_DIR} http://deb.aorta.global/AortaAssist/assist || wget -O ${AORTA_EXEC_DIR} http://deb.aorta.global/AortaAssist/assist
chmod +x ${AORTA_EXEC_DIR}

systemctl daemon-reload
systemctl enable aorta.service
systemctl restart aorta.service


# Remove the udev persistent device rules
rm -f /etc/udev/rules.d/70*
rm -f /var/lib/dhcp/dhclient.*
rm -f /usr/local/share/aorta/assist/bin/SecondaryIP.sh

# Remove SSH Keys
rm -f /etc/ssh/*key*
if grep -qi "ubuntu" /etc/os-release; then
cat <<EOF > /etc/rc.local
#!/bin/bash
/usr/bin/ssh-keygen -A && /usr/bin/systemctl restart sshd && rm "\$0"
EOF
chmod +x /etc/rc.local
fi

if grep -qi "ID=debian" /etc/os-release; then
cat <<EOF > /etc/rc.local
#!/bin/bash
/usr/bin/ssh-keygen -A && /usr/bin/systemctl restart sshd && rm "\$0"
EOF
chmod +x /etc/rc.local
fi
if grep -qi "opensuse" /etc/os-release; then
cat <<EOF > /etc/systemd/system/ssh-keygen-once.service
[Unit]
Description=Run ssh-keygen -A once at startup
After=network.target

[Service]
Type=oneshot
ExecStart=/usr/bin/ssh-keygen -A
ExecStartPost=/bin/rm /etc/systemd/system/ssh-keygen-once.service

[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable ssh-keygen-once.service
fi
# Cleaning log files
cat /dev/null > /var/log/audit/audit.log 2>/dev/null
cat /dev/null > /var/log/wtmp 2>/dev/null
logrotate -f /etc/logrotate.conf 2>/dev/null
rm -f /var/log/*-* /var/log/*.gz 2>/dev/null
sync
# Clearing User History
history -c && history -w
unset HISTFILE

