#!/bin/bash

INSMOD="/sbin/insmod"
MODPROBE="/sbin/modprobe"
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() {
  $MODPROBE -q soundcore 2> /dev/null
  banner "snd-page-alloc"
  if ! $INSMOD -m $MODDIR/snd-page-alloc.o >> $MAP; then exit 1; fi
  banner "snd"
  if ! $INSMOD -m $MODDIR/snd.o cards_limit=$CARDS_LIMIT >> $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 preallocate_dma=1 >> $MAP; then exit 1; fi
  if [ -r $MODDIR/snd-pcm-oss.o ]; then
    banner "snd-pcm-oss"
    if ! $INSMOD -m $MODDIR/snd-pcm-oss.o >> $MAP; then exit 1; fi  
  fi
  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
  if [ -r $MODDIR/snd-rawmidi.o ]; then
    banner "snd-rawmidi"
    if ! $INSMOD -m $MODDIR/snd-rawmidi.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
    banner "snd-seq"
    if ! $INSMOD -m $MODDIR/snd-seq.o seq_default_timer_class=2 seq_default_timer_card=0 seq_default_timer_device=0 seq_default_timer_subdevice=0 >> $MAP; then exit 1; fi
    if [ -r $MODDIR/snd-seq-midi-event.o ]; then
      banner "snd-seq-midi-event"
      if ! $INSMOD -m $MODDIR/snd-seq-midi-event.o >> $MAP; then exit 1; fi
    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-ainstr-fm.o ]; then
      banner "snd-ainstr-fm"
      if ! $INSMOD -m $MODDIR/snd-ainstr-fm.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

if [ -w $MAP ]; then
  mv -f $MAP $OLDMAP
fi

./remove

case "$1" in
  interwave-stb|stb)
    basic; isapnp
    insert snd-cs4231-lib.o
    insert snd-gus.o
    insert snd-gus-synth.o
    insert snd-i2c.o
    insert snd-tea6330t.o
    insert snd-interwave-stb.o port=0x240 irq=7 dma1=5 dma2=6 midi=1 pcm_channels=8
    restore interwave-stb.conf
    ;;
  interwave|iw)
    basic; isapnp
    insert snd-cs4231-lib.o
    insert snd-gus.o
    insert snd-gus-synth.o
    insert snd-interwave.o irq=7 dma1=5 dma2=6 midi=1
    restore interwave.conf
    ;;
  es1688)
    basic
    insert snd-es1688-lib.o
    insert snd-mpu401-uart.o 
    insert snd-opl3-lib.o
    insert snd-es1688.o mpu_port=0x330
    restore es1688.conf
    ;;
  es18xx)
    basic; isapnp
    insert snd-mpu401-uart.o 
    insert snd-opl3-lib.o
    insert snd-es18xx.o irq=7 dma1=0 dma2=1
    restore es18xx.conf
    ;;
  sb16)
    basic; isapnp
    insert snd-sb-common.o
    insert snd-mpu401-uart.o 
    insert snd-sb16-dsp.o
    insert snd-sb16-csp.o
    insert snd-opl3-lib.o
    insert snd-sb16.o irq=7 dma8=1 dma16=5
    restore sb16.conf
    ;;
  sb)
    basic; isapnp
    insert snd-sb-common.o
    insert snd-sb8-dsp.o
    insert snd-opl3-lib.o
    insert snd-sb8.o port=0x220 irq=7 dma8=1
    restore sb.conf
    ;;
  sbawe)
    basic; isapnp
    insert snd-sb-common.o
    insert snd-sb16-dsp.o
    insert snd-sb16-csp.o
    insert snd-mpu401-uart.o 
    insert snd-opl3-lib.o
    if [ -r $MODDIR/snd-emu8000.o ]; then
      insert snd-emu8000.o
    fi
    insert snd-sbawe.o irq=7 dma8=1 dma16=5
    restore sbawe.conf
    ;;
  emu10k1|live)
    basic
    insert snd-ac97-codec.o
    insert snd-util-mem.o
    insert snd-emu10k1.o
    insert snd-seq-virmidi.o
    insert snd-emux-synth.o
    insert snd-emu10k1-synth.o
    restore emu10k1.conf
    ;;
  sb8)
    basic
    insert snd-sb-common.o
    insert snd-sb8-dsp.o
    insert snd-opl3-lib.o
    insert snd-sb8.o irq=5 dma8=1
    restore sb8.conf
    ;;
  gusm*|max)
    basic
    insert snd-cs4231-lib.o
    insert snd-gus.o
    insert snd-gus-synth.o
    insert snd-gusmax.o irq=7 pcm_channels=8 dma1=6 dma2=7
    restore gusmax.conf
    ;;
  gusc*|clas*)
    basic
    insert snd-gus.o
    insert snd-gus-synth.o
    insert snd-gusclassic.o irq=7 dma1=6 dma2=7
    restore gusclassic.conf
    ;;
  guse*|ext*)
    basic
    insert snd-es1688.o
    insert snd-mpu401-uart.o 
    insert snd-opl3-lib.o
    insert snd-gus.o
    insert snd-gus-synth.o
    insert snd-opl3-synth.o
    insert snd-gusextreme.o irq=5 gf1_irq=11 mpu_port=0x300
    restore gusextreme.conf
    ;;
  opl3sa2)
    basic; isapnp
    insert snd-cs4231.o
    insert snd-mpu401-uart.o
    insert snd-opl3-lib.o
    insert snd-opl3sa2.o port=0x100 wss_port=0xe80 \
                        fm_port=0x388 midi_port=0x300 \
                        irq=5 dma1=0 dma2=1
    restore opl3sa2.conf
    ;;
  mozart)
    basic
    insert snd-ad1848.o
    insert snd-mozart.o
    restore mozart.conf
    ;;
  p1)
    basic; isapnp
    insert snd-sb16-dsp.o
    insert snd-mpu401-uart.o 
    insert snd-sb16.o irq=5 dma8=1 dma16=5 mpu_port=0x330
    insert snd-gus.o
    insert snd-gusclassic.o 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-gusmax.o
    insert snd-gusextreme.o irq1=11 mpu_port=0x300
    restore p2.conf
    ;;
  s3)
    basic
    insert snd-mpu401-uart.o 
    insert snd-opl3-lib.o
    insert snd-sonicvibes.o reverb=1
    restore sonicvibes.conf
    ;;
  ens1370)
    basic
    insert snd-ak4531-codec.o
    insert snd-ens1370.o
    restore ens1370.conf
    ;;
  ens1371)
    basic
    insert snd-ac97-codec.o
    insert snd-ens1371.o
