#!/bin/bash
#
# 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 .
#
#
# A pm-utils evolution of the gaim script from hibernate-script package made by Bernard Blackham
# Copyright (C) 2009 Laurento Frittella
#
#
# This script set your pidgin(s) offline on system suspend/hibernate
# and restore your previous status and status message on resume/thaw
#
# INSTALLATION: simply copy the script in the "/etc/pm/sleep.d" directory and make it executable
# (accordingly with the pm-utils man pages you should use a prefix between 00 - 49
# for the filename. Example: "/etc/pm/sleep.d/10pidgin")
#
# Version: 20090219
purple_remote=$(command -v purple-remote)
if [ -z "$purple_remote" ] || [ ! -x "$purple_remote" ]; then
echo "purple-remote command not found."
return 1
fi
get_env_var_of_process()
{
local pid="$1" envvar="$2"
tr '\0' '\n' /dev/null
for pid in $(pidof pidgin); do
user=$(get_env_var_of_process $pid USER)
dbus_session_bus_address=$(get_env_var_of_process $pid DBUS_SESSION_BUS_ADDRESS)
echo "user=$user" >> /tmp/pm-utils_pidgin.${pid}
echo "dbus_session_bus_address=$dbus_session_bus_address" >> /tmp/pm-utils_pidgin.${pid}
status=$(DBUS_SESSION_BUS_ADDRESS="$dbus_session_bus_address" su "$user" -c "purple-remote getstatus")
status_msg=$(DBUS_SESSION_BUS_ADDRESS="$dbus_session_bus_address" su "$user" -c "purple-remote getstatusmessage")
echo "purple_status=$status" >> /tmp/pm-utils_pidgin.${pid}
echo "purple_status_msg=$status_msg" >> /tmp/pm-utils_pidgin.${pid}
DBUS_SESSION_BUS_ADDRESS="$dbus_session_bus_address" su "$user" -c "purple-remote setstatus?status=offline"
done
;;
thaw|resume)
for to_resume in $(ls /tmp/pm-utils_pidgin*); do
source ${to_resume}
DBUS_SESSION_BUS_ADDRESS="$dbus_session_bus_address" su "$user" -c "purple-remote setstatus?status=${purple_status}&message=\"${purple_status_msg}\""
done
# Cleanup
rm -f /tmp/pm-utils_pidgin* &> /dev/null
;;
esac