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

static char rcsid[] =
    "$Id: oggwrap.c,v 1.2 2002/05/12 11:19:56 root Exp root $";

/*
 * $Log: oggwrap.c,v $
 * Revision 1.2  2002/05/12 11:19:56  root
 * #
 *
 * Revision 1.1  2002/05/12 11:19:30  root
 * #
 *
 * Revision 1.9  2001/10/12 13:51:19  root
 * #
 *
 * Revision 1.8  2001/01/30 06:16:33  root
 * #
 *
 * Revision 1.7  2000/12/01 04:42:23  root
 * #
 *
 * Revision 1.6  2000/12/01 04:13:55  root
 * #
 *
 * Revision 1.5  2000/11/23 23:45:30  root
 * #
 *
 * Revision 1.4  2000/10/07 18:30:11  root
 * #
 *
 * Revision 1.3  2000/10/07 16:41:53  root
 * #
 *
 * Revision 1.2  2000/10/06 19:52:46  root
 * #
 *
 * Revision 1.1  2000/10/06 06:21:36  root
 * #
 *
 */

#include <sys/types.h>
#include <sys/time.h>
#include <string.h>
#include <strings.h>
#include <stdlib.h>
#include <stdio.h>
#include <signal.h>
#include <fcntl.h>

#include <unistd.h>

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


int main(int argc, char *argv[])
{
  void *msglib;
  FILE *f;
  char buf[1024], *ptr = buf;

  int Y, M, D, h, m, s, S;

  int cmdunit = getpid() % 255;
  int oggunit;

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

 {
 int fd=open("/dev/null",O_WRONLY);
 dup2(fd,1);
 dup2(fd,2);
 }
 

  buf[0] = 0;

  f = fopen(argv[1], "r");
  fread(buf, 1, sizeof(buf), f);
  fclose(f);

  buf[sizeof(buf) - 1] = 0;

  if (!(ptr=index(ptr,'?'))) ptr=buf;

  while ((*ptr) && (!isdigit(*ptr)))
    ptr++;
  Y = strtol(ptr, &ptr, 10);
  while ((*ptr) && (!isdigit(*ptr)))
    ptr++;
  M = strtol(ptr, &ptr, 10);
  while ((*ptr) && (!isdigit(*ptr)))
    ptr++;
  D = strtol(ptr, &ptr, 10);
  while ((*ptr) && (!isdigit(*ptr)))
    ptr++;
  h = strtol(ptr, &ptr, 10);
  while ((*ptr) && (!isdigit(*ptr)))
    ptr++;
  m = strtol(ptr, &ptr, 10);
  s = 0;
  S = 0;
  oggunit = hifi_getunit("ogg");
  msglib = hifi_open(HIFI_CLASS_CMD, cmdunit); 

  {
    Byte *cptr, *ptr, *buf;
    buf = ptr = hifi_getbuf();
    ptr =
        hifipkt_new_rel(ptr, HIFI_CLASS_CMD,
                        cmdunit, HIFI_CLASS_OGG, oggunit);
    ptr = hifipkt_ecmd(cptr = ptr, HIFI_CMD_OGG_PLAY);
    ptr = hifipkt_ebyte(ptr, S);
    ptr = hifipkt_eword(ptr, Y);
    ptr = hifipkt_ebyte(ptr, M);
    ptr = hifipkt_ebyte(ptr, D);
    ptr = hifipkt_ebyte(ptr, h);
    ptr = hifipkt_ebyte(ptr, m);
    ptr = hifipkt_ebyte(ptr, s);
    hifipkt_elen(cptr, HIFI_PTR_DIFF(ptr, cptr));
    ptr = hifipkt_eeof(ptr);
    hifi_send(msglib, buf, HIFI_PTR_DIFF(ptr, buf));
  }

  return 0;
}
