#!/bin/sh
# This application is released under the GNU General Public License
# v3 (or, at your option, any later version). You can find the full
# text of the license under [url]http://www.gnu.org/licenses/gpl.txt[/url].
# By using, editing and/or distributing this software you agree to
# the terms and conditions of this license.
# Thank you for using free software!

PREFIX=$(cat /etc/screenlets/prefix)
if [ -e $PREFIX/share/screenlets-manager/screenlets-manager.py ]; then
	PREFIX=$(cat /etc/screenlets/prefix)
else
	PREFIX=/usr
fi
screenlet_folders="$HOME/.screenlets $PREFIX/share/screenlets"

runDaemon()
{
if [ -z "`ps -f -C python | grep screenlets-daemon`" ]; then            #Check if Daemon is running
        printf "Loading Daemon"           #Load Daemon
        $PREFIX/share/screenlets-manager/screenlets-daemon.py > /dev/null &
else
        printf "Daemon is already running"
fi
}

listScreenlets()
{
for l in $screenlet_folders; do
    if [ -d "$l" ]; then
        for d in `ls -1 $l`; do
            if [ -d $l/$d ]; then
                screenletpy="$l/$d/${d}Screenlet.py"
                if [ -e $screenletpy ]; then
                    printf "$d\n"
                fi
            fi
        done
    fi
done
}

startgui()
{
exec python -u $PREFIX/share/screenlets-manager/screenlets-manager.py $@             #Load Gui
}

findScreenlet()
{
screenlet="$1"

#find new screenlet
for h in $screenlet_folders; do
    if [ -e $h/$screenlet/${screenlet}Screenlet.py ]; then
        location="$h/$screenlet/${screenlet}Screenlet.py"
    fi
done
if [ -z $location ]; then
    printf "There is no screenlet named $screenlet"
    exit 1
fi
}

auScreenlet()
{
AU_DIR="$HOME/.config/autostart"                #Autostart folder
printf "[Desktop Entry]\n" > $AU_DIR/${1}Screenlet.desktop              #Make a .desktop entry to autostart the screenlet
printf "Name=${1}Screenlet\n" >> $AU_DIR/${1}Screenlet.desktop
printf "Version=1.0\n" >> $AU_DIR/${1}Screenlet.desktop
printf "Exec=python -u $location > /dev/null\n" >> $AU_DIR/${1}Screenlet.desktop
printf "X-GNOME-Autostart-enabled=true\n" >> $AU_DIR/${1}Screenlet.desktop
}

runScreenlet()
{
        $location > /dev/null &         #Run Screenlet
}

help_me()                       #Help
{
printf "  Options:\n"
printf "\t --gui \t\t\t Open up the Graphical Interface\n"
printf "\t --cli \t\t\t Use the Command Line interface\n"
printf "  Cli Options (only use one at a time):\n"
printf "\t    run \t\t\t Run the Screenlet\n"
printf "\t    au \t\t\t Add the Screenlet to your autostart list\n"
printf "\t    list \t\t\t List all installed screenlets\n"
}

if [ x$1 = x"--gui" ]; then            #Decide what to do
        runDaemon
    MANAGER_OPTIONS=`printf $@ | sed -e s/$1//`
        startgui $MANAGER_OPTIONS
        exit 0
elif [ x$1 = x"--cli" ]; then
        if [ x$2 = x"run" ]; then
                findScreenlet $3
                runScreenlet
                exit 0
        elif [ x$2 = x"au" ]; then
                findScreenlet $3
                auScreenlet $3
                exit 0
    elif [ x$2 = x"list" ]; then
        listScreenlets
        exit 0
        else
                help_me
                exit 1
        fi
else
        help_me
        exit 1
fi
