Sophie

Sophie

distrib > Mandriva > 9.1 > ppc > media > main > by-pkgid > bb23757c2103e08e206a6d1231227700 > files > 13

suspend-scripts-1.4-1mdk.noarch.rpm

#!/bin/bash
# -*- Mode: shell-script -*-
# Copyright (C) 2003 by MandrakeSoft, Chmouel Boudjnah <chmouel@mandrakesoft.com>
# Redistribution of this file is permitted under the terms of the GNU 
# Public License (GPL)
#
# suspend: 10
# resume: 30
#

. /etc/sysconfig/suspend

ARG=$1

if [[ $RESTORE_SOUND != "yes" ]];then
    exit;
fi

SOUND_RUN=/var/run/pm/soundprogs
MODULE_RUN=/var/run/pm/soundmodule
XMMS_PLAYLIST=/var/run/pm/xmms-playlist.m3u
XMMS_CURRENT=/var/run/pm/xmms-current

function suspend {
    rm -rf $SOUND_RUN
    if [ -c /dev/sndstat ]; then
	for n in `cat /dev/sndstat 2>/dev/null|grep Open\ by|awk {'print $3'}|uniq`; do
	    SOUNDPROG=`echo $n|awk -F / {'print $2'}`
	    SOUNDPID=`echo $n|awk -F / {'print $1'}`
	    SOUNDUSER=`ls -ld /proc/$SOUNDPID |awk {'print $3'}`
	    SOUNDDISPLAY=`perl -e 's/\000/\n/g;' -p /proc/$SOUNDPID/environ|grep ^DISPLAY=|cut -d= -f2-`

      	    # Make sure nobody is playing foul tricks on us
	    SOUNDPROG=`echo $SOUNDPROG |sed -e "s/|.*//"`
	    kill $SOUNDPID
	    ps "$SOUNDPID" &>/dev/null && kill -9 $SOUNDPID
	    logger \
"apmscript: Program $SOUNDPROG ($SOUNDPID) of user $SOUNDUSER on display $SOUNDDISPLAY terminated."	    
	done
    fi
    
    for n in `lsof -n|grep /dev|grep ' 14,'|sed -e 's/ \+/|/g'|cut '-d|' -f1-3`; do
	SOUNDPROG=`echo $n |cut '-d|' -f1`
	SOUNDPROG=`echo $SOUNDPROG |sed -e 's/|.*//'`
	SOUNDPID=`echo $n |cut '-d|' -f2`
	SOUNDUSER=`echo $n |cut '-d|' -f3`
	if [[ -f $SOUND_RUN ]] && grep -q "$SOUNDPROG.*$SOUNDUSER.*" $SOUND_RUN;then
	    continue;
	fi
	SOUNDDISPLAY=`perl -e 's/\000/\n/g;' -p /proc/$SOUNDPID/environ|grep ^DISPLAY=|cut -d= -f2-`

	#specific programs here
	if [[ $SOUNDPROG == "xmms" ]];then
	    su --shell="/bin/bash" - "$SOUNDUSER" -c \
		'xmms-shell -e currenttrack|cut -d" " -f3|tr /./ " "' > $XMMS_CURRENT
	    su --shell="/bin/bash" - "$SOUNDUSER" -c \
		"xmms-shell -e \"save /tmp/.tmp.xmms.list;fade left 0 1 50000;xmmsquit\"" > $XMMS_PLAYLIST
	    mv -f /tmp/.tmp.xmms.list $XMMS_PLAYLIST
	else
	    kill $SOUNDPID
	    ps "$SOUNDPID" &>/dev/null && kill -9 $SOUNDPID
	fi
	echo "$SOUNDPROG|$SOUNDPID|$SOUNDUSER|$SOUNDDISPLAY" >>$SOUND_RUN
	logger \
"apmscript: Program $SOUNDPROG ($SOUNDPID) of user $SOUNDUSER on display $SOUNDDISPLAY terminated."

    done
    # We unload the modules later to give the driver some time
    # to spin down.
}

function resume {
    if [[ -f $MODULE_RUN ]];then
	for m in `cat $MODULE_RUN`; do
	    modprobe $m
	done
    fi
    
    if [[ -f $SOUND_RUN ]];then
    # at least we can restart the programs using sound on
    # the right display
	for n in `cat $SOUND_RUN`; do
	    SOUNDPROG=`echo $n|cut '-d|' -f1`
	    SOUNDPID=`echo $n|cut '-d|' -f2`
	    SOUNDUSER=`echo $n|cut '-d|' -f3`
	    SOUNDDISPLAY=`echo $n|cut '-d|' -f4`

	    if [[ $SOUNDPROG == "xmms" ]];then
		CURRENT=$(cat $XMMS_CURRENT)
		su --shell="/bin/bash" - "$SOUNDUSER" -c \
		    "source ~/.bashrc;[ -f ~/.bash_profile ] && source ~/.bash_profile;export DISPLAY=\"$SOUNDDISPLAY\"; xmms $XMMS_PLAYLIST &"
		su --shell="/bin/bash" - "$SOUNDUSER" -c \
		    "source ~/.bashrc;[ -f ~/.bash_profile ] && source ~/.bash_profile;export DISPLAY=\"$SOUNDDISPLAY\"; xmms-shell -e \"jump $CURRENT;play\" >/dev/null 2>/dev/null"
		logger "apmscript: Restarted xmms as user $SOUNDUSER on display $SOUNDDISPLAY position $CURRENT"
	    else
		su --shell="/bin/bash" - "$SOUNDUSER" -c \
		    "source /etc/profile;[ -f ~/.bashrc ] && \
source ~/.bashrc;[ -f ~/.bash_profile ] && \
source ~/.bash_profile;export DISPLAY="$SOUNDDISPLAY";$SOUNDPROG &"
		logger "apmscript: Program $SOUNDPROG started as user $SOUNDUSER on display $SOUNDDISPLAY."
	    fi
	done
	rm -f $SOUND_RUN $XMMS_PLAYLIST $XMMS_CURRENT
    fi
}

case $ARG in
    suspend)
	suspend
	;;
    resume)
	resume
	;;
esac