#!/usr/bin/env bash
#
# vim: set sw=2 ts=2 expandtab:
#
# Copyright (C) 2010 by Multi-Tech Systems
#
# Author: James Maki <jmaki@multitech.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#

PATH=${PATH}:.

source /usr/lib/mts-io-sysfs/mts-io-sysfs-inc.sh

trap "exit_handler" EXIT
exit_handler() {
  local exit_code=$?
  if [ ${exit_code} -ne 0 ]; then
    log_error "exiting with ${exit_code}"
  fi
}

reset_short_handler() {
  echo "reset short signal received"
}
reset_long_handler() {
  echo "reset long signal received"
}
RESET_SHORT_CMD="reset_short_handler"
RESET_LONG_CMD="reset_long_handler"
RESET_SHORT_SIGNAL=$(kill -l SIGUSR1)
RESET_LONG_SIGNAL=$(kill -l SIGUSR2)

PROGRAM=$(basename $0)
VERSION="0.0.2"

print_version() {
  printf "${PROGRAM} ${VERSION}\n"
  printf "Copyright (C) 2010 by Multi-Tech Systems\n"
cat <<EOF
This program is free software; you may redistribute it under the terms of
the GNU General Public License version 2 or (at your option) any later version.
This program has absolutely no warranty.
EOF
}

usage() {

  MTS_IO_DIR="/sys/devices/platform/mts-io"

  local out=1
  if [ $# -eq 1 ]; then
    out=${1}
  fi

  #header
  printf "Usage: ${PROGRAM} [ OPTIONS ] OBJECT [--] [ ARGUMENTS ]\n" >&${out}
  printf "where  OBJECT := {\n" >&${out}
  printf "         show SHOW-NAME |\n" >&${out}
  printf "         store STORE-NAME |\n" >&${out}
  printf "       }\n" >&${out}
  printf "\n" >&${out}

  #items for SHOW
  printf "       SHOW-NAME := {\n" >&${out}
  for f in $MTS_IO_DIR/*;
  do
    FILENAME=${f##*/}
    case $FILENAME in
      modalias )
        ;;
      power )
        ;;
      subsystem )
        ;;
      uevent )
        ;;
      * )
        printf "         $FILENAME\n" >&${out} ;;
    esac
  done
  printf "       }\n" >&${out}
  printf "\n" >&${out}

  #items for STORE
  printf "       STORE-NAME := {\n" >&${out}
  for f in $MTS_IO_DIR/*;
  do
    FILENAME=${f##*/}
    case $FILENAME in
      #mac addresses are read-only
      mac-* )
        ;;
      #product-id, device-id, vendor-id are all read-only
      *-id )
        ;;
      #hw-version is read-only
      hw-version )
        ;;
      #imei is read-only
      imei )
        ;;
      modalias )
        ;;
      power )
        ;;
      subsystem )
        ;;
      uevent )
        ;;
      board-temperature )
        ;;
      extserial-dtr )
        ;;
      adc[0-9] )
        ;;
      din[0-9] )
        ;;
      gpi[0-9] )
        ;;
      gpi[0-9][0-9] )
        ;;
      reset )
        ;;
      led3 )
        ;;
      led-ls )
        ;;
      usbhd-ps-oc )
        ;;
      radio-reset )
        printf "         $FILENAME { 0 }\n" >&${out} ;;
      reset-monitor )
        printf "         $FILENAME { pid short-signal long-signal [extra-long-signal] }\n" >&${out} ;;
      serial-mode )
        printf "         $FILENAME { loopback | rs232 | rs422 | rs485 }\n" >&${out} ;;
      * )
        printf "         $FILENAME BOOLEAN\n" >&${out} ;;
    esac
  done
  printf "       }\n" >&${out}
  printf "\n" >&${out}


  #footer
  printf "       OPTIONS := {\n" >&${out}
  printf "         --verbose\n" >&${out}
  printf "       }\n" >&${out}
  printf "\n" >&${out}
  printf "       BOOLEAN := { OFF | ON }\n" >&${out}
  printf "       OFF := 0\n" >&${out}
  printf "       ON := 1\n" >&${out}
  printf "\n" >&${out}
}

ARGS=$(getopt -o "" --long verbose,version,help -n $0 -- "$@")
if [ $? != 0 ]; then
  usage 2
  exit 1
fi
eval set -- "${ARGS}"

while true; do
  case "$1" in
  --version)
    print_version
    exit 0
    shift
    ;;

  --help)
    usage 1
    exit 0
    shift
    ;;

  --verbose)
    VERBOSE=true
    shift
    ;;
  --)
    shift
    break
  ;;
  *)
    usage 2
    exit 1
  ;;
  esac
done

if [ $# -lt 1 ]; then
  usage 2
  exit 1
fi

cmd=${1}
shift

case "${cmd}" in
show)
  show "$@"
  ;;
store)
  store "$@"
  ;;
show-trigger)
  show_trigger "$@"
  ;;
store-trigger)
  store_trigger "$@"
  ;;
reset-monitor-trap)
  while true; do
    if [ $# -eq 0 ]; then
      break
    fi

    if [ $# -ne 3 ]; then
      usage 2
      exit 1
    fi

    case "${1}" in
    short-cmd)
      RESET_SHORT_CMD=${2}
      RESET_SHORT_SIGNAL=$(kill -l ${3})
      if [ $? -ne 0 ]; then
        exit 1
      fi
      ;;
    long-cmd)
      RESET_LONG_CMD=${2}
      RESET_LONG_SIGNAL=$(kill -l ${3})
      if [ $? -ne 0 ]; then
        exit 1
      fi
      ;;
    *)
      usage 2
      exit 1
      ;;
    esac

    shift; shift; shift
  done

  trap "${RESET_SHORT_CMD}" ${RESET_SHORT_SIGNAL}
  trap "${RESET_LONG_CMD}" ${RESET_LONG_SIGNAL}

  store reset-monitor "$$ ${RESET_SHORT_SIGNAL} ${RESET_LONG_SIGNAL}"

  while true; do
    sleep 1
  done
  ;;
*)
  usage 2
  exit 1
  ;;
esac

exit 0