#    insert snd-share.o
    restore ens1371.conf
    ;;
  trident)
    basic
    insert snd-mpu401-uart.o 
    insert snd-ac97-codec.o
    insert snd-util-mem.o
    insert snd-trident.o
    insert snd-trident-synth.o
    restore trident.conf
    ;;
  cs46xx)
    basic
    insert snd-ac97-codec.o
    insert snd-cs46xx.o
    restore cs46xx.conf
    ;;
  cs4281)
    basic
    insert snd-ac97-codec.o
    insert snd-opl3-lib.o
    insert snd-cs4281.o
    restore cs4281.conf
    ;;
  fm801)
    basic
    insert snd-ac97-codec.o
    insert snd-mpu401-uart.o 
    insert snd-opl3-lib.o
    insert snd-fm801.o
    restore fm801.conf
    ;;
  es1938|solo1)
    basic
    insert snd-mpu401-uart.o
    insert snd-opl3-lib.o
    insert snd-es1938.o    
    restore solo1.conf
    ;;
  cs4232)
    basic; isapnp
    insert snd-mpu401-uart.o 
    insert snd-cs4231-lib.o
    insert snd-opl3-lib.o
    insert snd-cs4232.o port=0x534 \
		fm_port=0x388 mpu_port=0x330 jport=0x208 \
		dma1=1 dma2=0 \
		irq=5 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-lib.o
#    insert snd-cs4236.o port=0x534 cport=0x120 \
#		fm_port=0x388 mpu_port=0x330 jport=0x208 \
#		dma1=1 dma2=0 \
#		irq=5 mpu_irq=7
    insert snd-cs4236.o irq=7
    restore cs4236.conf
    ;;
  ice1712|envy24)
    basic
    insert snd-ac97-codec.o
    insert snd-mpu401-uart.o 
    insert snd-i2c.o
    insert snd-cs8427.o
    insert snd-ice1712.o enable=1,1
    restore ice1712.conf
    ;;
  i8x0|ich)
    basic
    insert snd-ac97-codec.o
    insert snd-mpu401-uart.o 
    insert snd-intel8x0.o ac97_clock=48000
    restore intel8x0.conf
    ;;
  pdplus|prodif-plus)
    basic
    insert snd-pdplus.o
    restore prodif-plus.conf
    ;;
  via686*|via8233*|via)
    basic
    insert snd-ac97-codec.o
    insert snd-mpu401-uart.o
    insert snd-via82xx.o mpu_port=0x330
    restore via82xx.conf
    ;;
  ymfpci|yamaha)
    basic
    insert snd-ac97-codec.o
    insert snd-mpu401-uart.o
    insert snd-opl3-lib.o
    insert snd-ymfpci.o
    restore ymfpci.conf
    ;;
  dummy)
    basic
    insert snd-dummy.o enable=1,1 pcm_devs=4,4
    #restore dummy.conf
    ;;
  virmidi)
    basic
    insert snd-seq-virmidi.o
    insert snd-virmidi.o enable=1,1 midi_devs=8,8
    ;;
  serialmidi)
    basic
    insert snd-serialmidi.o
    ;;
  usb)
    basic
    insert snd-usb-audio.o
    ;;
  vxpocket2)
    basic
    insert snd-vx-lib.o
    insert snd-vx-cs.o
    insert snd-vxpocket.o
    ;;
  pdaudiocf)
    basic
    insert snd-ak4117.o
    insert snd-pdaudiocf.o
    ;;
  t1)
    basic; isapnp
    insert snd-mpu401-uart.o 
    insert snd-cs4231-lib.o
    insert snd-cs4236-lib.o
    insert snd-opl3-lib.o
    insert snd-sb16-dsp.o
    insert snd-sb16-csp.o
    insert snd-sb16.o irq=10 dma8=1 dma16=5
    insert snd-cs4236.o
    ;;
  t2)
    basic
    insert snd-ac97-codec.o
    insert snd-util-mem.o
    insert snd-emu10k1.o
    insert snd-seq-virmidi.o
    insert snd-emux-synth.o
    insert snd-emu10k1-synth.o
    insert snd-mpu401-uart.o 
    insert snd-ice1712.o
    restore t2.conf
    ;;
  t3)
    basic
    insert snd-ac97-codec.o
    insert snd-mpu401-uart.o
    insert snd-opl3-lib.o
    insert snd-ymfpci.o
    insert snd-i2c.o
    insert snd-cs8427.o
    insert snd-ice1712.o
    restore t3.conf
    ;;
  t4)
    basic
    insert snd-ak4117.o
    insert snd-pdaudiocf.o
    insert snd-mpu401-uart.o 
    insert snd-ac97-codec.o
    insert snd-util-mem.o
    insert snd-trident.o
    #insert snd-trident-synth.o
    restore trident.conf
    ;;
  *)
    echo "Unknown soundcard $1..."
    ;;
esac
echo "Insert done..."
