/* drawhack
 * (c) Tony Howat, 1997
 * xargle@eh.org or http://www.xargle.demon.co.uk/2020
 *
 * drawhack does its best to produce some sort of sensible textual output
 * from a drawfile in it own special way, dissecting each object as it
 * goes.
 *
 * drawhack may be freely distributed, edited, whatever, as long as I get
 * some of the hugs if people find it useful
 */

#include <stdio.h>
#include <string.h>
#include <time.h>
#include <stdlib.h>
#include "kernel.h"

#define DRAW 0x77617244

/* see OS3 PRM 4-458 */
typedef struct 
{
  char title[4];  
  int majorver;     /* Major part of version number */
  int minorver;     /* Minor part of version number */
  char creator[12]; /* String ID of program which created file */
  int xmin;         /* file bounding box */
  int ymin;         /* file bounding box */
  int xmax;         /* file bounding box */
  int ymax;         /* file bounding box */
} draw_file_header;

typedef struct 
{
  int tag;          /* 1 word  */
  int size;         /* 1 word  */
  int xmin;         /* object bounding box */
  int ymin;         /* object bounding box */
  int xmax;         /* object bounding box */
  int ymax;         /* object bounding box */
} draw_object_header;

typedef struct 			/* Library object */
{
  char name[21];			/* Object name */
  time_t time;			/* Time last updated */
} draw_libstrhdr;

#define Osfile_Readinfo  17

int file_size(char *file)
{
  _kernel_osfile_block block;

  if (_kernel_osfile(Osfile_Readinfo, file, &block) == _kernel_ERROR)
    return -1;
  else
    return (block.start);
}

int drawhack(char *file)
{
  FILE *in;
  int length;
  int *data;
  int off;
  int objn=0;
  draw_object_header *oh;
  int *dat;

  if((in=fopen(file,"rb+"))==NULL)
  {
    printf("Couldn't open %s.\n",file);
    return 1;
  }

  length=file_size(file);

  if((data=(int *)malloc(length))==NULL)
  {
    printf("Couldn't allocate memory");
    return 1;
  }

  if(1!=fread(data,length,1,in))
    printf("erk");

  printf("%x\n",(int)*data);

  dat=data;
  while(*dat!=6)
  {
    ++dat;
  }
  printf("0x6 occurs at offset %i [%x,%x]\n",(int)dat-(int)data,*(dat+4),*(dat+8));

  off = sizeof(draw_file_header);
  while(off < length) {
    printf("%i\n",data);
    oh = (draw_object_header *)((int)data+(int)off);
    printf("%i\n",oh);
    printf("%x\n",(int)*data);
    printf("off=%i, oh->tag = %i, oh->length = %i\n",off,oh->tag,oh->size);

    if(oh->tag==100)
    {
      draw_object_header *ohl;
      FILE *out;
      char outname[20];
      draw_file_header dfh={"Draw",0xc9,0,"DLibrary",0,0,0,0};

      ohl=oh;//+sizeof(draw_libstrhdr);
      sprintf(outname,"$.out%i",objn);
      out=fopen(outname,"wb+");
      objn++;
      dfh.xmin=ohl->xmin; dfh.xmax=ohl->xmax;
      dfh.ymin=ohl->ymin; dfh.ymax=ohl->ymax;
      fwrite(&dfh,1,sizeof(draw_file_header),out);
      printf("off:%i\n",(int)ohl-(int)data);
      ohl=(draw_object_header *)data+92;
      fwrite(ohl,1,oh->size-sizeof(draw_file_header)-12,out);
      fclose(out);
      
    }

    off += oh->size;
  }



  fclose(in);
  return 0;
}

int main(void)
{
  printf("Drawhack V0.00, (c) Tony Howat 1997.\n"
         "Compiled " __DATE__"\n\n");

  return drawhack("<DLibrary$Dir>.tests.2");
}
