#!/bin/bash
#########################################################
#							#
# This are NScripts v3.4				#
#							#
# Licensed under the GNU GENERAL PUBLIC LICENSE 3	#
#							#
# Copyright 2007 - 2008 Christopher Bratusek		#
#							#
#########################################################

if [[ -x /usr/bin/gksu || -x /opt/gnome/bin/gksu ]]; then
	sudotool="gksu -u root"
elif [[ -x /usr/bin/gnomesu || -x /opt/gnome/bin/gnomesu ]]; then
	sudotool=gnomesu
fi

if [[ $sudotool == "" ]]; then
	destination=$(zenity --list --radiolist --width="270" --height="350" --text "Where to install Theme?" --title "Destination ..." --column "Pick" --column "Destination" TRUE "$HOME/.themes/" FALSE "$HOME/.sawfish/themes/" FALSE "$HOME/.exaile/" FALSE "$HOME/.icons/" FALSE "$HOME/.mplayer/skins/" FALSE "$HOME/.cairo-clock/theme/")
else	destination=$(zenity --list --radiolist --width="270" --height="400" --text "Where to install Theme?" --title "Destination ..." --column "Pick" --column "Destination" TRUE "$HOME/.themes/" FALSE "$HOME/.sawfish/themes/" FALSE "/usr/share/sawfish/themes/" FALSE "$HOME/.exaile/" FALSE "/usr/share/themes/" FALSE "$HOME/.icons/" FALSE "/usr/share/icons/" FALSE "$HOME/.mplayer/skins/" FALSE "/usr/share/mplayer/skins/" FALSE "/usr/share/gdm/" FALSE "$HOME/.cairo-clock/theme/" FALSE "/usr/share/cairo-clock/themes/" FALSE "/boot/grub/")
fi

if [[ ! -a $destination ]]; then
	mkdir -p $destination
fi

echo $NAUTILUS_SCRIPT_SELECTED_URIS > ~/.gnome2/temp_theme_list

for file in $(cat ~/.gnome2/temp_theme_list); do

file_name=$(echo $file | sed -e 's/file:\/\///g' -e 's/\%20/\ /g')

shortfile=$(echo $file | sed -e 's/\%20/\\ /g' -e 's/.*\///g' -e 's/.tar.*//g')

	if [[ -d "$file_name" ]] ; then \
		action=copy;
	elif [[ $(echo "$file_name") == *tar* ]]; then \
		action=unpack;
	else	action=copy;
	fi

	if [[ $destination != /usr/share/themes/ && $destination != /usr/share/icons/ && $destination != /usr/share/sawfish/themes/ && $destination != $HOME/.exaile/ && $destination != /usr/share/mplayer/skins && $action == copy ]]; then \
		cp -r "$file_name" $destination | zenity --progress --pulsate --title "Finishing" text "extracting $shortfile" --auto-close;
	elif [[ $destination != /usr/share/themes/ && $destination != /usr/share/icons/ && $destination != /usr/share/sawfish/themes && $destination != $HOME/.exaile/ && $destination != /usr/share/mplayer/skins && $action == unpack ]]; then \
		tar xf "$file_name" -C $destination | zenity --progress --pulsate --title "Finishing" text "extracting $shortfile" --auto-close;
	elif [[ $destination == $HOME/.exaile/ && $action == copy ]]; then
		if [[ -a $destination/exaile.extheme ]]; then
			mv $destination/exaile.extheme{,.old}
		fi
		cp "$file_name" $desination/exaile.extheme
	elif [[ $destination == $HOME/.exaile/ && $action == unpack ]]; then
		if [[ -a $destination/exaile.extheme ]]; then
			mv $destination/exaile.extheme{,.old}
		fi
		tar xf "$file_name" -C $destination
	elif [[ $action == copy ]]; then \
		$sudotool "cp -r '$file_name' $destination";
	elif [[ $action == unpack ]]; then \
		$sudotool "tar xf '$file_name' -C $destination" | zenity --progress --pulsate --title "Finishing" text "extracting $shortfile" --auto-close;
	fi

if (( $? == 0 )); then
	zenity --info --title "Success" --text "$shortfile installed";
else	zenity --info --title "Failure" --text "$shortfile failed to install";
fi;

done

rm -f ~/.gnome2/temp_theme_list

