#!/bin/bash

INSMOD="/sbin/insmod"
MODDIR="../modules"
MAP="../snd.map"
OLDMAP="../snd.map.old"
CARDS_LIMIT=2

function banner() {
   echo > /dev/null
#  echo "Inserting $1.."
#  sleep 1
}

function isapnp() {
  if [ -r $MODDIR/isapnp.o ]; then
    banner "isapnp"
    if ! $INSMOD -m $MODDIR/isapnp.o >> $MAP; then exit 1; fi
  fi
}

function basic() {
  banner "snd"
  if ! $INSMOD -m $MODDIR/snd.o snd_cards_limit=$CARDS_LIMIT >> $MAP; then exit 1; fi
  banner "snd-mixer"
  if ! $INSMOD -m $MODDIR/snd-mixer.o >> $MAP; then exit 1; fi
  if [ -r $MODDIR/snd-mixer-oss.o ]; then
    banner "snd-mixer-oss"
    if ! $INSMOD -m $MODDIR/snd-mixer-oss.o >> $MAP; then exit 1; fi  
  fi
  banner "snd-timer"
  if ! $INSMOD -m $MODDIR/snd-timer.o >> $MAP; then exit 1; fi
  banner "snd-pcm"
  if ! $INSMOD -m $MODDIR/snd-pcm.o >> $MAP; then exit 1; fi
  banner "snd-pcm1"
  if ! $INSMOD -m $MODDIR/snd-pcm1.o >> $MAP; then exit 1; fi
  if [ -r $MODDIR/snd-pcm1-oss.o ]; then
    banner "snd-pcm1-oss"
    if ! $INSMOD -m $MODDIR/snd-pcm1-oss.o >> $MAP; then exit 1; fi  
  fi
  if [ -r $MODDIR/snd-midi.o ]; then
    banner "snd-midi"
    if ! $INSMOD -m $MODDIR/snd-midi.o >> $MAP; then exit 1; fi
  fi
  if [ -r $MODDIR/snd-hwdep.o ]; then
    banner "snd-hwdep"
    if ! $INSMOD -m $MODDIR/snd-hwdep.o >> $MAP; then exit 1; fi
  fi
  if [ -r $MODDIR/snd-seq.o ]; then
    if [ -r $MODDIR/snd-seq-device.o ]; then
      banner "snd-seq-device"
      if ! $INSMOD -m $MODDIR/snd-seq-device.o >> $MAP; then exit 1; fi
    fi
    banner "snd-seq"
    if ! $INSMOD -m $MODDIR/snd-seq.o snd_seq_default_timer=256 >> $MAP; then exit 1; fi
    if [ -r $MODDIR/snd-seq-midi.o ]; then
      banner "snd-seq-midi"
      if ! $INSMOD -m $MODDIR/snd-seq-midi.o >> $MAP; then exit 1; fi
    fi
    if [ -r $MODDIR/snd-seq-midi-emul.o ]; then
      banner "snd-seq-midi-emul"
      if ! $INSMOD -m $MODDIR/snd-seq-midi-emul.o >> $MAP; then exit 1; fi
    fi
    if [ -r $MODDIR/snd-seq-instr.o ]; then
      banner "snd-seq-instr"
      if ! $INSMOD -m $MODDIR/snd-seq-instr.o >> $MAP; then exit 1; fi
    fi
    if [ -r $MODDIR/snd-ainstr-simple.o ]; then
      banner "snd-ainstr-simple"
      if ! $INSMOD -m $MODDIR/snd-ainstr-simple.o >> $MAP; then exit 1; fi
    fi
    if [ -r $MODDIR/snd-ainstr-gf1.o ]; then
      banner "snd-ainstr-gf1"
      if ! $INSMOD -m $MODDIR/snd-ainstr-gf1.o >> $MAP; then exit 1; fi
    fi
    if [ -r $MODDIR/snd-ainstr-iw.o ]; then
      banner "snd-ainstr-iw"
      if ! $INSMOD -m $MODDIR/snd-ainstr-iw.o >> $MAP; then exit 1; fi
    fi
    if [ -r $MODDIR/snd-seq-oss.o ]; then
      banner "snd-seq-oss"
      if ! $INSMOD -m $MODDIR/snd-seq-oss.o >> $MAP; then exit 1; fi
    fi
  fi
}

