#!/bin/sh

#####################################################################
# This script is provided in hopes that it will be useful to
# Technologic Systems customers.  There are no guarantees or 
# warranties.  It was lightly tested using the Debian Sarge
# distribution which is provided in the default, factory software 
# load for the TS-7800.  Also, the TS-CAN1 PC/104 peripheral had 
# JP4 ON, all others OFF.
#
# Here is an overview of what this script actually does:
#
#    1.) Determine if the proper binaries are installed.  If not, 
#        automatically download and install them.  You will need to 
#        ensure the Ethernet cable is connected.
#    2.) Ensure that the PC/104 bus is ON.
#    3.) Insert/install the driver into the running kernel.
#    4.) Ensure that the /bin-utils/ directory is in $PATH.
#    5.) Run the 'sendburst' utility as a demonstration.
#
# Feel free to use this script in your own application and/or modify
# it to fit.  Also, keep in mind that this can be included in the 
# Debian init/runlevel scripts so it loads automatically when the 
# board boots.  Refer to the following resource for more information:
# http://www.embeddedarm.com/about/resource.php?item=410#runonbootup
#
# Also, you may need to ensure that the script has executable rights
# by using the command: chmod +x ts-can1-7800
#
# -- DRH 08/18/2010
#####################################################################

if [ ! -e /bin-utils/sendburst ]; then
	# Remove just in case this has been attempted before
	rm -rf ocera-lincan-compiled.tar.gz*
	
	echo -n "Obtaining IP address via DHCP..."	
	pump
	echo "done"
	
	echo "Begin downloading required binaries for TS-CAN1"
	cd /root/
	wget ftp://ftp.embeddedarm.com/ts-arm-sbc/ts-7800-linux/\
binaries/ocera-lincan-compiled.tar.gz
	cd -
	echo "Finished downloading required binaries for TS-CAN1"

	echo -n "Installing required binaries for TS-CAN1..."
	tar xzf ocera-lincan-compiled.tar.gz -C /root/
	cd /root/lincan-7800/
	tar czf /root/ocera-lincan-compiled-root.tar.gz .
	tar xzf /root/ocera-lincan-compiled-root.tar.gz -C /
	cd -
	echo "done"

	echo -n "Cleaning up..."
	rm -rf /root/lincan-7800/
	rm -rf /root/ocera-lincan-compiled-root.tar.gz
	echo "done"
	echo "NOTE: Original download kept for reference (ocera-lincan-compiled.tar.gz)"
fi

echo -n "Turning PC/104 bus ON..."
(
	peekpoke 32 0xe8000030 0x55555555
	peekpoke 32 0xe8000034 0x55555555
	peekpoke 32 0xe8000038 0x55555
	peekpoke 32 0xe800003c 0x55555     
) > /dev/null
echo "done"

lsmod | grep lincan > /dev/null
if [ $? -eq 1 ]; then
	echo -n "Installing lincan.ko module..."
	insmod /modules/lincan.ko hw=tscan1 irq=6 io=0x150 baudrate=500
	echo "done"
fi

echo $PATH | grep bin-utils > /dev/null
if [ $? -eq 1 ]; then
	echo -n "Adding /bin-utils to PATH..."
	PATH=$PATH:/bin-utils/
	echo "done"
fi

echo "Running example utility \"sendburst\""
echo ">> sendburst -w1 -b5 -c5 -d /dev/can0"
sendburst -w1 -b5 -c5 -d /dev/can0

