# -*- sh -*-
# vim:ft=sh:ts=8:sw=4:noet

# $Id: gaim 338 2007-06-10 16:19:56Z mendel $

gaim_remote=$(command -v gaim-remote)

AddConfigHandler GaimOptions
AddConfigHelp "LogoutGaim <boolean>" "Changes all locally running Gaim's status to offline before suspending, and (optionally) change it back to the original status after resuming."
AddConfigHelp "GaimRestoreStatus <boolean>" "Changes back Gaim's status to the original status after resuming."
AddConfigHelp "GaimLogoutMessage <string>" "Status message to set when logging out Gaim."
AddConfigHelp "GaimLoginMessage <string>" "Status message to set when logging in Gaim."

# Gets the value of the environment variable with the given name of the process with the given PID.
get_env_var_of_process()
{
    local pid="$1" envvar="$2"

    tr '\0' '\n' </proc/$pid/environ | sed -ne 's/^'"$envvar"'=\(.*\)$/\1/p'
}

LogoutGaim()
{
    [ x"$LOGOUT_GAIM" != "x1" ] && return 0

    if [ -z "$gaim_remote" ] || [ ! -x "$gaim_remote" ]; then
	vecho 0 'Cannot log out Gaim: `gaim-remote` not found.'
	return 0
    fi

    local pid i=0
    for pid in `pidof gaim`; do
	local user dbus_session_bus_address gaim_status saved_status_id status_type gaim_remote_cmd

	user=$(get_env_var_of_process $pid USER)
	dbus_session_bus_address=$(get_env_var_of_process $pid DBUS_SESSION_BUS_ADDRESS)

	vecho 2 "Saving status of $user's Gaim using D-Bus session bus address $dbus_session_bus_address"
	# gaim-remote (as of 2.0.0beta5) does not support the 'getstatus' command, so we do it manually
	saved_status_id=$(DBUS_SESSION_BUS_ADDRESS="$dbus_session_bus_address" su "$user" -c "$gaim_remote 'GaimSavedstatusGetCurrent'")
	status_type=$(DBUS_SESSION_BUS_ADDRESS="$dbus_session_bus_address" su "$user" -c "$gaim_remote 'GaimSavedstatusGetType?saved_status=$saved_status_id'")
	gaim_status=$(DBUS_SESSION_BUS_ADDRESS="$dbus_session_bus_address" su "$user" -c "$gaim_remote 'GaimPrimitiveGetIdFromType?type=$status_type'")

	# using this eval-crap to be POSIX-compliant (arrays are nonstandard)
	eval "GAIM_LOGGED_OUT_SESSIONS_USER_$i='$user'"
	eval "GAIM_LOGGED_OUT_SESSIONS_DBUS_$i='$dbus_session_bus_address'"
	eval "GAIM_LOGGED_OUT_SESSIONS_STATUS_$i='$gaim_status'"
	i=`expr $i + 1`

	gaim_remote_cmd="setstatus?status=offline"
	if [ -n "$GAIM_LOGOUT_MESSAGE" ]; then
		gaim_remote_cmd="$gaim_remote_cmd&message=$GAIM_LOGOUT_MESSAGE"
	fi

	vecho 2 "Logging out $user's Gaim using D-Bus session bus address $dbus_session_bus_address"
	DBUS_SESSION_BUS_ADDRESS="$dbus_session_bus_address" su "$user" -c "$gaim_remote '$gaim_remote_cmd'"
    done

    return 0
}

LoginGaim()
{
    [ x"$LOGOUT_GAIM" != "x1" ] && return 0

    [ x"$GAIM_RESTORE_STATUS" != "x1" ] && return 0

    if [ -z "$gaim_remote" ] || [ ! -x "$gaim_remote" ]; then
	vecho 0 'Cannot log on Gaim: `gaim-remote` not found.'
	return 0
    fi

    local i=0
    while :; do
	local user dbus_session_bus_address gaim_status gaim_remote_cmd

	user=`eval "echo \\\$GAIM_LOGGED_OUT_SESSIONS_USER_$i"`
	dbus_session_bus_address=`eval "echo \\\$GAIM_LOGGED_OUT_SESSIONS_DBUS_$i"`
	gaim_status=`eval "echo \\\$GAIM_LOGGED_OUT_SESSIONS_STATUS_$i"`
	i=`expr $i + 1`

	[ -z "$user" ] && break

	gaim_remote_cmd="setstatus?status=$gaim_status"
	if [ -n "$GAIM_LOGIN_MESSAGE" ]; then
		gaim_remote_cmd="$gaim_remote_cmd&message=$GAIM_LOGIN_MESSAGE"
	fi

	vecho 2 "Logging back (to status $gaim_status) $user's Gaim using D-Bus session bus address $dbus_session_bus_address"
	DBUS_SESSION_BUS_ADDRESS="$dbus_session_bus_address" su "$user" -c "$gaim_remote '$gaim_remote_cmd'"
    done

    return 0
}

GaimOptions()
{
    case "$1" in
	logoutgaim)
	    if BoolIsOn "$1" "$2"; then
		LOGOUT_GAIM=1
		if [ -z "$GAIMLOGOUT_HOOKED" ]; then
		    AddSuspendHook 19 LogoutGaim
		    AddResumeHook 19 LoginGaim
		    GAIMLOGOUT_HOOKED=1
		fi
	    else
		LOGOUT_GAIM=0
	    fi
	    ;;
	gaimrestorestatus)
	    if BoolIsOn "$1" "$2"; then
		GAIM_RESTORE_STATUS=1
	    else
		GAIM_RESTORE_STATUS=0
	    fi
	    ;;
	gaimlogoutmessage)
	    GAIM_LOGOUT_MESSAGE="$2"
	    ;;
	gaimloginmessage)
	    GAIM_LOGIN_MESSAGE="$2"
	    ;;
	*)
	    return 1
	    ;;
    esac
}
