#!/bin/sh

# Copyright (c) 2018, The ULBSD Project
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice, this
#   list of conditions and the following disclaimer.
#
# * Redistributions in binary form must reproduce the above copyright notice,
#   this list of conditions and the following disclaimer in the documentation
#   and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# $Id: ulbsd-sound 97 2023-08-24 13:05:23Z sv $

DIALOG="cdialog"
sound_file="/usr/local/share/sounds/Oxygen-Sys-Log-In.ogg"

# ---------- Select language ----------

if [ "${LANG}" == "ru_RU.UTF-8" ]
then
    backtitle="Звуковые устройства"
    ok_label=" Протестировать "
    extra_label=" Сохранить "
    cancel_label=" Отмена "
    msg="Вы можете выбрать устройство и воспроизвести тестовый звук, а также сохранить настройки в файле /etc/sysctl.conf."
    default_txt="(по умолчанию)"
    auto_select="Автоматический выбор (требуется перезагрузка)"
else
    backtitle="Sound devices"
    ok_label=" Test "
    extra_label=" Save "
    cancel_label=" Cancel "
    msg="You can select a device and play a test sound, and save the settings in the /etc/sysctl.conf file."
    default_txt="(default)"
    auto_select="Automatic selection (reboot required)"
fi

# ---------- Select sound device ----------

default_pcm="`sysctl -ni hw.snd.default_unit`"
auto_pcm="`sysctl -ni hw.snd.default_auto`"
default_item="pcm${default_pcm}"

break_program="0"
while [ "${break_program}" == "0" ]
do

    pcm_num="0"
    list_pcms=""
    dlg_width="24"
    break_cycle="0"

    while [ "${break_cycle}" == "0" ]
    do
        pcm_info="`sysctl -ni dev.pcm.${pcm_num}.%desc`"
        if [ ! "${pcm_info}" ]
        then
            break_cycle="1"
        else
            cur_pcm="pcm${pcm_num}"
            pcm_play="`sysctl -ni dev.pcm.${pcm_num}.play.vchans`"
            pcm_rec="`sysctl -ni dev.pcm.${pcm_num}.rec.vchans`"
            if [ "${pcm_play}" ] && [ "${pcm_rec}" ]
            then
                pcm_info="${pcm_info} (play/rec)"
            else
                if [ "${pcm_play}" ] && [ ! "${pcm_rec}" ]
                then
                    pcm_info="${pcm_info} (play)"
                else
                    if [ ! "${pcm_play}"] && [ "${pcm_rec}" ]
                    then
                        pcm_info="${pcm_info} (rec)"
                    fi
                fi
            fi
            if [ "${default_pcm}" -eq "${pcm_num}" ]
            then
                pcm_info="${pcm_info} ${default_txt}"
            fi
            cur_length="`echo "${cur_pcm}  ${pcm_info}" | sed -E 's/ /_/g' | awk '{print length($1)}'`"
            if [ "${cur_length}" -gt "${dlg_width}" ]
            then
                dlg_width="${cur_length}"
            fi
            list_pcms="${list_pcms} '${cur_pcm}' '${pcm_info}'"
            pcm_num="`expr -e ${pcm_num} + 1`"
        fi
    done
    
    list_pcms="'auto' '${auto_select}' ${list_pcms}"
    
    dlg_width="`expr -e ${dlg_width} + 8`"
    dlg_height="`expr -e ${pcm_num} + 9`"

    dialog="${DIALOG} --stdout --no-collapse --backtitle '${backtitle}' --ok-label '${ok_label}' --cancel-label '${cancel_label}' --extra-button --extra-label '${extra_label}' --default-item '$default_item' --menu '${msg}' ${dlg_height} ${dlg_width} ${pcm_num} ${list_pcms}"
    input=$(eval $dialog)
    
    case "${?}" in
        3)  
            unit="`echo "$input" | sed -E 's/pcm//g'`"
            sed -i '' -e '/hw.snd.default_unit/Id' /etc/sysctl.conf
            if [ "${unit}" == "auto" ]
            then
                sysctl hw.snd.default_auto=1 >> /dev/null
            else
                sysctl hw.snd.default_unit=${unit} >> /dev/null
                echo "hw.snd.default_unit=${unit}" >> /etc/sysctl.conf
            fi
            break_program="1"
        ;;
        0)
            unit="`echo "$input" | sed -E 's/pcm//g'`"
            if [ "${unit}" == "auto" ]
            then
                sysctl hw.snd.default_auto=1 >> /dev/null
            else
                sysctl hw.snd.default_unit=${unit} >> /dev/null
            fi
            sndfile-play ${sound_file} >> /dev/null & 
            default_item="${input}"
        ;;
        *)        
            sysctl hw.snd.default_unit=${default_pcm} >> /dev/null
            sysctl hw.snd.default_auto=${auto_pcm} >> /dev/null
            break_program="1"
        ;;
    esac
    
done
    
    