#!/bin/bash

# Pull in common software
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 0;
fi

fix_openbox() {
	## Set black background for openbox
	sed -i -e 's#test -z $BG || $BG -solid .*#test -z $BG || $BG -solid "\#000000"#g' /usr/lib/openbox/openbox-autostart
	rm -rf /usr/share/xsessions/openbox-kde.desktop
}

fix_thunar() {
	## Remove thunar wallpaper plugin
	twplug='/usr/lib/thunarx-3/thunar-wallpaper-plugin.so'
	if [[ -e "$twplug" ]]; then
		rm -rf "$twplug"
	fi
}

fix_nmd() {
	## Create nmd script for bspwm session
	cp -rf /usr/bin/networkmanager_dmenu /usr/local/bin/nmd && sed -i -e 's/config.ini/nmd.ini/g' /usr/local/bin/nmd
}

fix_grub() {
	## Fix GRUB_DISTRIBUTOR for Ultimate Arch Edge
	sed -i -e 's/GRUB_DISTRIBUTOR=.*/GRUB_DISTRIBUTOR="Ultimate Arch #DISTTYPEUPPER"/g' /etc/default/grub
	sed -i -e 's/#GRUB_DISABLE_OS_PROBER=false/GRUB_DISABLE_OS_PROBER="false"/g' /etc/default/grub
	cat /etc/grub.d/10_linux | grep 'OS="${GRUB_DISTRIBUTOR} Linux"' &>/dev/null
	if [[ "$?" == 0 ]]; then
		sed -i -e 's/OS="${GRUB_DISTRIBUTOR} Linux"/OS="${GRUB_DISTRIBUTOR}"/g' /etc/grub.d/10_linux
	fi
	INSTALLED=$(type -p grub-mkconfig)
	if [[ "${INSTALLED}" ]]; then
		if [[ -f "/boot/grub/grub.cfg" ]]; then
			FullBar
			Encapsulate "Updating Grub."
			FullBar
			grub-mkconfig -o /boot/grub/grub.cfg
		fi
	fi
}

main(){
	local hookname="${1}"

	case "$hookname" in
		fix-openbox)  fix_openbox ;;
		fix-thunar)   fix_thunar ;;
		fix-nmd)	  fix_nmd ;;
		fix-grub)	 fix_grub ;;
		"")		   echo "Nothing to do!";;
	esac
}

main "$@"
