#!/bin/bash -e

new="$1"
_VERBOSE=""

show_usage() {
	cat <<EOF
Usage: setbranding [options] [<new_branding_name>]

Program shows or change current branding

Valid options are:
  -l        list all available brandings
  -v        verbose output
  -h,--help display help screen

<new_branding_name> is full two-word or short name of branding

Examples:

  # setbranding

Run program without branding name displays current branding packages

  # setbranding -l

Display all available brandings

  # setbranding altlinux-centaurus

Change branding to altlinux-centaurus (altlinux- may be omitted)

Report bugs to http://bugs.altlinux.org
EOF
	exit
}

show_branding_list() {
	apt-cache search --names-only branding release|sed -e 's/^branding-\(.\+\)-release .*$/\1/g' |sort
	exit 1
}

if [ -z "$new" ]
then

	# Show installed branding pacakges
	rpm -qa branding-\* --qf '%{name}\n'

else

	# Show usage information
	if [ "$new" = "--help" -o "$new" = "-h" ] ; then
		show_usage
	fi

	# Show available brandings
	test "$new" = "-l" && show_branding_list

	# Verbose output
	if [ "$new" = "-v" ] ; then
		_VERBOSE="1"
		if [ "$#" -lt 2 ] ; then
			show_usage
		else
			new="$2"
		fi
	fi
	
	# Check privileges
	if [ "$(whoami)" != "root" ]; then
		echo "Change branding requires root privileges. Program is terminated."
		exit 1
	fi
	
	# Strict branding name
	echo "$new" | grep '-' >/dev/null || new="altlinux-$new"
	echo -n "Change branding to $new? [Y/n] "
	read choice
	test "+$choice" = "+n" && exit
	packages="$(rpm -qa branding-\* --qf '%{name}\n')"
	removed="$(echo $packages| tr ' ' '\n' | grep -v "^branding-$new-"| sed 's/$/-/')"
	#apt-get update

	# Build list of packages with exclude missed packages
	installed="$(echo $packages | tr ' ' '\n' | cut -f4 -d- | sed "s/^/branding-$new-/"|sort)"
	installed="`comm -12 <(echo "$installed") <(apt-cache pkgnames branding-$new|sort)`"
	#echo $installed $removed | tr ' ' '\n'

	# Exit if no installed or removed packages are found
	test -z "$installed" -a -z "$removed" && exit 0

	# Show apt-get with parameters
	test -n "$_VERBOSE" && echo "RUN /usr/bin/apt-get install $installed $removed"

	# Store old rpm names
	before="$(rpm -qa branding-\* --qf '%{name}-%{version}-%{release}\n')"

	# Replace old branding with new one
	/usr/bin/apt-get install $installed $removed

	difference="`sort <(echo "$before") <(rpm -qa branding-\* --qf '%{name}-%{version}-%{release}\n') | uniq -u`"
	test -n "$_VERBOSE" && echo "DIFFERENCE: $difference"

	# If no changes don't update initrd and grub
	test -z "$difference" && exit 0

	# Update Grub theme if grub exists
	# Check available grub2
	if [ -x /usr/sbin/grub-mkconfig ]; then
		# Check available grub.cfg
		if [ -w /boot/grub/grub.cfg ]; then
			test -n "$_VERBOSE" && echo "RUN /usr/sbin/grub-mkconfig -o /boot/grub/grub.cfg"
			/usr/sbin/grub-mkconfig -o /boot/grub/grub.cfg
		else
			echo "Warning: /boot/grub/grub.cfg not accessible. Skipping grub setup."
		fi
	else
		echo "grub-mkconfig not found Skipping grub setup."
	fi
	
	# Generate newt bootsplash
	test -n "$_VERBOSE" && echo "RUN /usr/sbin/make-initrd"
	/usr/sbin/make-initrd
fi
