#!/bin/bash

## 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

mkdir -p ~/.config/autostart
echo -e "[Desktop Entry]\nType=Application\nName=Start JACK Control\nExec=qjackctl-start.sh" | xargs -0 > ~/.config/autostart/qjackctl-start.desktop

mkdir -p ~/.kde/Autostart
cp ~/.config/autostart/qjackctl-start.desktop ~/.kde/Autostart/qjackctl-start.desktop

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

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
    echo "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'."
    sleep 3
    exit
  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

restart alsa-mixer-save

qjackctl

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

echo "The /.asoundrc was changed."
rm -f ~/.config/autostart/qjackctl-start.desktop
rm -f ~/.kde/Autostart/qjackctl-start.desktop

exit