function insert() {
  if ! $INSMOD -m $MODDIR/$@ >> $MAP; then exit 1; fi
}

function restore() {
  if [ -r /etc/asound/$1 ]; then
    /usr/sbin/alsactl -f /etc/asound/$1 restore
  fi
}

if [ -z "$1" ]; then
  echo "Specify soundcard ID..."
  exit
fi

mv -f $MAP $OLDMAP

./remove

case "$1" in
  interwave-stb|stb)
    basic; isapnp
    insert snd-cs4231.o
    insert snd-gus.o
    insert snd-synth-gus.o
    insert snd-i2c.o
    insert snd-tea6330t.o
    insert snd-card-interwave-stb.o snd_port=0x240 snd_irq=7 snd_dma1=5 snd_dma2=6 snd_midi=1 snd_pcm_voices=8
    restore interwave-stb.conf
    ;;
  interwave|iw)
    basic; isapnp
    insert snd-cs4231.o
    insert snd-gus.o
    insert snd-synth-gus.o
    insert snd-card-interwave.o snd_dma1=5 snd_dma2=6 snd_midi=1
    restore interwave.conf
    ;;
  es1688)
    basic
    insert snd-es1688.o
    insert snd-mpu401-uart.o 
    insert snd-opl3.o
    insert snd-card-es1688.o snd_mpu_port=0x330
    restore es1688.conf
    ;;
  es18xx)
    basic; isapnp
    insert snd-es18xx.o
    insert snd-mpu401-uart.o 
    insert snd-opl3.o
    insert snd-card-es18xx.o
    restore es18xx.conf
    ;;
  sb16)
    basic; isapnp
    insert snd-sb16-dsp.o
    insert snd-sb16-csp.o
    insert snd-mpu401-uart.o 
    insert snd-opl3.o
    insert snd-card-sb16.o snd_irq=7 snd_dma8=1 snd_dma16=5
    restore sb16.conf
    ;;
  sb)
    basic; isapnp
    insert snd-sb8-dsp.o
    insert snd-opl3.o
    insert snd-card-sb8.o snd_port=0x220 snd_irq=5 snd_dma8=3
    restore sb.conf
    ;;
  sbawe)
    basic; isapnp
    insert snd-sb16-dsp.o
    insert snd-sb16-csp.o
    insert snd-mpu401-uart.o 
    insert snd-opl3.o
    if [ -r $MODDIR/snd-emu8000.o ]; then
      insert snd-emu8000.o
    fi
    insert snd-card-sbawe.o snd_irq=10 snd_dma8=1 snd_dma16=5
    restore sbawe.conf
    ;;
  sb8)
    basic
    insert snd-sb8-dsp.o
    insert snd-opl3.o
    insert snd-card-sb8.o snd_irq=5 snd_dma8=1
    restore sb8.conf
    ;;
  gusm*|max)
    basic
    insert snd-cs4231.o
    insert snd-gus.o
    insert snd-synth-gus.o
    insert snd-card-gusmax.o snd_irq=7 snd_pcm_voices=8
    restore gusmax.conf
    ;;
  gusc*|clas*)
    basic
    insert snd-gus.o
    insert snd-synth-gus.o
    insert snd-card-gusclassic.o snd_irq=7 snd_dma1=6 snd_dma2=7
    restore gusclassic.conf
    ;;
  guse*|ext*)
    basic
    insert snd-es1688.o
    insert snd-mpu401-uart.o 
    insert snd-opl3.o
    insert snd-gus.o
    insert snd-synth-gus.o
    insert snd-card-gusextreme.o snd_irq1=7 snd_mpu_port=0x300
    restore gusextreme.conf
    ;;
  opl3sa2)
    basic; isapnp
    insert snd-cs4231.o
    insert snd-mpu401-uart.o
    insert snd-opl3.o
    insert snd-card-opl3sa2.o snd_port=0x100 snd_wss_port=0xe80 \
                        snd_fm_port=0x388 snd_midi_port=0x300 \
                        snd_irq=5 snd_dma1=0 snd_dma2=1
    restore opl3sa2.conf
    ;;
  mozart)
    basic
    insert snd-ad1848.o
    insert snd-card-mozart.o
    restore mozart.conf
    ;;
  p1)
    basic; isapnp
    insert snd-sb16-dsp.o
    insert snd-mpu401-uart.o 
    insert snd-card-sb16.o snd_irq=5 snd_dma8=1 snd_dma16=5 snd_mpu_port=0x330
    insert snd-gus.o
    insert snd-card-gusclassic.o snd_dma2=-1
    restore p1.conf
    ;;
  p2)
    basic
    insert snd-cs4231.o
    insert snd-es1688.o
    insert snd-mpu401-uart.o 
    insert snd-gus.o
    insert snd-card-gusmax.o
    insert snd-card-gusextreme.o snd_irq1=11 snd_mpu_port=0x300
    restore p2.conf
    ;;
  s3)
    basic
    insert snd-mpu401-uart.o 
    insert snd-s3-86c617.o
    insert snd-opl3.o
    insert snd-card-sonicvibes.o snd_reverb=1
    restore sonicvibes.conf
    ;;
  ens1370)
    basic
    insert snd-ak4531-codec.o
    insert snd-ens1370.o
    insert snd-card-ens1370.o
    restore ens1370.conf
    ;;
  ens1371)
    basic
    insert snd-ac97-codec.o
    insert snd-ens1371.o
    insert snd-card-ens1371.o
    restore ens1371.conf
    ;;
  trident)
    basic
    insert snd-mpu401-uart.o 
    insert snd-ac97-codec.o
    insert snd-trident.o
    insert snd-card-trident.o
    insert snd-synth-trident.o
    restore trid4dwave.conf
    ;;
  cs461x)
    basic
    insert snd-ac97-codec.o
    insert snd-cs461x.o
    insert snd-card-cs461x.o
    restore cs461x.conf
    ;;
  fm801)
    basic
    insert snd-ac97-codec.o
    insert snd-fm801.o
    insert snd-mpu401-uart.o 
    insert snd-opl3.o
    insert snd-card-fm801.o
    restore fm801.conf
    ;;
  es1938|solo1)
    basic
    insert snd-es1938.o
    insert snd-card-es1938.o
    restore solo1.conf
    ;;
  cs4232)
    basic; isapnp
    insert snd-mpu401-uart.o 
    insert snd-cs4231.o
    insert snd-opl3.o
    insert snd-card-cs4232.o snd_port=0x534 \
		snd_fm_port=0x388 snd_mpu_port=0x330 snd_jport=0x208 \
		snd_dma1=1 snd_dma2=0 \
		snd_irq=5 snd_mpu_irq=7
    restore cs4232.conf
    ;;
  cs4236)
    basic; isapnp
    insert snd-mpu401-uart.o 
    insert snd-cs4231.o
    insert snd-cs4236.o
    insert snd-opl3.o
#    insert snd-card-cs4236.o snd_port=0x534 snd_cport=0x120 \
#		snd_fm_port=0x388 snd_mpu_port=0x330 snd_jport=0x208 \
#		snd_dma1=1 snd_dma2=0 \
#		snd_irq=5 snd_mpu_irq=7
    insert snd-card-cs4236.o
    restore cs4236.conf
    ;;
  dummy)
    basic
    insert snd-card-dummy.o
    restore dummy.conf
    ;;
  t1)
    basic; isapnp
    insert snd-mpu401-uart.o 
    insert snd-cs4231.o
    insert snd-cs4236.o
    insert snd-opl3.o
    insert snd-sb16-dsp.o
    insert snd-sb16-csp.o
    insert snd-card-sb16.o snd_irq=10 snd_dma8=1 snd_dma16=5
    insert snd-card-cs4236.o
    ;;
  detect)
    isapnp
    if ! $INSMOD -m $MODDIR/snd.o > $MAP; then exit 1; fi
    if ! $INSMOD -m $MODDIR/snd-detect.o >> $MAP; then exit 1; fi
    cat /proc/asound/detect
    ;;
  *)
    echo "Unknown soundcard $1..."
    ;;
esac
echo "Insert done..."
