#!/bin/bash
#---------------------------------------------
#   icons.sh
#       Copyright 2006 by Google
#
#   Utility script to create icons on a Linux desktop.
#   Works on most XDG compliant Linux systems; does
#   not work everywhere.
#
#   Refer to the usage() function below for usage.
#---------------------------------------------

usage()
{
    echo Usage:
    echo  $0 "[--root|--user] [--create desktop|--delete desktop]"
    echo "Where --user (install only for the user file) is the default."
    echo One of --create or --delete must be specified.
    echo With --create, the file specified must exist and be a valid XDG .desktop file
    echo With --delete, the file must be a pattern that will exist
}



# Locate where our root is installed by looking for the directory
# where this this script is located, unwinding symlinks on the way
# find the xdg-utils we want to call within it.
icons_locate_xdg_utils()
{
    if [ -z "$PIC_ROOT" ]
    then
        argv0=`which "$0"`
        [ -z "$argv0" ] && argv0="$0"
        echo "$argv0" | egrep "^/" >/dev/null
        if [ $? -ne 0 ]
        then
            argv0="`pwd`/$argv0"
        fi
        dir=`dirname "$argv0"`
        while [ ! -x "$dir/../wine/bin/wine" ]
        do
            [ ! -h "$argv0" ] && break
            argv0=`ls -l "$argv0" | sed -e 's/^.*-> //'`
            echo "$argv0" | egrep "^/" >/dev/null
            if [ $? -ne 0 ]
            then
                argv0="$dir/$argv0"
            fi
            dir=`dirname "$argv0"`
        done
        dir=`echo "$dir" | sed -e 's%\(/\.\)*$%%' -e 's%\(/\./\(\./\)*\)%/%'`
        PIC_ROOT="$dir/.."
        PIC_BINDIR="$dir"
    fi

    # Make sure the system has a version of XDG_DESKTOP_MENU > 1.0.2
    # or else we have to use our own copy
    USE_PICASA_XDG_DESKTOP_MENU=1
    XDG_VER="`xdg-desktop-menu --version 2> /dev/null | cut -d" " -f 2`"
    if [ -n "$XDG_VER" ] ; then
        XDG_VER_MAJOR=`echo $XDG_VER | cut -d"." -f 1`
        XDG_VER_MINOR=`echo $XDG_VER | cut -d"." -f 2`
        XDG_VER_REVISION=`echo $XDG_VER | cut -d"." -f 3`
        if [ "$XDG_VER_MAJOR" -gt "1" ] ; then
            USE_PICASA_XDG_DESKTOP_MENU=0
        elif [ "$XDG_VER_MAJOR" -eq "1" ] ; then
            if [ "$XDG_VER_MINOR" -gt "0" ] ; then
                USE_PICASA_XDG_DESKTOP_MENU=0
            else
                if [ -n "$XDG_VER_REVISION" ] && [ "$XDG_VER_REVISION" -gt "2" ] ; then
                    USE_PICASA_XDG_DESKTOP_MENU=0
                fi
            fi
        fi
    fi

    if [ "$USE_PICASA_XDG_DESKTOP_MENU" = "1" ] ; then
        XDG_DESKTOP_MENU="$PIC_ROOT/bin/xdg-utils-1.0.2/scripts/xdg-desktop-menu"
    else
        XDG_DESKTOP_MENU=`which xdg-desktop-menu`
    fi

    if [ ! -x "$XDG_DESKTOP_MENU" ]
    then
        if [ "$1" = "--no-fail" ]
        then
            return 1
        fi
        echo "`basename \"$0\"`:error: could not find xdg-desktop-menu in PATH or '$PIC_ROOT'" >&2
        exit 1
    fi

    # Make sure it's exported
    export XDG_DESKTOP_MENU
    export PIC_ROOT
    return 0
}


