#!/bin/bash
set -e
export DEBIAN_FRONTEND=noninteractive
export PATH=/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games:/usr/share/games:/usr/local/sbin:/usr/sbin:/sbin:~/.local/bin:/snap/bin:$PATH

# Handle systems where /lib is not merged in /usr/lib
if [ ! -h /lib ]; then
    mv /usr/lib/live/config/* /lib/live/config/
fi

echo "Installing flatpak repositories (if needed)"
flatpak remote-add --if-not-exists flathub https://dl.flathub.org/repo/flathub.flatpakrepo || true
flatpak remote-add --if-not-exists winepak https://dl.winepak.org/repo/winepak.flatpakrepo || true


echo "Removing useless stuff"
apt update
apt-get purge -y qt4-designer qttools5-dev-tools texlive-latex-extra-doc texlive-pstricks-doc texlive-pictures-doc texlive-latex-recommended-doc texlive-latex-base-doc texlive-fonts-recommended-doc texlive-pstricks-doc python-mpltoolkits.basemap-data leafpad xpra ferret minicom xpdf gvim llvm-7-dev python-scipy python-all python-all-dev || true

echo "Manually enabling pulseaudio"
systemctl --user enable pulseaudio.service || true

echo "Manually enabling live-config"
systemctl enable live-config || true

echo "Doing some magic on networking stuff"
systemctl enable NetworkManager || true
systemctl enable resolvconf || true
#rm /etc/resolv.conf || true
#ln -s etc/resolvconf/run/resolv.conf etc/resolv.conf || true
mkdir -p /etc/systemd/network /etc/udev/rules.d
ln -sf /dev/null "/etc/systemd/network/90-mac-for-usb.link"
ln -sf /dev/null "/etc/systemd/network/99-default.link"
echo > "/etc/udev/rules.d/73-special-net-names.rules"

echo "Manually disabling inetd"
systemctl disable inetd || true

echo "Manually disabling mariadb"
systemctl disable mariadb || true

echo "Manually disabling postgresql"
systemctl disable postgresql || true

echo "Manualy disabling redis-server"
systemctl disable redis-server || true

echo "Manually disabling nginx"
systemctl disable nginx || true

echo "Manually disabling cups"
systemctl disable cups cups-browsed || true

echo "Manually disabling avahi"
systemctl disable avahi-daemon || true

echo "Manually disabling hostapd"
systemctl disable hostapd || true

echo "Manually disaling phpsessionclean"
sudo systemctl disable phpsessionclean.timer || true

echo "Manually disabling apt-daily systemd timers"
sudo systemctl disable apt-daily-upgrade.timer || true
sudo systemctl disable apt-daily.time || true

echo "cleaning the system with bleachbit"
bleachbit -c system.localizations apt.autoclean apt.clean apt.autoremove apt.package_lists deepscan.backup deepscan.ds_store deepscan.thumbs_db deepscan.tmp system.cache system.rotated_logs thumbnails.cache &> /dev/null && echo "done"


parrot-mirror-selector default

echo "adding additional modules to initramfs"
apt -y install initramfs-tools || true
echo "nls_ascii" >> /etc/initramfs-tools/modules
echo "ext4" >> /etc/initramfs-tools/modules
echo "btrfs" >> /etc/initramfs-tools/modules
echo "xfs" >> /etc/initramfs-tools/modules
#echo "zfs" >> /etc/initramfs-tools/modules

echo "Enabling cryptsetup initramfs support"
if [ -e /etc/cryptsetup-initramfs/conf-hook ]; then
    if grep -q '^#CRYPTSETUP=' /etc/cryptsetup-initramfs/conf-hook; then
	sed -i -e 's/^#CRYPTSETUP=.*/CRYPTSETUP=y/' /etc/cryptsetup-initramfs/conf-hook
    else
	echo "CRYPTSETUP=y" >>/etc/cryptsetup-initramfs/conf-hook
    fi
fi

update-initramfs -c -u -k all || true

firecfg --clean || true
firecfg --add-users root || true
firecfg --add-users user || true
firecfg || true

# Hold kernel packages in live image.
for pkg in $(dpkg-query -f'${binary:Package}\n' -l 'linux-image-*' 'linux-headers-*' 'linux-kbuild-*' | grep ii | awk '{print $2}'); do
	apt-mark hold $pkg
done

if [ -x "$(which updatedb 2>/dev/null)" ]; then
    updatedb
fi
