#!/bin/bash
VER=0.4.5

KERNEL_RPM=linux-2.6.11-iose_0.2.rpm
KERNEL_SMP_RPM=linux-smp-2.6.11-iose_0.2.rpm

#Verify we are root`
if [ `id -u` != 0 ]; then
    echo "Need to be root to install."
    exit 1
fi

#Set variables, create directories
if [ -z $BIN_BASE ]; then
    BIN_BASE=`pwd`
fi
if [ -z $SDK_ROOT ]; then
    SDK_ROOT=/opt/iop-sdk
fi
SDK_DIR=`dirname $SDK_ROOT`
SDK_ROOT_BASE=`basename $SDK_ROOT`
LIBUSB_ROOT=/usr/local
IOSE_TARBALL=$BIN_BASE/$IOSE_TARBALL/iose$VER.tar.gz
IOSE_EXAMPLES=examples.tar.gz
IOSE_LIBUSB=$BIN_BASE/libusb.broadon.tar.gz
IOSE_REGIRQ=reg_irq

#Install libusb
pushd /usr/local >/dev/null
tar -xzf $IOSE_LIBUSB
popd  >/dev/null

# If $SDK_DIR exists, clean it
if [ -d $SDK_DIR ]; then
    rm -rf $SDK_ROOT
else
    echo "Creating $SDK_DIR"
    mkdir -p $SDK_DIR
fi

#Install iose
pushd $SDK_DIR >/dev/null
echo "Installing IOP OS emulator SDK version $VER in $SDK_ROOT"
tar -xzf $IOSE_TARBALL
if [ $SDK_ROOT_BASE != iop-sdk ]; then
    mv iop-sdk $SDK_ROOT_BASE
fi
popd >/dev/null

#Install reg_irq utility used to register user-space
#interrupt handlers
# If utils directory does not exist, create it.
if [ ! -d $SDK_ROOT/usr/bin/utils ]; then
    mkdir -p $SDK_ROOT/usr/bin/utils
fi
cp $IOSE_REGIRQ $SDK_ROOT/usr/bin/utils

#Install examples
echo "Installing examples in: /tmp"
cd /tmp
tar -xzf $BIN_BASE/$IOSE_EXAMPLES

echo
echo "**** If you wish to run the USB-mouse example or need emulator support for IRQ's, you need to do the following ****"
echo "Please install the supplied patched 2.6.11 kernel:"
echo " On SMP/hyper-threaded systems: 'rpm -Uvh --force kernel-smp-2.6.11-iose_2.0.i686.rpm"
echo " On uni-processor systems: 'rpm -Uvh --force kernel-2.6.11-iose_2.0.i686.rpm"
