#!/bin/sh

echo "Replacing inittab with 'boot' version"
#cp /usr/share/Board_Framework_Phase1Alpha/replace_inittab /etc/inittab
sed -i -e 's/PS0:12345:respawn:/PS0:12345:boot:/' /etc/inittab
echo "Issuing init service to reload inittab"
kill -HUP 1
echo "Replacing inittab with 'respawn' version"
sed -i -e 's/PS0:12345:boot:/PS0:12345:respawn:/' /etc/inittab
#cp /usr/share/Board_Framework_Phase1Alpha/original_inittab /etc/inittab
echo "Killing all processes that use /dev/ttyPS0"
for pid in $(ls -l /proc/[0-9]*/fd/* 2> /dev/null | grep ttyPS0 | awk -F/ '{ print $3 }'); do
    echo "killing $pid"
    kill -9 $pid
done
echo "Killing all processes that use /dev/ttyPS0 again (for good measure)"
for pid in $(ls -l /proc/[0-9]*/fd/* 2> /dev/null | grep ttyPS0 | awk -F/ '{ print $3 }'); do
    echo "killing $pid"
    kill -9 $pid
done
echo "Moving into BoardFramework Directory"
cd /usr/share/Board_Framework_Phase1Alpha/
echo "Launching vccaux workaround"
/usr/share/Board_Framework_Phase1Alpha/AuxiliaryScripts/vccaux_workaround_vck190.sh &
echo "Launching BoardFramework"
python3 /usr/share/Board_Framework_Phase1Alpha/boardframework.py
echo "Issuing init service to reload inittab"
echo "Getty should start up after this..."
kill -HUP 1
