#!/bin/bash
#
# Script to auto start the Lane Machine GUI
#

# create the communication fifo if not present
if [ ! -r "/tmp/LMcomm" ]
    then
        mkfifo /tmp/LMcomm
fi

if [ -f "/proc/sys/cpu/0/speed" ]
    then
    insmod /opt/LM_GUI/drivers/gpio.o
else
    insmod /opt/LM_GUI/drivers/gpio.ko
fi

# create the storage directories if not present
if [ ! -r "/opt/LM_GUI/data" ]
    then
        mkdir /opt/LM_GUI/data
        mkdir /opt/LM_GUI/data/logs
        mkdir /opt/LM_GUI/data/patterns
fi

# create the storage directories if not present
if [ ! -r "/mnt/hda1" ]
    then
        mkdir /mnt/hda1
fi

mount -t vfat,ext3 /dev/hda1 /mnt/hda1

# Define the tcl/tk library path
export LD_LIBRARY_PATH=/opt/LM_GUI/lib
export PATH=$PATH:/opt/LM_GUI/scripts
export DISPLAY=:0.0

# Disable X screen saver (check for X Windows running)
i=1
while [ "$i" -ne 0 ]
do
    sleep 1
    xset s off
    i=$?
done

#xset +fp /usr/X11R6/lib/X11/fonts/Type1
#xset +fp /usr/X11R6/lib/X11/fonts/TTF

# Start the watchdog timer in a different process
if [ -f "/opt/LM_GUI/watchdog" ]
    then
        /opt/LM_GUI/watchdog &
fi

# Start the Lane Machine GUI application
cd /opt/LM_GUI/scripts

cp err4.txt err5.txt
cp err3.txt err4.txt
cp err2.txt err3.txt
cp err1.txt err2.txt
cp err.txt err1.txt

cp out4.txt out5.txt
cp out3.txt out4.txt
cp out2.txt out3.txt
cp out1.txt out2.txt
cp out.txt out1.txt

/opt/LM_GUI/lib/wish /opt/LM_GUI/scripts/main.tcl > out.txt 2> err.txt &

exit 0
