#!/bin/bash

basekdir=/usr/src/linux
#basekdir=/home/src/1/linux
kdir=$basekdir/drivers/sound
if [ "x$1" != "x" ]; then
  kdir="$1"
fi
backup=$kdir/../sound.backup

rm -rf `find $kdir -name "*.[ao]"`
rm -rf $kdir/oss
if [ -d $backup ]; then
  rm -rf $kdir/*
  mv $backup/* $kdir
fi

mkdir -p $backup
rm -rf $backup/*
cp -a $kdir/* $backup

mkdir $kdir/oss
mv $kdir/*.[chS] $kdir/oss
mv $kdir/README* $kdir/COPYING $kdir/CHANGELOG $kdir/oss
mv $kdir/Makefile $kdir/oss
mv $kdir/Config.in $kdir/oss
mv $kdir/cs4281 $kdir/oss
mv $kdir/dmasound $kdir/oss
mv $kdir/emu10k1 $kdir/oss
mv $kdir/oss/sound_firmware.[ch] $kdir
mv $kdir/oss/sound_core.c $kdir

cat > $kdir/Config.in << EOF
# drivers/sound/Config.in
#

mainmenu_option next_comment
comment 'Open Sound System'

tristate 'Open Sound System' CONFIG_SOUND_PRIME
if [ "\$CONFIG_SOUND_PRIME" != "n" ]; then
  source drivers/sound/oss/Config.in
fi

endmenu

mainmenu_option next_comment
comment 'Advanced Linux Sound Architecture'

tristate 'Advanced Linux Sound Architecture' CONFIG_SND
if [ "\$CONFIG_SND" != "n" ]; then
  source drivers/sound/Alsa-Config.in
fi

endmenu
EOF

cat > $kdir/Makefile << EOF
# Makefile for the Linux sound card driver
#

export-objs     := sound_core.o

obj-\$(CONFIG_SOUND) += soundcore.o

subdir-\$(CONFIG_SOUND_PRIME) += oss

ifeq (\$(CONFIG_SOUND_PRIME),y)
  obj-y += oss/sounddrivers.o
endif

subdir-\$(CONFIG_SND) += alsa lowlevel cards

ifeq (\$(CONFIG_SND),y)
  obj-y += alsa/s.o lowlevel/lowlevel.o cards/cards.o
endif

list-multi      := soundcore.o

soundcore-objs  := sound_core.o sound_firmware.o

O_TARGET := sounddrivers.o

include \$(TOPDIR)/Rules.make


soundcore.o: \$(soundcore-objs)
	\$(LD) -r -o \$@ \$(soundcore-objs)
EOF

cd $kdir
patch -p0 << EOF
--- sound_firmware.c.old	Tue Jul 10 13:23:20 2001
+++ sound_firmware.c	Tue Jul 10 13:24:22 2001
@@ -1,4 +1,4 @@
-#include "os.h"
+#include <linux/vmalloc.h>
 #define __KERNEL_SYSCALLS__
 #include <linux/module.h>
 #include <linux/fs.h>
EOF
cd -


cd $kdir/oss

if [ -d $kdir/alsa ]; then

  echo "ALSA already installed, trying to recover."

  patch -R -p0 << EOF
--- Makefile.old	Mon Mar 19 20:19:24 2001
+++ Makefile	Fri Mar 23 23:28:04 2001
@@ -83,6 +83,11 @@
   obj-y += dmasound/dmasound.o
 endif
 
+subdir-\$(CONFIG_SND) += alsa lowlevel cards
+
+ifeq (\$(CONFIG_SND),y)
+  obj-y += alsa/s.o lowlevel/lowlevel.o cards/cards.o
+endif
 
 # Declare multi-part drivers.
 
EOF

  patch -p0 << EOF
--- Config.in.old	Mon Oct 15 17:39:36 2001
+++ Config.in	Mon Oct 15 17:39:42 2001
@@ -212,12 +212,3 @@
   fi
 fi
 
-mainmenu_option next_comment
-comment 'Advanced Linux Sound Architecture'
-
-tristate 'Advanced Linux Sound Architecture' CONFIG_SND
-if [ "\$CONFIG_SND" != "n" ]; then
-  source drivers/sound/Alsa-Config.in
-fi
-
-endmenu
EOF

fi

patch -p0 << EOF
--- Makefile.old	Tue Jul 10 10:57:29 2001
+++ Makefile	Tue Jul 10 11:02:55 2001
@@ -9,12 +9,11 @@
 
 export-objs	:=  ad1848.o audio_syms.o midi_syms.o mpu401.o \\
 		    msnd.o opl3.o sb_common.o sequencer_syms.o \\
-		    sound_core.o sound_syms.o uart401.o	\\
+		    sound_syms.o uart401.o	\\
 		    nm256_audio.o ac97.o ac97_codec.o aci.o
 
 # Each configuration option enables a list of files.
 
-obj-\$(CONFIG_SOUND)		+= soundcore.o
 obj-\$(CONFIG_SOUND_OSS)		+= sound.o
 obj-\$(CONFIG_SOUND_CS4232)	+= cs4232.o ad1848.o 
 
@@ -87,7 +86,7 @@
 # Declare multi-part drivers.
 
 list-multi	:= sound.o gus.o pas2.o sb.o sb_lib.o vidc_mod.o \\
-    soundcore.o wavefront.o
+    wavefront.o
 
 sound-objs	:= 							\\
     dev_table.o soundcard.o sound_syms.o		\\
@@ -95,8 +94,6 @@
     midi_syms.o midi_synth.o midibuf.o					\\
     sequencer.o sequencer_syms.o sound_timer.o sys_timer.o
 
-soundcore-objs	:= sound_core.o sound_firmware.o
-
 gus-objs	:= gus_card.o gus_midi.o gus_vol.o gus_wave.o ics2101.o
 pas2-objs	:= pas2_card.o pas2_midi.o pas2_mixer.o pas2_pcm.o
 sb-objs		:= sb_card.o
@@ -115,9 +112,6 @@
 
 sound.o: \$(sound-objs)
 	\$(LD) -r -o \$@ \$(sound-objs)
-
-soundcore.o: \$(soundcore-objs)
-	\$(LD) -r -o \$@ \$(soundcore-objs)
 
 gus.o: \$(gus-objs)
 	\$(LD) -r -o \$@ \$(gus-objs)

EOF
cd -

cat << EOF
The directory $kdir/../sound.backup contains backup of original sources.
Don't forget to run bellow command, if you apply the ALSA patch later:
mv $kdir/Makefile.orig $kdir/Makefile
EOF