create_desktop_menu()
{
    # Older versions of Mandriva has a different menu setup
    if [ -f "/etc/mandrake-release" ] ; then
        DO_STANDARD_XDG=0
        HAS_MANDRIVA_MENU=0
        MANDRIVA_VERSION=`cat /etc/mandrake-release | grep Mandriva | sed 's/^Mandriva Linux release \([0-9]*\)\..*/\1/'`

        if [[ -n "$MANDRIVA_VERSION" && "$MANDRIVA_VERSION" -ge "2008" ]] ; then
            # Mandriva 2008 and onward
            DO_STANDARD_XDG=1
        elif [[ -n "$MANDRIVA_VERSION" && "$MANDRIVA_VERSION" -eq "2007" ]] ; then
            # Mandriva 2007
            MANDRIVA_MULTIMEDIA_DIR="desktop-directories/mandriva-multimedia.directory"
            MANDRIVA_GRAPHICS_DIR="desktop-directories/mandriva-multimedia-graphics.directory"
        else
            # Mandrake, Mandriva 2005/2006
            MANDRIVA_MULTIMEDIA_DIR="desktop-directories/mandrakelinux\$2dmultimedia.directory"
            MANDRIVA_GRAPHICS_DIR="desktop-directories/mandrakelinux\$2dmultimedia\$2dgraphics.directory"
        fi

        if [ "$DO_STANDARD_XDG" -eq "0" ] ; then
            for xdg_dir in `echo "$XDG_DATA_DIRS" | sed 's/:/ /g'` ; do
                if [[ -f "${xdg_dir}/$MANDRIVA_MULTIMEDIA_DIR" && -f "${xdg_dir}/$MANDRIVA_GRAPHICS_DIR" ]] ; then
                    HAS_MANDRIVA_MENU=1
                    break
                fi
            done

            if [ $HAS_MANDRIVA_MENU -eq 1 ] ; then
                "$XDG_DESKTOP_MENU" install --mode $mode --novendor "${xdg_dir}/$MANDRIVA_MULTIMEDIA_DIR" "${xdg_dir}/$MANDRIVA_GRAPHICS_DIR" "$PIC_ROOT/desktop/google-picasa.directory" "$desktop_file"
            else
                "$XDG_DESKTOP_MENU" install --mode $mode --novendor "$PIC_ROOT/desktop/google-picasa.directory" "$desktop_file"
            fi
            return
        fi
    fi

    # For all the other major Linux distros.
    # We prefer to put our menu under graphics, but will fall back to something
    # else if we can. We prefer KDE menus to Gnome menus because Gnome tolerates
    # KDE's menus better than KDE tolerate Gnome's.

    catagory_dir=
    for catagory in suse-graphics kde-graphics Graphics Photography \
        Photograph Viewer 2DGraphics suse-applications kde-applications \
        Application Applications ; do
        for xdg_dir in `echo "$XDG_DATA_DIRS" | sed 's/:/ /g'` ; do
            if [ -f "${xdg_dir}/desktop-directories/${catagory}.directory" ] ; then
                catagory_dir="${xdg_dir}/desktop-directories/${catagory}.directory"
                break
            fi
        done
        if [ -n "$catagory_dir" ] ; then
            break
        fi
    done

    if [ -n "$catagory_dir" ] ; then
        "$XDG_DESKTOP_MENU" install --mode $mode --novendor "$catagory_dir" "$PIC_ROOT/desktop/google-picasa.directory" "$desktop_file"
    else
        "$XDG_DESKTOP_MENU" install --mode $mode --novendor "$PIC_ROOT/desktop/google-picasa.directory" "$desktop_file"
    fi

    # As a last resort, just copy the file over if xdg-desktop-menu fails
    if [ $? -gt 0 ] ; then
        if [ "$mode" = "user" ] ; then
            APPLICATIONS_DIR="$HOME/.local/share/applications"
        else
            APPLICATIONS_DIR="/usr/share/applications"
        fi
        if [ -d "$APPLICATIONS_DIR" ] ; then
            /bin/cp "$desktop_file" "$APPLICATIONS_DIR"
            echo "Categories=Graphics;Photography;Photograph;Viewer;2DGraphics;Application;Applications" >> "$APPLICATIONS_DIR/$(basename "$desktop_file")"
        fi
    fi
}


icons()
{

    mode=user
    action=
    while [ $# -gt 0 ] ; do
        parm=$1
        shift

        case $parm in
          --create)
            if [ ! -f "$1" ] ; then
                echo Error:  You must specify a valid desktop file as a parameter to --create.
                return 1
            fi
            if [ -n "$action" ] ; then
                echo Error:  You must specify only one of --create or --delete
                return 1
            fi

            action=create
            desktop_file=$1
            shift
            ;;

          --delete)
            if [ -z "$1" ] ; then
                echo Error:  You must specify a desktop file name as a parameter to --delete.
                return 1
            fi
            if [ -n "$action" ] ; then
                echo Error:  You must specify only one of --create or --delete
                return 1
            fi
            action=delete
            desktop_file=$1
            shift
            ;;

          --user)
            mode=user
            ;;

          --root)
            mode=system
            ;;

          *)
            echo "$parm:  Invalid parameter/option"
            usage
            return 2
            ;;
        esac
    done

    if [ -z "$action" ] ; then
        usage
        return 2
    fi


    icons_locate_xdg_utils

    [ -n "$XDG_DATA_DIRS" ] || XDG_DATA_DIRS=/usr/share/:/usr/local/share/

    # Some distros have misconfigured XDG_CONFIG_DIRS. We refuse to use desktop
    # environment specific XDG_CONFIG_DIRS if /etc/xdg exists.
    if [ -d "/etc/xdg" ] ; then
        NEW_XDG_CONFIG_DIRS=
        for path in `echo "$XDG_CONFIG_DIRS" | sed 's/:/ /g'` ; do
            if [[ "$path" = "/etc/kde/xdg" || "$path" = "/etc/gnome/xdg" || "$path" = "/etc/xdg/kde" || "$path" = "/etc/xdg/gnome" ]] ; then
                continue
            else
                if [ -z "$NEW_XDG_CONFIG_DIRS" ] ; then
                    NEW_XDG_CONFIG_DIRS=$path
                else
                    NEW_XDG_CONFIG_DIRS=$NEW_XDG_CONFIG_DIRS:$path
                fi
            fi
        done
        XDG_CONFIG_DIRS=$NEW_XDG_CONFIG_DIRS
    fi

    case $action in 
        create)
            create_desktop_menu
            ;;

        delete)
            "$XDG_DESKTOP_MENU" uninstall --mode $mode "$PIC_ROOT/desktop/google-picasa.directory" "$desktop_file"

            # Remove manually if xdg-desktop-menu fails
            if [ $? -gt 0 ] ; then
                if [ "$mode" = "user" ] ; then
                    APPLICATIONS_DIR="$HOME/.local/share/applications"
                else
                    APPLICATIONS_DIR="/usr/share/applications"
                fi
                DESKTOP_FILE="$APPLICATIONS_DIR/$(basename "$desktop_file")"
                if [ -f "$DESKTOP_FILE" ] ; then
                    /bin/rm "$DESKTOP_FILE"
                fi
            fi
            ;;
    esac

    return 0

}




icons "$@"

