#!/bin/bash

KXDRSCVER="0.4"

echo "KXDocker Resource installer" $KXDRSCVER

find_KDE_global()
{
	MYKDE="$1"

	if ! [ "$MYKDE" == "" ];
	then
		echo "$MYKDE";
		return;
	fi

	if ! [ "$KDEDIR" == "" ];
	then
		MYKDE="$KDEDIR";
	echo "$MYKDE"
		return;
	fi

	MYKDE=`kde-config --prefix`
	echo "$MYKDE"
	return;
}

find_KDE_local()
{
	MYKDE="$1"

	if ! [ "$MYKDE" == "" ];
	then
		echo "$MYKDE";
		return;
	fi

	MYKDE=`kde-config --localprefix`
	echo "$MYKDE"
	return;
}

pass3()
{
		 case "$1" in
			 yes)
			 echo "installing to:  ${KXDPREFIX}/share/apps/kxdocker"
			 ! mkdir -p ${KXDPREFIX}/share/apps/kxdocker/themes && kdialog --error "Error while creating: ${KXDPREFIX}/share/apps/kxdocker/themes" && exit 1
			 ! tar -jxf themes.tar.bz2 -C ${KXDPREFIX}/share/apps/kxdocker/ && kdialog --error "Error while decompressing themes" && exit 1
			 ! mkdir -p ${KXDPREFIX}/share/apps/kxdocker/plugins && kdialog --error "Error while creating: ${KXDPREFIX}/share/apps/kxdocker/plugins" && exit 1
			 ! tar -jxf plugins.tar.bz2 -C ${KXDPREFIX}/share/apps/kxdocker/ && kdialog --error "Error while decompressing plugins" && exit 1
			 ! mkdir -p ${KXDPREFIX}/share/apps/kxdocker/sounds && kdialog --error "Error while creating: ${KXDPREFIX}/share/apps/kxdocker/sounds" && exit 1
			 ! tar -jxf sounds.tar.bz2 -C ${KXDPREFIX}/share/apps/kxdocker/ && kdialog --error "Error while decompressing sounds" && exit 1
			 
			 kdialog --msgbox "Installation successfully into: ${KXDPREFIX}/share/apps/kxdocker"
			
			SURE2=`kdialog --yesno "Do you like to install default configuration into: ${KXDPREFIX}/share/apps/kxdocker/kxdocker_conf.xml? (pass 1/1)" && echo "yes"`;
			if [ "$SURE2" == "yes" ];
			then
				! cp kxdocker_conf.xml ${KXDPREFIX}/share/apps/kxdocker/ && kdialog --error "Error while installing configuration: ${KXDPREFIX}/share/apps/kxdocker/kxdocker_conf.xml" && exit 1
				
				kdialog --msgbox "Installation successfully into: ${KXDPREFIX}/share/apps/kxdocker"
			fi
			 
			 ;;
			 *)
		echo "Installation aborted";
		exit 1;

			 ;;
		 esac
}

pass2()
{
	KXDPREFIX=`kdialog --inputbox "Please insert PREFIX where install resources (pass 2/3)" "$KXDPREFIX"`
	if ! [ "$KXDPREFIX" == "" ];
	then
		SURE=`kdialog --yesno "Are you sure to install KXDocker resources in ${KXDPREFIX}/share/apps/kxdocker? (pass 3/3)" && echo "yes"`;
		pass3 $SURE;
	else
              echo "Installation aborted";
              exit 1;
	fi
}


if ! [ "$DISPLAY" == "" ];
then
	kdialog --msgbox "KXDocker Resource installer $KXDRSCVER";
	ISLOCAL=`kdialog --radiolist "Where do you like to install resources (pass 1/3)" local "Local user directory (HOME)" 0 global "Global MultiUser install (KDEDIR)" 0`

	case "$ISLOCAL" in
		global)
			KXDPREFIX=`find_KDE_global "$1"`
			pass2
		;;
		local)
			KXDPREFIX=`find_KDE_local "$1"`
			pass2
		;;
		*)
		echo "Installation aborted";
		exit 1;
		;;
	esac

else
	echo "You have to run this script under X environment with KDE correctly installed";
	exit 1;
fi

