#!/bin/bash
######################################################
#    rtload
#
#    ARTIS loader
#
# Modify this template to suit your own application
######################################################

# First, test if the number of arguments is correct and all files exist...
if [ $# -lt 2 ]
then
	RUNTIME=-1
	TRAZA=traza
#	echo 'Use: rtload time trace_file'
#	exit
else
	RUNTIME=$1
	TRAZA=$2
fi

RTL_MODULES_LIST="mbuff rtl rtl_time rtl_posixio rtl_fifo"
KERNEL_VERSION=`cat /proc/version | cut -d " " -f 3`
MHZ=`cat /proc/cpuinfo | grep "cpu MHz" | cut -f 2 -d: `


# First make sure there are no old stuff loaded
/sbin/rmmod rtl_tasks_fprio 2>/dev/null
/sbin/rmmod rtl_artis 2>/dev/null
rtlinux stop
rm -f $TRAZA.*

# Load the required rt-linux modules:
for i in $RTL_MODULES_LIST
   do
      echo "Loading $i"
      /sbin/insmod /lib/modules/$KERNEL_VERSION/misc/$i.o
   done

# Load the rt-linux ARTIS module:
echo "Loading Reflex Layer"
sync
/sbin/insmod /lib/modules/$KERNEL_VERSION/misc/rtl_artis.o
sync

# Loading the KDM Data
sync
/sbin/insmod ./rtl_userclass.o

# Loading the Meta-KDM
sync
/sbin/insmod ./rtl_metavalue.o

# Run Deliberative Layer
echo "Starting Deliberative Layer"
./artis_delib 2> salida &


echo "Press any key to start Reflex Layer"
read junk
# Load the rt-linux APP module:
echo "Loading tasks module"

sync


###############################################################
# UNCOMMENT THESE LINES IF YOU WANT TO USE THE SERIAL PORT    #
# IN RT-LINUX USING ITS OWN DRIVER: RT_COM                    #
###############################################################
#
#setserial /dev/ttyS0 uart none
#setserial /dev/ttyS1 uart none
#/sbin/insmod /usr/src/rtlinux/drivers/rt_com/rt_com.o
###############################################################


###############################################################
# LOAD HERE ANY OTHER MODULE rtl_tasks.o MAY NEED             #
###############################################################
#/sbin/insmod <your_module1.o>
#/sbin/insmod <your_module2.o>
###############################################################

echo "Waiting ..."
sleep 5
echo "Loading rtl_tasks"
/sbin/insmod ./rtl_tasks.o

if  [ 0 -lt $RUNTIME ]
then
	# Now sleep for a while:
	echo "Waiting $RUNTIME seconds..."
	sleep $RUNTIME

	# Unload application module:
	echo "Removing rtl_tasks"
	/sbin/rmmod rtl_tasks
	echo "Waiting...(5s)"
	sleep 5

###############################################################
# UNLOAD HERE ALL OF THE MODULES NEEDED BY rtl_tasks.o IN THE #
# REMEMBER THAT MODULES HAVE TO BE REMOVED INT THE OPPOSITE   #
# ORDER AS THEY WERE INSERTED                                 #
###############################################################
#	/sbin/rmmod your_module2
#	/sbin/rmmod your_module1
###############################################################


###############################################################
# UNCOMMENT THESE LINES IF YOU WANT TO USE THE SERIAL PORT    #
# IN RT-LINUX USING ITS OWN DRIVER: RT_COM                    #
###############################################################
#
#	echo "Waiting...(5s)"
#	sleep 5
#	echo "Unloading rt_com"
#	/sbin/rmmod  rt_com
#	echo "rt_com unloaded"
#
###############################################################


###############################################################
# All trace data corresponding to the execution of the        #
# application has to be properly processed                    #
###############################################################
	echo "Generating trace files"
	/usr/local/bin/mem2trace $TRAZA  2>$TRAZA.m2t-err 
	/usr/local/bin/trace2kiwi $MHZ $TRAZA-temp 2 $TRAZA.tri linuxapp.tri 2>$TRAZA.t2k-err
	echo "Processing the output for kiwi and deleting all empty/temporal files"
	/usr/bin/sort -g $TRAZA-temp.ktr | sed s/-1//g > $TRAZA.ktr

	for i in $TRAZA*.*; do
		if [ ! -s $i ]
		then
			rm -f $i
		fi
	done
	rm -f $TRAZA-temp.ktr

	echo "Waiting for the trace to stop and stop all tasks"
	sleep 3
###############################################################


	# Unload the rest of modules:
	echo "Unloading ARTIS modules and RT-Linux modules"
	/sbin/rmmod rtl_userclass rtl_metavalue rtl_artis
	#/sbin/rmmod rtl_userclass rtl_artis
	rtlinux stop

fi
