<?php
#
# Format a particular N2K command
#
# Example: this will request all product information
#
# 1970-01-01-00:00:00.000,6,59904,0,255,3,14,f0,01
#
#
# (C) 2009-2026, Kees Verruijt, Harlingen, The Netherlands.
#
# This file is part of CANboat.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

  require_once('format-message.inc');
  require_once('canboat-default.inc');

  $format = getCanboatDefault('WRITE_FORMAT', 'CANBOAT');

  function streaming()
  {
    global $format;

    while ($f = fgets(STDIN))
    {
      $match = preg_split('/,/', trim($f), -1, PREG_SPLIT_NO_EMPTY);

      $command = $match[0];
      $dst = 255;

      $p = new FormatMessage($command);
      $p->setFormat($format);
      $args = $p->getArgs();

      $n = 1;
      foreach ($args as $arg)
      {
        if (!isset($match[$n]))
        {
          echo "Missing argument $arg on line\n";
          echo "Usage: $command ".join($args, ' ')."\n";
          exit(1);
        }
        $p->setArg($arg, $match[$n]);
        $n++;
      }
      echo $p->formatMessage($dst);
    }
  }

  function usage()
  {
    global $format_n2k_messages;

    $p = new FormatMessage('request_pgn'); // fill format_n2k_messages

    echo "Usage: format-message [--format YDWG|CANBOAT] <dst> <command>\n";
    echo "  <dst> := destination address 0..254 or 255 for broadcast\n";
    echo "  <command> := ...\n";
    foreach ($format_n2k_messages as $key => $value)
    {
      printf("    %-15s %-20s %s\n", $key, $value['args'], $value['description']);
    }
    exit(1);
  }

  if (isset($argc))
  {
    if ($argc < 2)
    {
      usage();
    }
    if ($argv[1] == "--stdin")
    {
      streaming();
      exit(0);
    }
    if ($argv[1] == "--format")
    {
      $format = $argv[2];
      $dst = $argv[3];
      $command = $argv[4];
      $n = 5;
    }
    else
    {
      $dst = $argv[1];
      $command = $argv[2];
      $n = 3;
    }
  }
  else
  {
    if (!array_key_exists('command', $_REQUEST) || !array_key_exists('dst', $_REQUEST))
    {
      usage();
    }
    $dst = $_REQUEST['dst'];
    $command = $_REQUEST['command'];
    if (array_key_exists('format', $_REQUEST))
    {
      $format = $_REQUEST['format'];
    }
  }

  $p = new FormatMessage($command);
  $p->setFormat($format);
  $args = $p->getArgs();

  if (isset($argc))
  {
    foreach ($args as $arg)
    {
      if (!isset($argv[$n]))
      {
        echo "Missing argument $arg on commandline\n";
        echo "Usage: $argv[0] $command ".join($args, ' ')."\n";
        exit(1);
      }
      $p->setArg($arg, $argv[$n]);
      $n++;
    }
  }
  else
  {
    foreach ($args as $arg)
    {
      if (!isset($_REQUEST[$arg]))
      {
        echo "Missing argument $arg in request\n";
        exit(1);
      }
      $p->setArg($arg, $argv[$n]);
    }
  }

  echo $p->formatMessage($dst);
?>
