#!/bin/sh
#rcrsn51 2017-10-20

export TEXTDOMAIN=sound-card-selector
export OUTPUT_CHARSET=UTF-8

SELECT ()
{
CARD=$(echo $CARDPICK | awk '{print $2}' | tr -d ":")
DEVICE=$(echo $CARDPICK | cut -d "," -f 2 | awk '{print $2}' | tr -d ":")
cat > /tmp/scs.conf << EOF
$(gettext 'Add these lines to the hidden file')  ~/.asoundrc

defaults.pcm.card $CARD
defaults.pcm.device $DEVICE
defaults.ctl.card $CARD
EOF
Xdialog --no-cancel --title "Sound Card Selector" --textbox /tmp/scs.conf 15 60
}
export -f SELECT

BOOTORDER ()
{
cat > /tmp/scs.boot << EOF
$(gettext 'To prevent your sound cards from changing order at bootup,')
$(gettext 'create the file /etc/modprobe.d/snd_cards_priorities.conf.')
$(gettext 'List the kernel modules in the desired order. For example:')

EOF
CARDS=$(cat /proc/asound/modules | awk '{print $2}'| tr "\n" ",")	#then remove trailing comma
echo options snd slots=${CARDS%,} >> /tmp/scs.boot
echo >> /tmp/scs.boot
echo $(gettext 'Click Edit and paste in this line of code.') >> /tmp/scs.boot
Xdialog --no-cancel --title "Sound Card Selector" --textbox /tmp/scs.boot 16 80
}
export -f BOOTORDER

HELP2 ()
{
Xdialog --left --wrap --title " " --msgbox "$(gettext '1. Run: lspci -nnk\n2. You should see two Audio devices. They are probably Card 0 and Card 1.\n3. Note the [vendor:product] ID codes, like [1002:4383] and [10de:0e0f].\n4. Edit the file /etc/modprobe.d/snd_cards_priorites.conf. Or use the Make button.\n\ne.g. options snd_hda_intel vid=1002,10de pid=4383,0e0f index=1,0\n\n5. The first argument is the kernel module.\n6. The "vid" argument lists the two vendor IDs, and "pid" lists the two product IDs.\n7. The "index" argument sets the desired order.\n8. Reboot.')" 0 0
}
export -f HELP2

BOOTORDER2 ()
{
echo $(gettext 'Click Edit and paste in this line of code.') > /tmp/scs.boot
echo >> /tmp/scs.boot
IDS=$(cat /proc/bus/pci/devices | grep snd_hda_intel | awk '{print $2}')
echo options snd_hda_intel vid=${IDS:0:4},${IDS:9:4} pid=${IDS:4:4},${IDS:13:4} index=0,1 >> /tmp/scs.boot
Xdialog --no-cancel --title "Sound Card Selector" --textbox /tmp/scs.boot 10 80
}
export -f BOOTORDER2

QUICKSET ()
{
[ ! -f $HOME/.asoundrc.reset ] && mv $HOME/.asoundrc $HOME/.asoundrc.reset
cat > $HOME/.asoundrc << EOF
defaults.pcm.card $1
defaults.pcm.device $2
defaults.ctl.card $3
EOF
$TEXTEDITOR $HOME/.asoundrc &
}
export -f QUICKSET

###############

mkdir -p /etc/pwf #just in case
APPFILE=/etc/pwf/appfile
if [ ! -f $APPFILE ]; then
	echo "export TEXTEDITOR=leafpad" > $APPFILE
	echo "export FILEMANAGER=pcmanfm" >> $APPFILE
	echo "export WEBBROWSER=/usr/lib/firefox/firefox" >> $APPFILE
	echo "export IMAGEVIEWER=viewnior" >> $APPFILE
	Xdialog --title " " --msgbox "$(gettext 'Open the file /etc/pwf/appfile.\nSet your helper applications.')" 0 0
	exit
fi
. $APPFILE

aplay -l | grep "^card" > /tmp/scs.card
CARDLIST=""
while read L; do
CARDLIST=$CARDLIST"<item>$L</item>"
done </tmp/scs.card

cat /proc/asound/modules > /tmp/scs.mod
MODLIST=""
while read L; do
	LL=$(echo -n $L | awk '{print "card  " $1 ":  " $2}')
	MODLIST=$MODLIST"<item>$LL</item>"
done </tmp/scs.mod

export DIALOG="
<window title=\"$(gettext 'Sound Card Selector')\">
<vbox>
	<frame $(gettext 'Select a default sound card and device')>
		<combobox width-request=\"650\" >
		 	$CARDLIST
			<variable>CARDPICK</variable>
		</combobox>
	<hbox>
			<button>
				<label>$(gettext 'Select')</label>
				<action>SELECT &</action>
			</button>
			<button>
				<label>$(gettext 'Edit')</label>
				<action>$TEXTEDITOR ~/.asoundrc &</action>
			</button>
		</hbox>
	</frame>
	<frame Quick select (expert)>
		<hbox>
			<button>
				<label>0-0-0</label>
				<action>QUICKSET 0 0 0</action>
			</button>
			<button>
				<label>0-3-0</label>
				<action>QUICKSET 0 3 0</action>
			</button>
			<button>
				<label>1-0-1</label>
				<action>QUICKSET 1 0 1</action>
			</button>
			<button>
				<label>$(gettext 'Reset')</label>
				<action>cp $HOME/.asoundrc.reset $HOME/.asoundrc</action>
				<action>$TEXTEDITOR ~/.asoundrc &</action>
			</button>
		</hbox>
	</frame>
	<frame $(gettext 'Kernel modules used by your sound cards')>
		<hbox>
		<combobox width-request=\"250\">
		 	$MODLIST
		</combobox>
			<button>
				<label>$(gettext 'Help')</label>
				<action>BOOTORDER &</action>
			</button>
			<button>
				<label>$(gettext 'Edit')</label>
				<action>$TEXTEDITOR /etc/modprobe.d/snd_cards_priorities.conf &</action>
			</button>
		</hbox>
	</frame>
	<frame $(gettext 'If your sound cards use the same kernel module')>
		<hbox>
			<button>
				<label>$(gettext 'Help')</label>
				<action>HELP2 &</action>
			</button>
			<button>
				<label>$(gettext 'Make')</label>
				<action>BOOTORDER2 &</action>
			</button>
			<button>
				<label>$(gettext 'Edit')</label>
				<action>$TEXTEDITOR /etc/modprobe.d/snd_cards_priorities.conf &</action>
			</button>
		</hbox>
	</frame>
	<hbox>
		 <button><label>$(gettext 'Quit')</label></button>
	</hbox>
</vbox>
</window>"

gtkdialog3 -c -p DIALOG
