#!/bin/bash
# Run this script in an empty directory
# Optionally put the hpklinux, alsa-driver and alsa-lib tarballs in the
# directory to avoid redownloading them
# It downloads all the necessary parts and builds alsa driver with
# latest hpklinux
# Not much error detection or recovery, if something fails, you will
# have to complete the process manually
# You may also want to add other drivers to the alsa-driver configure commandline

# HPKDIR="released/v4/10/10"
HPKDIR="beta/v4/11/08"
HPKVER="4.11.08"

ALSAVER="1.0.25"
ALSADRV=$ALSAVER
ALSALIB=$ALSAVER
ALSAUTILS=$ALSAVER

if [ ! -e hpklinux-$HPKVER.tar.bz2 ]
then
   wget http://audioscience.com/internet/download/drivers/$HPKDIR/hpklinux-$HPKVER.tar.bz2
fi
tar xf hpklinux-$HPKVER.tar.bz2

if [ ! -e alsa-driver-$ALSADRV.tar.bz2 ]
then
wget ftp://ftp.alsa-project.org/pub/driver/alsa-driver-$ALSADRV.tar.bz2
fi
tar xf alsa-driver-$ALSADRV.tar.bz2

if [ ! -e alsa-lib-$ALSALIB.tar.bz2 ]
then
wget ftp://ftp.alsa-project.org/pub/lib/alsa-lib-$ALSALIB.tar.bz2
fi
tar xf alsa-lib-$ALSALIB.tar.bz2

if [ ! -e alsa-utils-$ALSAUTILS.tar.bz2 ]
then
wget ftp://ftp.alsa-project.org/pub/utils/alsa-utils-$ALSAUTILS.tar.bz2
fi
tar xf alsa-utils-$ALSAUTILS.tar.bz2

cd hpklinux-$HPKVER
./configure
make
echo "Doing sudo make install hpklinux"
sudo make install
cd ..

cd alsa-driver-$ALSADRV
./configure --with-cards=asihpi --with-debug=full
rm pci/asihpi/*.[ch]
cp ../hpklinux-$HPKVER/alsa/Makefile.alsa pci/asihpi/Makefile
make
echo "Doing sudo make install alsa-driver"
sudo make install
cd ..

cd alsa-lib-$ALSALIB
./configure
make
echo "Doing sudo make install alsa-lib"
sudo make install
cd ..

cd alsa-utils-$ALSAUTILS
./configure
make
echo "Doing sudo make install alsa-utils"
sudo make install
cd ..

sudo modprobe -r asihpi # cant run both drivers simultaneously!
sudo modprobe snd-asihpi
dmesg
echo "if snd-asihpi driver has not loaded, you probably need to unload ALL"
echo "ALSA driver modules before reloading"
