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

# usually pps0 is pps, but imx6dl has ptp0 as pps0
# so we need to use ppsfind
PPS_NAME=$(ppsfind pps | cut -d: -f1)
[ -z "$PPS_NAME" ] && PPS_NAME=pps0
DEV_PPS=/dev/$PPS_NAME
TTY_GPS=/dev/ttyUSB11

DEV_GPS=/dev/gps0
DEV_GPSPPS=/dev/gpspps0

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 $?
