#!/bin/bash
#       setpicasascreensaver
#   Script to set up Picasa as a screen saver
#
#   FIXME - This script is imperfect, and has many flaws, amongst which are:
#           1.  If there are multiple user installs on a system,
#               they will not coexist peacefully.  Each will clobber
#               the other in turn.  A root install on that system
#               should work okay.
#           2.  The set option really only works the first time.
#               If the user changes screen savers, and notably
#               makes Picasa not be the 0th in the list, then
#               this won't work.
#           3.  Unset only sorta kinda works.  The user will likely
#               have to manually fix their screen saver after removing
#               Picasa
#           4.  There is no support for the new fancy dancy Gnome screen saver
#
#

usage()
{
    echo Usage:
    echo  $0 action
    echo "Where action is one of:"
    echo "  install         -   Install showpicasascreensaver to /usr/[X11R6]/lib/xscreensaver"
    echo "  remove          -   Remove showpicasascreensaver from /usr/[X11R6]/lib/xscreensaver"
    echo "  set             -   Setup a screen saver for the current user, performing the"
    echo "                      install step if it can be done easily, and there is no"
    echo "                      showpicasascreensaver already"
    echo "  unset           -   Cleanup user specific screen saver settings at picasa removal"
}


# Locate where our root is installed by looking for the directory
# where this this script is located, unwinding symlinks on the way
locate_pic_root()
{
    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
    if [ ! -x "$PIC_ROOT/wine/bin/wine" ]
    then
        if [ "$1" = "--no-fail" ]
        then
            PIC_ROOT=""
            return 1
        fi
        #echo "`basename \"$0\"`:error: could not find wine/bin/wine in '$PIC_ROOT'" >&2
        return 1
    fi
    # Make sure it's exported
    export PIC_ROOT
    export PIC_BINDIR
    return 0
}


setpicasascreensaver()
{

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

        case $parm in
          install | remove | set | unset)
            action=$parm
            ;;

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

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


    locate_pic_root

    # If /usr/[X11R6/]lib/screensaver doesn't have showpicasascreensaver, add it there, with XXXsu if need be
    for x in /usr/lib/xscreensaver /usr/X11R6/lib/xscreensaver ; do
        if [ -d "$x" ] ; then
            screen_dir="$x"
            break;
        fi
    done

    # Handle remove actions
    if [ "$action" = "remove" ] ; then
        for x in /usr/lib/xscreensaver /usr/X11R6/lib/xscreensaver ; do
            if [ -f "$x/showpicasascreensaver" ] ; then
                rm -f "$x/showpicasascreensaver"
                break;
            fi
        done

        return 0
    fi

    # Handle install actions
    if [ "$action" = "install" ] ; then
        if [ -d "$screen_dir" ] ; then
            if [ -h "$screen_dir/showpicasascreensaver" ] ; then
                rm -f "$screen_dir/showpicasascreensaver"  
            fi
            ln -s $PIC_BINDIR/showpicasascreensaver "$screen_dir/"
        fi

        return 0
    fi


    # Handle unset actions
    if [ "$action" = "unset" ] ; then
        rm -f ~/.kde/share/applnk/System/ScreenSavers/picasascr.desktop 
        return 0
    fi


    ###
    #   From here on, everything is presumed to be a set option
    #       which is meant to be done just for this user
    ###

    if [ -d "$screen_dir" ] ; then
        if [ ! -f "$screen_dir/showpicasascreensaver" ] ; then
            if [ -n "$KDE_FULL_SESSION" -o "$GDMSESSION" = "KDE" ] ; then
                kdesu -c "ln -s $PIC_BINDIR/showpicasascreensaver $screen_dir/"
            else
                export PATH=$PATH:$PIC_BINDIR
                xsu -u root -c "ln -s $PIC_BINDIR/showpicasascreensaver $screen_dir/"
            fi
        fi
    fi

    # Next, set up ~/.xscreensaver correctly

    if [ ! -f ~/.xscreensaver ] ; then
        echo 'selected: 0 ' >~/.xscreensaver
        echo 'programs: "Picasa Screensaver" showpicasascreensaver' >>~/.xscreensaver
    else
        grep -iq picasa ~/.xscreensaver
        if [ $? -gt 0 ] ; then
            cat ~/.xscreensaver |
              sed 's/^\programs:.*\\$/\0\
                "Picasa ScreenSaver" showpicasascreensaver \\n\\/g' | \
              sed 's/^\selected:.*$/selected:    0/' > ~/.xscreensaver.picasa
        else
            cat ~/.xscreensaver |
              sed 's/^\selected:.*$/selected:    0/' > ~/.xscreensaver.picasa
        fi

        if [ ! -f ~/.xscreensaver.before.picasa ] ; then
            if [ -f ~/.xscreensaver ] ; then
                mv ~/.xscreensaver ~/.xscreensaver.before.picasa
            fi
        fi

        mv ~/.xscreensaver.picasa ~/.xscreensaver

    fi



    # Then put the picasascr.desktop file into ~/.kde/...
    mkdir -p ~/.kde/share/applnk/System/ScreenSavers/
    if [ -h ~/.kde/share/applnk/System/ScreenSavers/picasascr.desktop ] ; then
        rm -f ~/.kde/share/applnk/System/ScreenSavers/picasascr.desktop 
    fi
    ln -s $PIC_ROOT/desktop/picasascr.desktop ~/.kde/share/applnk/System/ScreenSavers/

    # Then set the default KDE screen saver
    if [ -f ~/.kde/share/config/kdesktoprc ] ; then
        grep -qi "\[ScreenSaver\]" ~/.kde/share/config/kdesktoprc
        if [ $? -eq 0 ] ; then
            append_desktoprc=false
            sed 's/^Saver=.*$/Saver=picasascr.desktop/' <~/.kde/share/config/kdesktoprc >~/.kde/share/config/kdesktoprc.picasa

            if [ ! -f ~/.kde/share/config/kdesktoprc.before.picasa ] ; then
                mv ~/.kde/share/config/kdesktoprc ~/.kde/share/config/kdesktoprc.before.picasa
            fi
            mv ~/.kde/share/config/kdesktoprc.picasa ~/.kde/share/config/kdesktoprc
            
        else
            append_desktoprc=true
        fi
    else
        mkdir -p ~/.kde/share/config 
        append_desktoprc=true
    fi

    if [ x$append_desktoprc = xtrue ] ; then
        echo "[ScreenSaver]" >> ~/.kde/share/config/kdesktoprc
        echo "Saver=picasascr.desktop" >> ~/.kde/share/config/kdesktoprc
    fi

    # Then invoke their screen saver configuration utility
    if [ -n "$EUID" ] ; then
        ps -o cmd -U $EUID | grep -v grep | grep -q xscreensaver
        if [ $? -eq 0 ] ; then
            xscreensaver-demo &
        fi
    fi

    if [ -n "$KDE_FULL_SESSION" -o "$GDMSESSION" = "KDE" ] ; then
        kcmshell screensaver &
    fi

    return 0
}

setpicasascreensaver "$@"
