#!/bin/bash

throw()
{
	echo 'error:' $@
	exit 1
}

throw_missing_libs()
{
	throw 'libraries cannot be installed'
}

reset()
{
	OPTIONS=$@
	ENABLE_GTK=
	ENABLE_KDE=
}

cat <<EOF

This will build, install and run PieDock. If there are libraries or tools
missing, this script will try to install them through your local package
management system. In order to do that, you will be required to give your
root password or to run this script as root.

NOTE: If you are on Ubuntu and want Gnome integration, be sure to have
      community-maintained software sources (universe) enabled.

Do you want to build, install and run PieDock now (y/n)?
EOF

read && [[ $REPLY != y ]] && exit 1

# check if this is run in the right place
[ -x configure ] || {
	cd ..
	[ -x configure ] ||
		throw 'missing configure script'
}

# check for the setup script
[ -x sh/setup ] ||
	throw 'missing setup script'

# check for sudo if not run as root
SUDO=
[[ $EUID -ne 0 ]] && {
	SUDO=sudo
	which $SUDO &>/dev/null ||
		throw 'sudo not found and you are not root'
}

# try to guess optional configure options
reset $@
which pgrep &>/dev/null && {
	# check whether GNOME is running
	pgrep gnome-session &>/dev/null && {
		OPTIONS="$OPTIONS${OPTIONS:+ }--enable-gtk"
		ENABLE_GTK=1
	}

	# check whether KDE is running
	pgrep kdeinit &>/dev/null && {
		OPTIONS="$OPTIONS${OPTIONS:+ }--enable-kde"
		ENABLE_KDE=1
	}
}

for (( RUNS=2; RUNS--; ))
do
	# try to configure build for this system
	./configure $OPTIONS && {
		make ||
			throw 'build failed, check the top most error'

		$SUDO make install-strip ||
			throw 'installation failed, check the top most error'

		sh/setup ||
			throw 'setup failed'

		piedock ||
			throw "piedock failed to start $HOME/.piedockrc"

		cat <<EOF

Congratulations! PieDock was successfully installed and run!

You may press one of the following combinations to make the menu
appear:

    Windows-key + left mouse button
    Windows-key + scroll wheel up
    Windows-key + scroll wheel down

To fine-tune the experience, check out the configuration file at

$HOME/.piedockrc

You may now erase this directory.

EOF

		break
	}

	# try to get libraries on Ubuntu or Debian
	which apt-get &>/dev/null && {
		$SUDO apt-get -y --no-upgrade install g++ \
			libx11-dev \
			libxmu-dev \
			libxrender-dev \
			libxft2-dev \
			libfreetype6-dev \
			libpng-dev ||
			throw_missing_libs

		(( ENABLE_GTK )) && {
			$SUDO apt-get -y --no-upgrade install \
				gnome-devel ||
				reset $@
		}

		(( ENABLE_KDE )) && {
			$SUDO apt-get -y --no-upgrade install \
				qt4-dev-tools \
				kdelibs5-dev ||
				reset $@
		}

		continue
	}

	# try to get libraries on openSUSE
	which zypper &>/dev/null && {
		# where's Xft on SUSE?
		$SUDO zypper --non-interactive install gcc-c++ \
			make \
			xorg-x11-libX11-devel \
			xorg-x11-libXmu-devel \
			xorg-x11-libXrender-devel \
			freetype2-devel \
			libpng14-devel ||
			throw_missing_libs

		(( ENABLE_GTK )) && {
			$SUDO zypper --non-interactive install -t pattern \
				devel_gnome ||
				reset $@
		}

		(( ENABLE_KDE )) && {
			$SUDO zypper --non-interactive install -t pattern \
				devel_kde ||
				reset $@
		}

		continue
	}

	# try to get libraries on Fedora
	which yum &>/dev/null && {
		$SUDO yum -y install gcc-c++ \
			make \
			libX11-devel \
			libXmu-devel \
			libXrender-devel \
			libXft-devel \
			freetype-devel \
			libpng-devel ||
			throw_missing_libs

		(( ENABLE_GTK )) && {
			$SUDO yum -y install \
				gtk2-devel ||
				reset $@
		}

		(( ENABLE_KDE )) && {
			$SUDO yum -y install \
				qt-devel \
				kdelibs-devel ||
				reset $@
		}

		continue
	}

	# build on Gentoo
	which emerge &>/dev/null && {
		# assuming X libraries are installed already
		$SUDO emerge --noreplace libpng \
			libXrender \
			libXft \
			freetype ||
			throw_missing_libs

		(( ENABLE_GTK )) && {
			$SUDO emerge --noreplace \
				x11-libs/gtk+ ||
				reset $@
		}

		(( ENABLE_KDE )) && {
			$SUDO emerge --noreplace \
				qt-gui \
				kdelibs ||
				reset $@
		}

		continue
	}

	throw 'sorry, your package management system is not supported'
done
