#!/bin/sh
while getopts "a" option
do
    case $option in
        a) attach=1;;
    esac
done

if test ! -x /bin/gdbserver
then
    (
        cd /;
        tftp -gr gdbserver-6.1.tar.bz2 192.168.20.3;
        tar xovjf gdbserver-6.1.tar.bz2;
        rm gdbserver-6.1.tar.bz2
    )
fi

if test ${attach} -eq 1
then
    lineIoPid=$(echo $(ps | grep ARM | grep -v grep | cut -c-5) | cut -c-4)
    /bin/gdbserver 192.168.20.3:2345 --attach $lineIoPid
else
    type=$(cat /BayID.dat | cut -f3 -d'.')
    executable=""
    case "${type}" in
    40) executable="ARMLineIO251";;
    38) executable="ARMLineIO351";;
    36) executable="ARMLineIO";;
    *) echo "Unknown LINE I/O type \"$type\""; exit 0;
    esac

    /bin/gdbserver 192.168.20.3:2345 "/upgradeable/bin/${executable}"
fi 
