#!/bin/bash

# Copyright 2013 Chiraag M. Nataraj
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

save () {
echo "#!/usr/bin/env xdg-open

[Desktop Entry]
Version=1.0
Name= "$2"
GenericName=Web Browser
Exec=firefox -P WebApps -no-remote $1
Terminal=false
X-MultipleArgs=false
Type=Application
Icon=firefox
Categories=Application;Network;
MimeType=text/html;
StartupWMClass=Firefox
StartupNotify=true" > ~/.local/share/applications/"$2".desktop
}

inst() {
if [ $# -eq 3 ]
then
	save "$2" "$3";
fi 
if [ $# -eq 2 ]
then
	echo "Please give the program either three arguments or one argument - the first is install and the optional second and third arguments are the url of the website and the name of the menu item respectively.  Thanks :)"
	exit
fi
URL=`zenity --entry --title="appify - Chiraag Nataraj" --text="Enter complete URL of website:"`
if [ $? != 0 ] 
then 
	exit
fi
NAME="`zenity --entry --title="appify - Chiraag Nataraj" --text="Enter desired name of menu item:"`"
if [ $? -ne 0 ]
then
	exit
fi
test "$URL" = "";
RETURN3=$?
test "$NAME" = "";
RETURN4=$?
if [[ ($RETURN3 -eq 0) || ($RETURN4 -eq 0) ]]
then
	zenity --error --title="appify - Chiraag Nataraj" --text="Please retry, giving values for both dialog boxes!  Thanks :)"
exit
fi
save "$URL" "$NAME";
}

uninst() {
if [ $# -eq 3 ]
then
	echo "Please give the program two arguments - the first is uninstall and the second is the name of the menu item to uninstall.  Thanks :)"
	exit
fi
if [ $# -eq 2 ]
then
	IS_IN_LIST=`ls ~/.local/share/applications/ | grep "$2"`;
	test "$IS_IN_LIST" = ""
	RETURN=$?;
	if [[ $RETURN == 1 ]]
	then
		rm ~/.local/share/applications/"$2".desktop
		exit
	else
		if [ "$GRAPHICAL" == "true" ]
		then
		zenity --error --title="appify - Chiraag Nataraj" --text="Cannot remove - please install first!"
		exit
		else
		echo "Cannot remove - please install first!"
		exit
		fi
	fi
fi
UNINST=`zenity --entry --title="appify - Chiraag Nataraj" --text="Enter name of menu item to remove:"`
if [ $? -ne 0 ]
then
	exit
fi
GRAPHICAL="true"
uninst "uninstall" "$UNINST"
}

echo "Created by Chiraag Nataraj
Copyright 2010"

if [ -n "$1" ]
then
	if [ "$1" = "install" ]
	then
		inst "$1" "$2" "$3"
		exit 0
	fi
	if [ "$1" = "uninstall" ]
	then
		uninst "$1" "$2"
	fi
fi

if [ $# -eq 0 ]
then
	ans=`zenity --list --title="appify - Chiraag Nataraj" --text="What do you want to do?" --column=Pick --column=Option --radiolist A Install B Uninstall --hide-header`;
	if [ "$ans" = "Install" ]
	then
		inst
	fi
	if [ "$ans" = "Uninstall" ]
	then
		uninst
	fi
	if [ -z "$ans" ]
	then
		exit 0
	fi
fi
