#!/bin/bash

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_lsb_release() {
	local file=/etc/lsb-release

	if [[ -z "`grep "^DISTRIB_RELEASE=" $file`" ]] ; then
		# add missing DISTRIB_RELEASE=
		echo "DISTRIB_RELEASE=rolling" >> "$file"
	fi
	sed -i "$file" \
		-e 's|^DISTRIB_ID=.*$|DISTRIB_ID=ultimate|' \
		-e 's|^DISTRIB_RELEASE=.*$|DISTRIB_RELEASE=rolling|' \
		-e 's|^DISTRIB_DESCRIPTION=.*$|DISTRIB_DESCRIPTION=\"Ultimate Arch #DISTTYPEUPPER\"|'
}

fix_os_release() {
	local file=/usr/lib/os-release

	sed -i "$file" \
		-e 's|^NAME=.*$|NAME=\"Ultimate Arch #DISTTYPEUPPER\"|' \
		-e 's|^PRETTY_NAME=.*$|PRETTY_NAME=\"Ultimate Arch #DISTTYPEUPPER\"|' \
		-e 's|^ID=.*$|ID=ultimate|' \
		-e 's|^ID_LIKE=.*$|ID_LIKE=arch|' \
		-e 's|^BUILD_ID=.*$|BUILD_ID=rolling|' \
		-e 's|^HOME_URL=.*$|HOME_URL=\"https://ultimateedition.info\"|' \
		-e 's|^DOCUMENTATION_URL=.*$|DOCUMENTATION_URL=\"https://ultimateedition.info\"|' \
		-e 's|^SUPPORT_URL=.*$|SUPPORT_URL=\"https://ultimateedition.info\"|' \
		-e 's|^BUG_REPORT_URL=.*$|BUG_REPORT_URL=\"https://ultimateedition.info\"|' \
		-e 's|^LOGO=.*$|LOGO=uelogo|'

	if [ -z "$(grep "^ID_LIKE=" $file)" ] && [ -n "$(grep "^ID=" $file)" ] ; then
		# add missing ID_LIKE=
		sed -i $file -e '/^ID=/a \ID_LIKE=arch'
	fi
DRACUT=$(type -p dracut)
if [[ "${DRACUT}" ]]; then
	dracut -f --uefi --regenerate-all
	dracut /boot/initramfs-$(uname -r).img
	dracut -H /boot/initramfs-$(uname -r).img
fi
	# fix issue file
	sed -i 's|Arch Linux|Ultimate Arch #DISTTYPEUPPER|g' /etc/issue /usr/share/factory/etc/issue
}

main()
{
	local hookname="$1"

	case "$hookname" in
		os-release)  fix_os_release ;;
		lsb-release) fix_lsb_release ;;
		"")		  fix_os_release
					 fix_lsb_release
					 ;;
	esac
}

main "$@"
