#! /bin/sh
# aumix(-gtk)    Save/restore mixer settings on bootup
# By Eduard Bloch <blade@debian.org>
# Supports multiple devices with probing and devfs.
# Thanks to Leon Breedt <ljb@debian.org> for the original version.

NAME=aumix
CONF=/etc/aumixrc
AUMIX=/usr/bin/aumix
KEYFILE=/etc/default/aumix.stop

[ -x $AUMIX -a -f /usr/lib/menu/aumix ] || exit 0

if [ -f $KEYFILE ] ; then
   echo "Key file $KEYFILE detected, won't execute aumix."
   exit 0
fi

export PATH=/usr/sbin:$PATH
export I=""

if [ -x /etc/init.d/alsa ]; then
    if alsactl >/dev/null 2>&1; then
        echo "ALSA detected, so aumix will not touch the mixer settings."
        exit 0
    fi
    echo "ALSA detected, but alsactl not usable, carrying on with aumix."
fi

DEVFS=`grep " devfs " /proc/mounts | cut -f2 -d" "`

# if we have devfs mounted and there are mixer devices, use it rather then
# try-and-error methods
if test -n "$DEVFS" && ls $DEVFS/sound/mixer* >/dev/null 2>&1 ; then
   MIXERS=`cd $DEVFS/sound; ls mixer* 2>/dev/null`
   mute ()
   {
      echo "muting."
      for x in $MIXERS; do
         $AUMIX -d $DEVFS/sound/$x -v 0
      done
   }
   load ()
   {
      echo -n "Restoring mixer settings: "
      for x in $MIXERS; do
         $AUMIX -d $DEVFS/sound/$x -f $CONF${x#mixer} -L >/dev/null
         echo -n "$x, ";
      done
      echo "done."
   }
   store ()
   {
      echo -n "Saving mixer settings: "
      for x in $MIXERS; do
         $AUMIX -d $DEVFS/sound/$x -f $CONF${x#mixer} -S
         echo -n "$x, ";
      done
   }
else
   # no devfs, probing
   store ()
   {
      I=""
      echo -n "Saving mixer settings: "
      while $AUMIX -d /dev/mixer$I -f $CONF$I -S 2>/dev/null >/dev/null; do
         echo -n "mixer$I, "
         test "x$I" = "x" && I=0
         I=`expr $I + 1`
      done
      if [ "$I" = "" ]; then
         echo "failed."
         return 1;
      fi
   }

   load ()
   {
      I=""
      echo -n "Restoring mixer settings: "
      while [ -f $CONF$I ] && $AUMIX -d /dev/mixer$I -f $CONF$I -L 2>/dev/null >/dev/null; do
         echo -n "mixer$I, "
         test "x$I" = "x" && I=0
         I=`expr $I + 1`
      done
      if [ "$I" = "" ]; then
         echo "failed."
         return 1;
      else
         echo "done."
      fi
   }

   mute ()
   {
      I=""
      echo "muting."
      while $AUMIX -d /dev/mixer$I -v 0 2>/dev/null >/dev/null; do
         test "x$I" = "x" && I=0
         I=`expr $I + 1`
      done
   }
fi

case "$1" in
    start|restart|reload|force-reload)
        if [ -f $CONF ]; then
            rm -f /var/lock/aumix/saved
            load
        fi
        ;;
    stop)
        if [ -f /var/lock/aumix/saved ] ; then
          echo "Settings already stored and devices muted, not saving again..."
        else
           if store; then
              touch /var/lock/aumix/saved
              mute
           fi
        fi
        ;;
    save)
        if [ -f /var/lock/aumix/saved ] ; then
          echo "Settings already stored and devices muted, not saving again..."
        else
           store && echo done.
        fi
        ;;
    *)
        echo "Usage: /etc/init.d/$NAME {start|stop|save}" 
        exit 1
        ;;
esac

exit 0
