#!/bin/bash

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

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

function basic() {
  if ! $INSMOD -m $MODDIR/snd.o >> $MAP; then exit 1; fi
  if ! $INSMOD -m $MODDIR/snd-mixer.o >> $MAP; then exit 1; fi
  if [ -r $MODDIR/snd-mixer-oss.o ]; then
    if ! $INSMOD -m $MODDIR/snd-mixer-oss.o >> $MAP; then exit 1; fi  
  fi
  if ! $INSMOD -m $MODDIR/snd-timer.o >> $MAP; then exit 1; fi
  if ! $INSMOD -m $MODDIR/snd-pcm.o >> $MAP; then exit 1; fi
  if ! $INSMOD -m $MODDIR/snd-pcm1.o >> $MAP; then exit 1; fi
  if [ -r $MODDIR/snd-pcm1-oss.o ]; then
    if ! $INSMOD -m $MODDIR/snd-pcm1-oss.o >> $MAP; then exit 1; fi  
  fi
  if [ -r $MODDIR/snd-midi.o ]; then
    if ! $INSMOD -m $MODDIR/snd-midi.o >> $MAP; then exit 1; fi
  fi
  if [ -r $MODDIR/snd-synth.o ]; then
    if ! $INSMOD -m $MODDIR/snd-synth.o >> $MAP; then exit 1; fi
  fi
  if [ -r $MODDIR/snd-seq.o ]; then
    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
      if ! $INSMOD -m $MODDIR/snd-seq-midi.o >> $MAP; then exit 1; fi
    fi
    if [ -r $MODDIR/snd-seq-midi-emul.o ]; then
      if ! $INSMOD -m $MODDIR/snd-seq-midi-emul.o >> $MAP; then exit 1; fi
    fi
    if [ -r $MODDIR/snd-seq-instr.o ]; then
      if ! $INSMOD -m $MODDIR/snd-seq-instr.o >> $MAP; then exit 1; fi
    fi
    if [ -r $MODDIR/snd-ainstr-iw.o ]; then
      if ! $INSMOD -m $MODDIR/snd-ainstr-iw.o >> $MAP; then exit 1; fi
    fi
    if [ -r $MODDIR/snd-seq-oss.o ]; then
      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-i2c.o
    insert snd-tea6330t.o
    insert snd-interwave-stb.o snd_irq=5 snd_dma1=5 snd_dma2=6 snd_midi=1
    restore interwave-stb.conf
    ;;
  interwave|iw)
    basic; isapnp
    insert snd-cs4231.o
    insert snd-gus.o
    insert snd-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-audiodrive1688.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-audiodrive18xx.o
    restore es18xx.conf
    ;;
  sb16)
    basic; isapnp
    insert snd-sb16-dsp.o
    insert snd-mpu401-uart.o 
    insert snd-opl3.o
    insert snd-sb16.o snd_irq=5 snd_dma8=1 snd_dma16=5
    restore sb16.conf
    ;;
  sb)
    basic; isapnp
    insert snd-sb8-dsp.o
    insert snd-opl3.o
    insert snd-sb8.o snd_port=0x220 snd_irq=5 snd_dma8=3
    restore sb.conf
    ;;
  sbawe)
    basic; isapnp
    insert snd-sb16-dsp.o
    insert snd-mpu401-uart.o 
    insert snd-opl3.o
    if [ -r $MODDIR/snd-emu8000.o ]; then
      insert snd-emu8000.o
    fi
    insert snd-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-sb8.o snd_irq=5 snd_dma8=1
    restore sb8.conf
    ;;
  gusm*|max)
    basic
    insert snd-cs4231.o
    insert snd-gus.o
    insert snd-gusmax.o snd_irq=7
    restore gusmax.conf
    ;;
  gusc*|clas*)
    basic
    insert snd-gus.o
    insert snd-gusclassic.o snd_irq=11 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-gusextreme.o snd_irq1=11 snd_mpu_port=0x300
    restore gusextreme.conf
    ;;
  opl3sa)
    basic; isapnp
    insert snd-cs4231.o
    insert snd-mpu401-uart.o
    insert snd-opl3.o
    insert snd-opl3sa.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 opl3sa.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 snd_irq=5 snd_dma8=1 snd_dma16=5 snd_mpu_port=0x330
    insert snd-gus.o
    insert snd-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-gusmax.o
    insert snd-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-sonicvibes.o snd_reverb=1
    restore sonicvibes.conf
    ;;
  ens1370)
    basic
    insert snd-ak4531-codec.o
    insert snd-ens1370.o
    insert snd-audiopci1370.o
    restore es1370.conf
    ;;
  ens1371)
    basic
    insert snd-ac97-codec.o
    insert snd-ens1371.o
    insert snd-audiopci1371.o
    restore es1371.conf
    ;;
  trident)
    basic
    insert snd-ac97-codec.o
    insert snd-trident_dx_nx.o
    insert snd-trid4dwave.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
    ;;
  solo1)
    basic
    insert snd-es1938.o
    insert snd-esssolo1.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
    ;;
  detect)
    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..."
