#!/bin/bash

## With this script you can change the ~/.asoundrc. With this, you can change between idjcvoip, jackplug and pulseplug (and...?).
## You can use this script since Debian squeeze and since Ubuntu karmic.
## If you can, please make this script better and complete it and publish it again!


## copyright (C) 2010 Til Til
##
## 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., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  
## USA

######dependencies:
####obligatory:
##bash
##coreutils
##file
##findutils
##zenity

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

version="0.1.0"
icon="/usr/share/pixmaps/asoundconfsetter.xpm"
title="asoundconfsetter "$version""
user=$(id -n -u)
path_user=~

###### Default = English #####
back="setting of beginning"
change_default="change default"
close="Close"
finished="The $path_user/.asoundrc was changed to:"
info_asoundconfsetter="With this script you can change the $path_user/.asoundrc. With this, you can change between idjcvoip, jackplug, pulseplug and default."
not_changed="$path_user/.asoundrc was not changed"
no_module_pcm_jack="Jackplug and idjcvoip doesn't works. You have to install 'libasound_module_pcm_jack.so' which is part of the package 'libasound2-plugins' (since version 1.0.22) or, for architecture amd64, of 'lib32asound2-plugins' or 'ia32-libs'. Nevertheless, do you want to continue?"
no_asoundrc="NO .asoundrc"
not_zenity="For using $title, you have to install \"zenity\"."
select_ation="Please select the setting of your $path_user/.asoundrc!"
select_default="Please select the default-setting of your $path_user/.asoundrc!"

case $LANG in
######## German ########
  de* )
  back="Einstellung vom Anfang"
  change_default="Voreinstellung ändern"
  close="Schließen"
  finished="Die $path_user/.asoundrc wurde geändert nach:"
  info_asoundconfsetter="Mit diesem Skript kannst Du die $path_user/.asoundrc ändern. Mit diesem kannst Du zwischen idjcvoip, jackplug, pulseplug und default wechseln."
  not_changed="$path_user/.asoundrc wurde nicht geändert."
  no_module_pcm_jack="Jackplug und idjcvoip funktionieren nicht. Du musst 'libasound_module_pcm_jack.so' installieren, was Teil des Pakets 'libasound2-plugins' (seit Version 1.0.22) ist oder, für die Archiketur amd64, diese: 'lib32asound2-plugins' oder 'ia32-libs'. Möchtest Du trotzdem fortfahren?"
  no_asoundrc="KEINE .asoundrc"
  not_zenity="Um $title zu benutzen, musst Du \"zenity\" installieren.."
  select_ation="Bitte wähle die Einstellung Deiner $path_user/.asoundrc!"
  select_default="Bitte wähle die Voreinstellung Deiner $path_user/.asoundrc!"
esac

##zenity

if ! test -e /usr/bin/zenity
then
  if ! test -e /usr/local/bin/zenity
  then
    echo "$not_zenity $info_asoundconfsetter"
    exit
  fi
fi


function backup(){
if test -e ~/.asoundrc
then
  if ! test -e ~/.asoundrc~old
  then
    mv -f ~/.asoundrc ~/.asoundrc~old
  fi
else
  if ! test -e ~/.asoundrc~old
  then
    echo -e "no $path_user/.asoundrc" | xargs -0 > ~/.asoundrc~no
  fi
fi
}


function write_OSS(){
echo -e "##OSS\n\npcm.dsp0 {\n    type plug\n    slave-pcm \"duplex\"\n}\n\nctl.dsp0 {\n    type plug\n    slave.pcm \"snd_card\"\n}\n\nctl.mixer0 {\n    type plug\n    slave.pcm \"snd_card\"\n}" | xargs -0 > ~/.asoundrc
}


function write_rd(){
echo -e "##rd\n\npcm.rd0 {\n  type hw\n  card 0\n}\nctl.rd0 {\n  type hw\n  card 0\n}" | xargs -0 > ~/.asoundrc
}


function write_pulseplug(){
echo -e "#pulseplug\n\npcm.pulse {\n    type pulse\n}\nctl.pulse {\n    type pulse\n}\npcm.!default {\n    type pulse\n}\nctl.!default {\n    type pulse\n}" | xargs -0 > ~/.asoundrc
}


