#!/bin/bash
# spacefm installer

gtk3=0
hal=0
for opt in "$@"; do
    if [ "$opt" = "--help" ]; then
		./configure --help
		exit
    fi
    if [ "$opt" = "--with-gtk3" ]; then
        gtk3=1
    elif [ "$opt" = "--enable-hal" ]; then
        hal=1
    fi
done

build_deps='autotools-dev bash build-essential fakeroot desktop-file-utils libc6 libcairo2 libglib2.0-0 libpango1.0-0 libx11-6 shared-mime-info intltool pkg-config libglib2.0-dev'
if (( gtk3 )); then
	build_deps="$build_deps libgtk-3-0 libgtk-3-bin libgtk-3-dev"
else
	build_deps="$build_deps libgtk2.0-0 (>=2.18) libgtk2.0-bin libgtk2.0-dev"
fi
if (( hal )); then
	build_deps="$build_deps hal libhal-dev libhal-storage-dev dbus libdbus-glib-1-dev libhal-storage1
 libhal1 libdbus-glib-1-2"
else
	build_deps="$build_deps libudev0 (>=143) libudev-dev"
fi

echo "Running configure:  ./configure --disable-pixmaps \"$@\""
./configure --disable-pixmaps "$@"
if [[ $? -ne 0 ]]; then
	echo
	echo "configure was not successful, probably due to missing build"
	echo "dependencies.  Examine the message above to determine what is missing"
	echo "on your system, install the appropriate package, and try this installer"
	echo "again."
	echo
	echo "Build dependencies include (package names may vary on your distro):"
	echo "${build_deps}"
	echo
	exit 1
fi
echo
echo "Running make... (this can take awhile)"
make -s
if [[ ! -e src/spacefm ]]; then
	echo
	echo "make was not successful, possibly due to missing build"
	echo "dependencies.  Examine the errors above to determine what is missing"
	echo "on your system, install the appropriate packages, and try this installer"
	echo "again.  If no error is displayed, you may need to follow the manual"
	echo "build instructions in the README to see all of make's output."
	echo
	echo "Build dependencies include (package names may vary on your distro):"
	echo "${build_deps}"
	echo
	exit 1
fi
echo

echo "SpaceFM appears to have been built successfully.  To install it, you"
echo "may need to enter your root or administrator password below..."
echo
wh_sudo="$( which sudo 2>/dev/null )"
if [[ -z "$wh_sudo" ]]; then
	echo "Running su -c \"make install\""
	su -c "make install"
else
	echo "Running sudo make install"
	sudo make install
fi
if [[ $? -ne 0 ]]; then
	echo
	echo "Error: make install was not successful."
	echo
	exit 1
fi

echo
echo "Updating databases..."
if [[ -z "$wh_sudo" ]]; then
	echo "Running su -c \"update-mime-database /usr/share/mime\""
	su -c "update-mime-database /usr/share/mime"
	echo "Running su -c update-desktop-database -q\""
	su -c "update-desktop-database -q"
	echo "Running su -c \"gtk-update-icon-cache -q -t -f /usr/share/icons/hicolor\""
	su -c "gtk-update-icon-cache -q -t -f /usr/share/icons/hicolor"
	if [[ -d /usr/local/share/icons/hicolor ]]; then
		echo "Running su -c \"gtk-update-icon-cache -q -t -f /usr/local/share/icons/hicolor\""
		su -c "gtk-update-icon-cache -q -t -f /usr/local/share/icons/hicolor"
	fi
	if [[ -d /usr/local/share/icons/Faenza ]]; then
		echo "Running su -c \"gtk-update-icon-cache -q -t -f /usr/local/share/icons/Faenza\""
		su -c "gtk-update-icon-cache -q -t -f /usr/local/share/icons/Faenza"
	fi
	echo "Running su -c \"gtk-update-icon-cache -q -t -f /usr/share/icons/hicolor\""
	su -c "gtk-update-icon-cache -q -t -f /usr/share/icons/hicolor"
	if [[ -d /usr/share/icons/Faenza ]]; then
		echo "Running su -c \"gtk-update-icon-cache -q -t -f /usr/share/icons/Faenza\""
		su -c "gtk-update-icon-cache -q -t -f /usr/share/icons/Faenza"
	fi
else
	echo "Running sudo update-mime-database /usr/share/mime > /dev/null"
	sudo update-mime-database /usr/share/mime > /dev/null
	echo "Running sudo update-desktop-database -q"
	sudo update-desktop-database -q
	if [[ -d /usr/local/share/icons/hicolor ]]; then
		echo "Running sudo gtk-update-icon-cache -q -t -f /usr/local/share/icons/hicolor"
		sudo gtk-update-icon-cache -q -t -f /usr/local/share/icons/hicolor
	fi
	if [[ -d /usr/local/share/icons/Faenza ]]; then
		echo "Running sudo gtk-update-icon-cache -q -t -f /usr/local/share/icons/Faenza"
		sudo gtk-update-icon-cache -q -t -f /usr/local/share/icons/Faenza
	fi
	echo "Running sudo gtk-update-icon-cache -q -t -f /usr/share/icons/hicolor"
	sudo gtk-update-icon-cache -q -t -f /usr/share/icons/hicolor
	if [[ -d /usr/share/icons/Faenza ]]; then
		echo "Running sudo gtk-update-icon-cache -q -t -f /usr/share/icons/Faenza"
		sudo gtk-update-icon-cache -q -t -f /usr/share/icons/Faenza
	fi
fi

echo
echo "Installation appears successful."
echo

exit

