#!/bin/sh

# stop/continue processes using sound devices.

set -e

if [ -e /etc/default/alsa ]; then
 . /etc/default/alsa
fi

if [ "$1" = suspend ]; then
      if [ "$force_stop_modules_before_suspend" = "forcibly-unload-driver" ]; then
	alsactl store || true
	sleep 1
        /etc/init.d/alsa force-stop || true
      elif [ "$force_stop_modules_before_suspend" = "stop-procs" ]; then
	if [ -d /proc/asound ]; then
	  kill -STOP $(fuser $(ls -l /dev | tr -s ' ' | grep '14, ' | cut -d\  -f10 | sed -e 's#^#/dev/#' | tr '\n' ' ')) | cut -d: -f2 | tr '\n' ' '
        fi
      fi
      sleep 1
elif [ "$1" = resume ]; then
      if [ "$force_stop_modules_before_suspend" = "forcibly-unload-driver" ]; then
        /etc/init.d/alsa start || true
        sleep 1
        alsactl restore || true
      elif [ "$force_stop_modules_before_suspend" = "stop-procs" ]; then
	if [ -d /proc/asound ]; then
	    kill -CONT $(fuser $(ls -l /dev | tr -s ' ' | grep '14, ' | cut -d\  -f10 | sed -e 's#^#/dev/#' | tr '\n' ' ')) | cut -d: -f2 | tr '\n' ' '
        fi
      fi
fi
