#!/bin/bash
# pps-setup - configures GPIO so that it can be polled for the GPS's

function ConfigurePPS()
{
  if [ ! -d /debug ]; then
    mkdir /debug
  fi

  # Need debugfs to set Pin MUX
  mount -t debugfs nodev /debug

  # GPIO 167, input
  echo 0x104 > /debug/omap_mux/cam_wen

  # Provision GPIO access
  if [ ! -d /sys/class/gpio/gpio167 ]; then
    echo 167 > /sys/class/gpio/export
  fi

  echo out > /sys/class/gpio/gpio167/direction

  # This needs to toggle to get it working, not sure why yet
  echo 0 > /sys/class/gpio/gpio167/active_low
  echo 1 > /sys/class/gpio/gpio167/active_low

  echo rising > /sys/class/gpio/gpio167/edge

  umount /debug
}

ppssetup=`which pps-setup`
lockfile=`dirname "$psssetup"`.lockfile

if [ "$USER" = "root" ]
then
  ConfigurePPS
else
  (
    flock -x 200
    sudo $ppssetup
  ) 200>"$lockfile"
fi
