#!/bin/bash
post_install() {
	# Detect distro and set GRUB location and update method
	GRUB_DIR='grub'
	UPDATE_GRUB=''
	BOOT_MODE='legacy'
	GRUB_THEME="Fallout"
	if [[ -d /boot/efi && -d /sys/firmware/efi ]]; then
		BOOT_MODE='UEFI'
	fi
	if [[ -f "ultimate-common" ]]; then
		source ultimate-common
	elif [[ -f "/usr/share/ultimate_edition/ultimate-common" ]]; then
		source /usr/share/ultimate_edition/ultimate-common
	else
		echo "No Ultimate Edition common source. Please install ultimate-edition-common."
		exit 1;
	fi
	CheckRoot
	if [ $root_inode -ne 2 ]; then
		echo "Boot mode: ${BOOT_MODE}"
		echo 'Removing other themes from GRUB config'
		sed -i '/^GRUB_THEME=/d' /etc/default/grub
		echo 'Making sure GRUB uses graphical output'
		sed -i 's/^\(GRUB_TERMINAL\w*=.*\)/#\1/' /etc/default/grub
		echo 'Removing empty lines at the end of GRUB config' # optional
		sed -i -e :a -e '/^\n*$/{$d;N;};/\n$/ba' /etc/default/grub
		echo 'Adding new line to GRUB config just in case' # optional
		echo | tee -a /etc/default/grub
		if [[ -f "/usr/share/${GRUB_DIR}/themes/${GRUB_THEME}/theme.txt" ]]; then
			echo 'Adding theme to GRUB config'
			echo "GRUB_THEME=/usr/share/${GRUB_DIR}/themes/${GRUB_THEME}/theme.txt" | tee -a /etc/default/grub
		fi
		if which update-initramfs >/dev/null 2>&1
		then
		update-initramfs -u
		fi
		if which grub-mkconfig >/dev/null 2>&1;	then
			grub-mkconfig -o /boot/grub/grub.cfg
		fi
	else
		echo "Chroot Environment detected."
	fi
}
post_upgrade() {
post_install
}
