#! /bin/sh -
#
# Script to start the Advanced Linux Sound Architecture (ALSA)
#
# Copyright (c) 2017, 2019 Matias Fonzo, <selk@dragora.org>.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Sanity checks
if ! type alsactl > /dev/null 2>&1
then
    echo "${0}: Error: alsactl(1) is not available." 1>&2
    exit 127;
fi
if ! type amixer > /dev/null 2>&1
then
    echo "${0}: Error: amixer(1) is not available." 1>&2
    exit 127;
fi

if test -d /proc/asound
then
    if test -e /var/lib/alsa/asound.state
    then
        echo "Restoring mixer from /var/lib/alsa/asound.state ..."
        alsactl restore
    else
        echo "Setting local values for the ALSA mixer ..."
        amixer set Master 90% unmute       > /dev/null 2>&1
        amixer set Front 80% unmute        > /dev/null 2>&1
        amixer set PCM 80% unmute          > /dev/null 2>&1
        amixer set Side 80% unmute         > /dev/null 2>&1
        amixer set CD 90% unmute           > /dev/null 2>&1
        amixer set Headphone unmute        > /dev/null 2>&1
        amixer set Mic 90% unmute          > /dev/null 2>&1
        amixer set 'Front Mic' 90% unmute  > /dev/null 2>&1
        alsactl store
    fi
fi

