#!/bin/bash
# 
# u804fixsound.sh: Fix the sound problem from the Ubuntu 8.04 to the followed sound card:
#                  00:1b.0 Audio device: Intel Corporation 82801H (ICH8 Family) HD Audio Controller (rev 03)
#
# WARNING: There isn't any kind of warranty about some damages this script can do in your system. Run it for your own risk!
# 
# Author.......: Jansen Sena <jansen@jsena.info> 
# Date.........: July, 14th 2008
# 

#
# System utilities
#
LSPCI="/bin/lspci"
GREP="/bin/grep"
DATE="/bin/date"

#
# Configurations
#
SOUND_CARD_MODEL="00:1b.0 Audio device: Intel Corporation 82801H (ICH8 Family) HD Audio Controller (rev 03)"
ALSA_BASE_FILE="/etc/modprobe.d/alsa-base"
ALSA_BASE_CONFIG="options snd-hda-intel model=lenovo"
LSB_UBUNTU_ID="Ubuntu"
LSB_UBUNTU_RELEASE="8.04"

echo -n "Checking root privilegies..."
if [ "$USER" != "root" ] ; then 
	echo -e "\033[55G[\033[31;1mFailed\033[m]"
	echo "You must to run this script with root privilegies."
	echo "Aborting."
	exit 1
else
	echo -e "\033[55G[\033[32;1mOK\033[m]"
fi 

# Checking if the Linux operating system is Ubuntu 8.04 (Hardy Heron)
ANSWER="trash"
if [ ! -f /etc/lsb-release ]; then
	echo "I am not sure if your Linux is $LSB_UBUNTU_ID ($LSB_UBUNTU_RELEASE). Where is /etc/lsb-release file?"
else
	source /etc/lsb-release
	if [ "$LSB_UBUNTU_ID" == "$DISTRIB_ID" ] && [ "$LSB_UBUNTU_RELEASE" == "$DISTRIB_RELEASE" ]; then
		echo "I detected your $LSB_UBUNTU_ID ($LSB_UBUNTU_RELEASE)."
		ANSWER="y"
	else
		echo "The system is not $LSB_UBUNTU_ID ($LSB_UBUNTU_RELEASE)."
	 	echo "Your system detected: $DISTRIB_ID ($DISTRIB_RELEASE)."
	fi
fi
while [ "$ANSWER" != "y" ] && [ "$ANSWER" != "Y" ] ; do
	echo -n "Would you like to continue (y/n)? "
	read ANSWER
	if [ "$ANSWER" == "n" ] || [ "$ANSWER" == "N" ] ; then 
		echo "Aborting."
		exit 2
	fi	
done

echo -n "Checking for sound card model..."
$LSPCI | $GREP "$SOUND_CARD_MODEL" >/dev/null 2>&1
if [ $? -ne 0 ] ; then
	echo -e "\033[55G[\033[31;1mFailed\033[m]"
	echo "You sound card does not seem to be $SOUND_CARD_MODEL"
	echo "Aborting."
	exit 1
else
	echo -e "\033[55G[\033[32;1mOK\033[m]"
fi
sleep 2

echo -n "Checking for $ALSA_BASE_FILE file..."
if [ ! -f "$ALSA_BASE_FILE" ] ; then
	echo -e "\033[55G[\033[31;1mFailed\033[m]"
	echo "Aborting."
	exit 2
else
	echo -e "\033[55G[\033[32;1mOK\033[m]"
fi
sleep 2

echo -n "Checking for correct configuration..."
$GREP "$ALSA_BASE_CONFIG" "$ALSA_BASE_FILE" >/dev/null 2>&1
if [ $? -eq 0 ] ; then
	echo -e "\033[55G[\033[31;1mFound\033[m]"
	echo "The fix is already included in the $ALSA_BASE_FILE file."
	echo "Aborting."
	exit 3
else 
	echo -e "\033[55G[\033[32;1mNot found\033[m]"
fi 

echo -n "Fixing your sound card problem..."
echo "" >> "$ALSA_BASE_FILE"
echo "### This line was included by $(basename $0) script to fix sound problem." >> "$ALSA_BASE_FILE"
echo "### Date: $($DATE +%m/%d/%Y)" >> "$ALSA_BASE_FILE"
echo "$ALSA_BASE_CONFIG" >> "$ALSA_BASE_FILE"
echo -e "\033[55G[\033[32;1mOK\033[m]"

echo ""
echo "The fix was successfully included."
echo "Please, reboot your system and check if your sound card is working fine."
exit 0

