#!/bin/bash
if [ `id -u` != 0 ]; then
	echo "You must be root to install CommandIR and LIRC software.  su to root or"
	echo "run 'sudo sh $0'."
	exit 1
fi

precompiled="commandir-`uname -r`.ko"
prepath="/download/precompiled/"
source="commandir-1.4.4.tbz"
sourcepath="/download/"

server="http://support.commandir.com"
installdir="/lib/modules/`uname -r`/extras/"

lircconfigscript="getlircconfig.sh"
lircconfigpath="http://lircconfig.commandir.com/download/getlirc/"

#  see if there is a pre-compiled version for this kernel:
wget $server$prepath$precompiled -O $precompiled 2>&1
if [ -e x$precompiled ]; then
	echo "Precompiled driver found, installing..."
	mkdir -p $installdir
	cp $precompiled $installdir
	depmod -a
	echo "... installed driver for `uname -r`"
else
	echo "No precompiled driver found, downloading source..."
	wget $server$sourcepath$source -O $source
	if [ ! -e $source ] ; then
	echo "No source file found?  Check http://support.commandir.com/ for an updated installation script."
	exit 1
else
	tar -xjvf $source
	cd commandir-1.4.4
	make
	make install
	depmod -a
	echo "CommandIR driver compiled and installed"
fi
fi
set -e

echo "Testing for LIRC..."
locallirc="`whereis lircd`"

if [ "x$locallirc" != "xlircd:" ]; then
	echo "Reconfiguring LIRC for CommandIR Mini:"
	apt-get -y install lirc lirc-x module-assistant lirc-modules-source

	if [ -x /usr/src/lirc*deb ]; then

          result=`rm -f /usr/src/lirc*deb`
	fi
	modname="lirc-modules-`uname -r`"
        checkmodules=`dpkg --get-selections $modname`
	if [ $? -ge 1 ]; then
		echo "REMOVING lirc-modules-$modname"
	        apt-get -y remove lirc-modules-$modname
	else
		echo "NOT REMOVING lirc-modules-$modname"
	fi
        m-a clean lirc
        dpkg-reconfigure lirc-modules-source
        m-a update,prepare
        m-a a-i lirc
        depmod -a
	
else
	echo "LIRC not found; installing for you..."
	apt-get -y install lirc lirc-x module-assistant lirc-modules-source
	result=`rm -f /usr/src/lirc*deb`
	apt-get -y remove lirc-modules-`uname -r`
	m-a clean lirc
	dpkg-reconfigure lirc-modules-source
	m-a update,prepare
	m-a a-i lirc
	depmod -a
	echo "LIRC and modules installed, test loading now:  "
	modprobe lirc_cmdir
	echo "Test successful."
fi

# add commandir LIRC module to hardware configuration
grep -v MODULES /etc/lirc/hardware.conf > /tmp/hardware.conf
echo "MODULES=\"lirc_cmdir\"" >> /tmp/hardware.conf
echo "LOAD_MODULES=\"true\"" >> /tmp/hardware.conf
cp /tmp/hardware.conf /etc/lirc/

echo "Configured LIRC to load CommandIR driver lirc_cmdir at startup."

wget $lircconfigpath$lircconfigscript -O /usr/bin/$lircconfigscript
chmod +x /usr/bin/$lircconfigscript

echo "Installed LIRC remote configuration auto-install script, $lircconfigscript."
echo "Restarting LIRC..."
/etc/init.d/lirc restart

echo ""
echo "Software installation complete.  Next you must get the IR codes for "
echo "the remotes you wish to use and for the devices you wish to control."
echo "Check out http://lircconfig.commandir.com/ or "
echo "http://lirc.sourceforge.net/remotes/ for some commonly used codes and"
echo "replace /etc/lirc/lircd.conf with your codes."
echo ""
echo "Thanks for choosing CommandIR!"


