#!/bin/sh

# ADD BASH COMPLETION
rm -R -f ~/.cache/junest-list
echo -ne "$(ls ~/.junest/usr/bin_wrappers/ )"\\r >> ~/.cache/junest-list
echo "\n--help\n--app\n--delete\n--disable-completion\n--enable-completion\n--remove\n--service\n--update\n--version\n--media\n--sudo" >> ~/.cache/junest-list

# OPTIONS
case "$1" in
	'') echo "\n USAGE:\n\n   junest-export {OPTION}\n   junest-export {OPTION} {APPNAME}\n   junest-export {OPTION} {APPNAME} {FLAGS}\n\n Run 'junest-export -h' or 'junest-export --help' for more information\n";;
	'-h'|'--help')
		echo "\n USAGE:\n\n   junest-export {OPTION}\n   junest-export {OPTION} {APPNAME}\n   junest-export {OPTION} {APPNAME} {FLAGS}\n"
		echo " EXAMPLES:\n\n   junest-export --update\n   junest-export --app celestia\n   junest-export --app kdenlive --media\n   junest-export --service pamac --sudo\n"
		echo " OPTIONS:\n"
		echo "   --help/-h:            shows this message"
		echo "   --app/-a:             name of the application to export"
		echo "   --disable-completion  disable bash completion"
		echo "   --enable-completion   enable bash completion"
		echo "   --remove/--delete/-r  delete exported application or service"
		echo "   --service/-s:         name of the service to export"
		echo "   --update/-u:          update this script (requires 'wget')"
		echo "   --version/-v:         shows the version of this script"
		echo "\n FLAGS:\n"
		echo "   --app/-a:             action on an exported application"
		echo "   --media/-m:           enable the support for drives mounted in /media"
		echo "   --service/-s:         action on an exported service"
		echo "   --sudo/-S:            specify if the exported item should be run as sudo\n"
		;;
	'-a'|'--app')
		if [ -n "$SUDO_COMMAND" ]; then echo -e "\n WARNING: You don't need to run the option '$1' with SUDO.\n"; exit; fi
		while [ -n "$1" ]
		do
		desktopfile=$(ls ~/.junest/usr/share/applications/ | grep -i $2 | head -1)
		if test -f ~/.junest/usr/share/applications/$desktopfile > /dev/null 2>&1; then
			rm -R -f ~/.cache/junest-export
			mkdir ~/.cache/junest-export
			if test -f ~/.junest/usr/share/applications/*$2* > /dev/null 2>&1; then
				cp ~/.junest/usr/share/applications/*$2* ~/.cache/junest-export > /dev/null 2>&1
			else
				cp ~/.junest/usr/share/applications/$desktopfile ~/.cache/junest-export > /dev/null 2>&1
			fi
			appiconname=$(cat ~/.cache/junest-export/* > /dev/null 2>&1 | grep Icon | cut -c 6- | head -1)
			addmedia='-b "--bind /media /media"'
			sed -i "s#Exec=#Exec=sh -c '~/.local/share/junest/bin/junest -- #g" ~/.cache/junest-export/* > /dev/null 2>&1
			sed -i "/^Exec=/ s/$/$(echo \')/" ~/.cache/junest-export/* > /dev/null 2>&1
			while [ -n "$3" ]
			do
			case "$3" in
				'-S'|'--sudo')	sed -i "s#junest #junest -f #g" ~/.cache/junest-export/* > /dev/null 2>&1; break;;
				'-m'|'--media')	sed -i "s#junest #junest $addmedia #g" ~/.cache/junest-export/* > /dev/null 2>&1; break;;
				''|*)	break;;
			esac
			done
			while [ -n "$4" ]
			do
			case "$4" in
				'-S'|'--sudo')	sed -i "s#junest #junest -f #g" ~/.cache/junest-export/* > /dev/null 2>&1; break;;
				'-m'|'--media')	sed -i "s#junest #junest $addmedia #g" ~/.cache/junest-export/* > /dev/null 2>&1; break;;
				''|*)	break;;
			esac
			done
			mkdir -p ~/.local/share/applications
			mv ~/.cache/junest-export/* ~/.local/share/applications/ > /dev/null 2>&1
			rm -R -f ~/.cache/junest-export
			mkdir -p ~/.local/share/icons
			ln -s ~/.junest/usr/share/icons/hicolor ~/.local/share/icons/hicolor > /dev/null 2>&1
			cp ~/.junest/usr/share/pixmaps/*$appiconname* ~/.local/share/icons/ > /dev/null 2>&1
			echo " $2 has been successfully exported!"
			exit
		else
			echo " $2 is not a valid argument or is not installed."; break;
		fi
		done;;
		
	'-r'|'--remove'|'--delete')
		if [ -n "$SUDO_COMMAND" ]; then echo -e "\n WARNING: You don't need to run the option '$1' with SUDO.\n"; exit; fi
		while [ -n "$1" ]
		do
		case "$3" in
			'-a'|'--app') appdesktopfile=$(ls ~/.local/share/applications/ | grep -i $2 | head -1)
				if test -f ~/.local/share/applications/$appdesktopfile > /dev/null 2>&1; then
					rm -f ~/.local/share/applications/*$2* > /dev/null 2>&1
					rm -f ~/.local/share/applications/$appdesktopfile > /dev/null 2>&1
					echo " $2 has been successfully removed!"
					break
				else
					echo " ERROR: there is no *.desktop file exported for the app $2."; break
				fi;;
			'-s'|'--service') servicedesktopfile=$(ls ~/.junest/etc/xdg/autostart/ | grep -i $2 | head -1)
				if test -f ~/.config/autostart/$servicedesktopfile > /dev/null 2>&1; then
					rm -f ~/.config/autostart/*$2* > /dev/null 2>&1
					rm -f ~/.config/autostart/$servicedesktopfile > /dev/null 2>&1
					echo " $2 has been successfully removed!"
					break
				else
					echo " ERROR: there is no *.desktop file exported for the service '$2'."; break
				fi;;
			''|*) echo "\n USAGE:\n\n   junest-export --remove {APPNAME} {FLAG}\n\n"
				echo " FLAGS:\n\n   --app/-a:             the item is an application\n   --service/-s:         the item is a service\n"
				echo " EXAMPLES:\n\n   junest-export --remove kdenlive --app\n   junest-export --remove pamac --service\n"; exit;;
		esac
		done;;
		
		
	'-s'|'--service')
		if [ -n "$SUDO_COMMAND" ]; then echo -e "\n WARNING: You don't need to run the option '$1' with SUDO.\n"; exit; fi
		while [ -n "$1" ]
		do
		desktopfile=$(ls ~/.junest/etc/xdg/autostart/ | grep -i $2 | head -1)
		if test -f ~/.junest/etc/xdg/autostart/$desktopfile > /dev/null 2>&1; then
			rm -R -f ~/.cache/junest-export
			mkdir ~/.cache/junest-export
			if test -f ~/.junest/etc/xdg/autostart/*$2* > /dev/null 2>&1; then
				cp ~/.junest/etc/xdg/autostart/*$2* ~/.cache/junest-export > /dev/null 2>&1
			else
				cp ~/.junest/etc/xdg/autostart/$desktopfile ~/.cache/junest-export > /dev/null 2>&1
			fi
			appiconname=$(cat ~/.cache/junest-export/* > /dev/null 2>&1 | grep Icon | cut -c 6- | head -1)
			addmedia='-b "--bind /media /media"'
			sed -i "s#Exec=#Exec=sh -c '~/.local/share/junest/bin/junest -- #g" ~/.cache/junest-export/* > /dev/null 2>&1
			sed -i "/^Exec=/ s/$/$(echo \')/" ~/.cache/junest-export/* > /dev/null 2>&1
			while [ -n "$3" ]
			do
			case "$3" in
				'-S'|'--sudo')	sed -i "s#junest #junest -f #g" ~/.cache/junest-export/* > /dev/null 2>&1; break;;
				'-m'|'--media')	sed -i "s#junest #junest $addmedia #g" ~/.cache/junest-export/* > /dev/null 2>&1; break;;
				''|*)	break;;
			esac
			done
			while [ -n "$4" ]
			do
			case "$4" in
				'-S'|'--sudo')	sed -i "s#junest #junest -f #g" ~/.cache/junest-export/* > /dev/null 2>&1; break;;
				'-m'|'--media')	sed -i "s#junest #junest $addmedia #g" ~/.cache/junest-export/* > /dev/null 2>&1; break;;
				''|*)	break;;
			esac
			done
			mkdir -p ~/.config/autostart
			mv ~/.cache/junest-export/* ~/.config/autostart/ > /dev/null 2>&1
			rm -R -f ~/.cache/junest-export
			mkdir -p ~/.local/share/icons
			ln -s ~/.junest/usr/share/icons/hicolor ~/.local/share/icons/hicolor > /dev/null 2>&1
			cp ~/.junest/usr/share/pixmaps/*$appiconname* ~/.local/share/icons/ > /dev/null 2>&1
			echo " $2 has been successfully exported!"
			exit
		else
			echo " $2 is not a valid argument or is not installed."; break;
		fi
		done;;
	'-u'|'--update')
		DIR="$( cd "$( dirname "$0" )" && pwd )"
		wget -q https://raw.githubusercontent.com/ivan-hc/junest/master/bin/junest-export -O /tmp/junest-export.sh && chmod a+x /tmp/junest-export.sh
		mv /tmp/junest-export.sh $DIR/junest-export > /dev/null 2>&1
		echo " This script is updated to the version $($DIR/junest-export -v)";;
  '--disable-completion')
		if [ $(cat ~/.bash_completion | grep -o appman | head -1) == appman  ]; then
			grep -v 'complete -W "$(cat ~/.cache/junest-list 2>/dev/null)" junest-export' ~/.bash_completion > ~/.bash_completion.backup &&
			mv ~/.bash_completion.backup ~/.bash_completion
			echo " Completion disabled! "
		else
			echo " Completion script is NOT enabled! "
		fi;;
  '--enable-completion')
		if test -f ~/.bash_completion; then
			echo 'complete -W "$(cat ~/.cache/junest-list 2>/dev/null)" junest-export' >> ~/.bash_completion
			echo " Completion enabled! "
		else
		 	echo '#!/usr/bin/env bash' >> ~/.bash_completion
		 	echo 'complete -W "$(cat ~/.cache/junest-list 2>/dev/null)" junest-export' >> ~/.bash_completion
		 	chmod a+x ~/.bash_completion
		 	echo " Completion enabled! "
		fi;;
	'-v'|'--version') echo "0.0.4-2";;
	*) junest-export;;
esac