#! /bin/sh
#
# init script for gpio pps
#

DEV_NR=0
DEV_PPS=/dev/pps$DEV_NR
TTY_GPS=/dev/ttyUSB11

DEV_GPS=/dev/gps$DEV_NR
DEV_GPSPPS=/dev/gpspps$DEV_NR

GPSENABLED=no
PORT=CNone

# Read config file if it is present.
if [ -r /etc/telem-gps.conf ]
then
    . /etc/telem-gps.conf
fi

[ "x$GPSENABLED" = "xno" ] && exit 0

# configure PPS only if C1
[ "x$PORT" = "xC1" ] || exit 0

# currently supported only non-usb ports
HW_XML=/usr/local/etc/telem/hw-run.xml
TTY_GPS="$(ls `grep index=\"1\" $HW_XML | sed 's/^.*name=\"\([^ ]*\)\".*/\1/'` | grep tty)"

start() {
    echo "Loading pps modules (pps_core pps-gpio)"
    modprobe pps_core
    modprobe pps-gpio

    echo "Setting up devices for gps and pps"
#     [[ ! -e $TTY_GPS ]] && {
#         echo "No such port: $TTY_GPS."
#     }
    
    [[ ! -e $DEV_GPS ]] && ln -s $TTY_GPS $DEV_GPS
    [[ -e $DEV_PPS ]] && [[ ! -e  $DEV_GPSPPS ]] && ln -s $DEV_PPS $DEV_GPSPPS
    
}



case "$1" in
    start)
        start
        ;;
    stop)
        : # do nothing
        ;;
    *)
        echo $"Usage: $0 {start}"
        exit 1
esac

exit $?