function write_jackplug(){
if ! test -e /usr/lib/alsa-lib/libasound_module_pcm_jack.so
then
  if ! test -e /usr/lib32/alsa-lib/libasound_module_pcm_jack.so
  then
    if ! zenity --question --title="$title" --text="$no_module_pcm_jack" --window-icon=$icon
    then
      zenity --info --title="asoundconfsetter" --text="$not_changed" --window-icon=$icon --timeout 3
      exit
    fi
  fi
fi
echo -e "## Jackplug\n\npcm.!default {\n    type plug\n    slave { pcm \"jack\" }\n}\n\npcm.jack {\n    type jack\n    playback_ports {\n        0 alsa_pcm:playback_1\n        1 alsa_pcm:playback_2\n    }\n    capture_ports {\n        0 alsa_pcm:capture_1\n        1 alsa_pcm:capture_2\n    }\n}\n\nctl.mixer0 {\n    type hw\n}" | xargs -0 > ~/.asoundrc
}


function write_idjcvoip(){
if ! test -e /usr/lib/alsa-lib/libasound_module_pcm_jack.so
then
  if ! test -e /usr/lib32/alsa-lib/libasound_module_pcm_jack.so
  then
    if ! zenity --question --title="$title" --text="$no_module_pcm_jack" --window-icon=$icon
    then
      zenity --info --title="asoundconfsetter" --text="$not_changed" --window-icon=$icon --timeout 3
      exit
    fi
  fi
fi
echo -e "pcm.idjcvoip {\n   type plug\n   slave { pcm \"idjcjack\" }\n}\n\npcm.idjcjack {\n   type jack\n   playback_ports {\n      0 idjc-mx:voip_recv_lt\n      1 idjc-mx:voip_recv_rt\n   }\n   capture_ports {\n      0 idjc-mx:voip_send_lt\n      1 idjc-mx:voip_send_rt\n   }\n}" | xargs -0 > ~/.asoundrc
}


if [ "$action" = "" ]
then
  action=$(zenity --list --height=350 --column="$path_user/.asoundrc" OSS rd pulseplug jackplug idjcvoip "$no_asoundrc" "[[[$back]]]" "[[[$change_default]]]" "[[[x $close]]]" --title="$title" --text="$select_ation" --window-icon=$icon);echo "$action"
fi


if [ "$action" = "rd" ]
then
  backup
  write_rd
  restart alsa-mixer-save
fi

if [ "$action" = "OSS" ]
then
  backup
  write_OSS
  restart alsa-mixer-save
fi

if [ "$action" = "pulseplug" ]
then
  backup
  write_pulseplug
  restart alsa-mixer-save
fi

if [ "$action" = "jackplug" ]
then
  backup
  write_jackplug
  restart alsa-mixer-save
fi

if [ "$action" = "idjcvoip" ]
then
  backup
  write_idjcvoip
  restart alsa-mixer-save
fi

if [ "$action" = "$no_asoundrc" ]
then
  backup
  rm -f "~/.asoundrc"
  restart alsa-mixer-save
fi

if [ "$action" = "[[[$change_default]]]" ]
then
  default=$(zenity --list --height=300 --column="default $path_user/.asoundrc" OSS rd pulseplug jackplug idjcvoip "$no_asoundrc" --title="$title" --text="$select_default" --window-icon=$icon);echo "$default"
  if [ "$default" = "OSS" ]
  then
    write_OSS
  fi
  if [ "$default" = "pulseplug" ]
  then
    write_pulseplug
  fi
  if [ "$default" = "jackplug" ]
  then
    write_jackplug
  fi
  if [ "$default" = "idjcvoip" ]
  then
    write_idjcvoip
  fi
  if [ "$default" = "$no_asoundrc" ]
  then
    rm -f ~/.asoundrc
  fi
  if [ "$default" != "" ]
  then
    rm -f ~/.asoundrc~old
    rm -f ~/.asoundrc~no
    restart alsa-mixer-save
    action="$default, set as default"
  fi
fi

if [ "$action" = "[[[$back]]]" ]
then
  if test -e ~/.asoundrc~old
  then
    mv -f ~/.asoundrc~old ~/.asoundrc
  else
    if test -e ~/.asoundrc~no
    then
      rm -f ~/.asoundrc
      rm -f ~/.asoundrc~no
    fi
  fi
  restart alsa-mixer-save
fi

if [ "$action" = "[[[x $close]]]" ] || [ "$action" = "" ]
then
  exit
fi

zenity --info --title="asoundconfsetter" --text="$finished $action" --window-icon=$icon --timeout 3
action=""
asoundconfsetter.sh
exit
