#!/usr/bin/env bash
#
# firemenu v.1.2 
# fsmithred@gmail.com 2016
# License: GPL-3
# This is free software with NO WARRANTY. Use at your own risk!


# Make a list of installed apps that have a firejail profile.
# Let the user edit the list.
# Present a menu to the user. Start the selected app in firejail.



# You can specify which editor to use if yours is not found,
# or if you prefer a different one.
other_editor=""

# Your chosen editor's option to start a new instance instead of
# opening in a running instance of the editor.
other_editor_option=""

# Uncommented apps in this list will show in the menu.
applist="${HOME}/.config/firemenu.list"


make_menu () {

find_editor
rm -f "$applist"
echo "# Comment out any apps you don't want to show in the menu." > "$applist"
for file in /etc/firejail/*.profile ; do
	app=$(echo $file | sed 's/\(.*\)\..*/\1/')
	if [ -e /var/lib/dpkg/info/"${app##*/}.list" ] ; then
			echo "${app##*/}" >> "$applist" 
	fi
done
"$gui_editor" "${editor_option}" "$applist"
show_menu

}

comment_all () {

sed -i 's/^\([^#].*\)/# \1/g' "$applist"

}

show_menu () {
	
#if grep -v "#" "$applist" | grep [[:alpha:]]  ; then
	menu_text="Some profiles may not appear in this list.\n(See ${HOME}/.config/firemenu.list)\n"
#fi

selection=$(grep -v "#" "$applist" | yad --list --height 400 --title="FireMenu"  --text="${menu_text}" \
	--separator ""  --column "Application" --button=Edit:4 --button=Reset:3 --button=Unset:2 --button=Cancel:1 --button=OK:0)
	ans="$?"
	if [ $ans -eq 1 ] ; then
		exit 0
	fi

	if [ $ans -eq 2 ] ; then
		comment_all
		find_editor
		"$gui_editor" "${editor_option}" "$applist"
		show_menu
	fi

	if [ $ans -eq 3 ] ; then
		make_menu
	fi

	if [ $ans -eq 4 ] ; then
		find_editor
#		sed -i 's/^/#/g' "$applist"
		"$gui_editor" "${editor_option}" "$applist"
		show_menu
	fi

	if [ $ans -eq 0 ] ; then
		if [ -n "$selection" ] ; then
			firejail --seccomp "$selection" &
			exit 0
		fi
	fi

}


find_editor () {
	if [ -n "$other_editor" ] ; then
		gui_editor="$other_editor"
		editor_option="$other_editor_option"	
	elif  [ -e /usr/bin/geany ] ; then
		gui_editor="/usr/bin/geany"
		editor_option="-i"	
	elif  [ -e /usr/bin/gedit ] ; then
		gui_editor="/usr/bin/gedit"
		editor_option=""
	elif  [ -e /usr/bin/kate ] ; then
		gui_editor="/usr/bin/kate"
		editor_option=""
	elif  [ -e /usr/bin/kwrite ] ; then
		gui_editor="/usr/bin/kwrite"
		editor_option=""
	elif  [ -e /usr/bin/leafpad ] ; then
		gui_editor="/usr/bin/leafpad"
		editor_option="--sync"
	elif  [ -e /usr/bin/medit ] ; then
		gui_editor="/usr/bin/medit"
		editor_option="-n"
	elif  [ -e /usr/bin/mousepad ] ; then
		gui_editor="/usr/bin/mousepad"
	elif  [ -e /usr/bin/xed ] ; then
		gui_editor="/usr/bin/xed"
	else
		echo " No suitable editor found. 
 You must set the other_editor variable in
  $0. 

 Exiting..."
		yad --warning --image=gtk-dialog-warning --title="Warning" --center --text="  No suitable editor found.  
  You must set the other_editor variable in  
  $0.  

  Exiting..." --button=OK:0
		exit 1
	fi
}


show_menu

exit 0
