#!/bin/bash -e
# stop/continue processes using sound devices.
#  Copyright (c) 2000 Masato Taruishi <taru@debian.org>
#
#  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 2 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, write to the Free Software
#  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

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

# Includes common functions used by alsa-bas scripts.
if [ ! -f /usr/share/alsa-base/snd-dev-utils ]; then
  exit 0
fi
. /usr/share/alsa-base/snd-dev-utils

get_sound_devs
if [ "$1" = suspend ]; then
      get_sound_devs_proc
      disable_esd
      if [ "$force_stop_modules_before_suspend" = true ]; then
	alsactl store || true
	sleep 1
        disable_sound_devs
        ALSA_KEEP_DEV_PERMS="keep" /etc/init.d/alsa force-stop | logger || true
      else
        if ls /proc | grep asound > /dev/null; then
          disable_sound_devs
          stop_procs_using_sound_devs
        fi
      fi
      sleep 1
elif [ "$1" = resume ]; then
      if [ "$force_stop_modules_before_suspend" = true ]; then
        enable_sound_devs
        /etc/init.d/alsa start | logger || true
        sleep 1
        alsactl restore || true
      else
        if ls /proc | grep asound > /dev/null; then
          cont_procs_using_sound_devs
          enable_sound_devs
        fi
      fi
      enable_esd
fi
