/*
 * mpegcmd/mpegcmd.c:
 *
 * Copyright (c) 2000 James McKenzie <james@fishsoup.dhs.org>,
 * All rights reserved.
 *
 */

static char rcsid[] = "$Id:$";

/*
 * $Log:$
 */

#include <sys/socket.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/ioctl.h>
#include <signal.h>

#include <endian.h>


#include "hifi.h"
#include "hifimsg.h"
#include "libfiles.h"

int mpegunit;
int cmdunit;

int
main (int argc, char *argv[])
{
  void *msglib;
  Eweid e;
  char *t;
  int cmdunit = getpid () % 255;


  Byte *buf;
  Byte *ptr, *cptr;


  mpegunit = hifi_getunit ("mpeg");

  if (argc != 2)
    {
      fprintf (stderr, "Usage: %s filename|eweid \n", argv[0]);
      exit (1);
    }

  e = filename_to_eweid (argv[1]);

  if (!e)
    {
      e = atoeweid (argv[1]);
    }


  if (!e)
    {
      fprintf (stderr, "Couldn't create Eweid for %s\n", argv[1]);
      exit (1);
    }


  t = eweidtoa (e);
  printf ("%s: %s\n", argv[1], t);
  free (t);

  msglib = hifi_open (HIFI_CLASS_CMD, cmdunit);

  buf = hifi_getbuf ();
  ptr = buf;

  ptr =
    hifipkt_new_rel (ptr, HIFI_CLASS_CMD, cmdunit, HIFI_CLASS_MPEG, mpegunit);

  ptr = hifipkt_ecmd (cptr = ptr, HIFI_CMD_MPEG_PLAY);
  ptr = hifipkt_ebyte (ptr, e->e[0]);
  ptr = hifipkt_ebyte (ptr, e->e[1]);
  ptr = hifipkt_ebyte (ptr, e->e[2]);
  ptr = hifipkt_ebyte (ptr, e->e[3]);
  ptr = hifipkt_ebyte (ptr, e->e[4]);
  ptr = hifipkt_ebyte (ptr, e->e[5]);
  ptr = hifipkt_ebyte (ptr, e->e[6]);
  ptr = hifipkt_ebyte (ptr, e->e[7]);
  hifipkt_elen (cptr, HIFI_PTR_DIFF (ptr, cptr));
  ptr = hifipkt_eeof (ptr);

  hifi_send (msglib, buf, HIFI_PTR_DIFF (ptr, buf));
  hifi_freebuf (buf);


  hifimsg_close (msglib);

  return 0;
}
