#include <stdio.h>
#define NULL 0

char *pgm;
char *readhdf();

main(argc,argv)
     int argc;
     char *argv[];
{
  int palsize,x,y,imagenumber,palettenumber,argindex;
  char pal[768];
  char *image;
  char *imagefile, *palettefile, *outfile;
  char *tmpstr;
  
  /* init for those systems who don't do these things cleanly */
  imagenumber = 0;
  palettenumber = 0;
  palettefile = NULL;
  imagefile = NULL;
  outfile = NULL;

  /* parse command line */
  pgm = argv[0];
  argindex = 1;
  while (argindex < argc)
    {
      if (argv[argindex][0] == '-')
	{
	  switch (argv[argindex][1])
	    {
	    case 'i':
	      argindex++;
	      imagefile = argv[argindex];
	      break;
	    case 'o':
	      argindex++;
	      outfile = argv[argindex];
	      break;
	    case 'n':		/* use image number */
	      argindex++;
	      tmpstr = argv[argindex];
	      imagenumber = atoi(tmpstr);
	      break;
	    case 'p':		/* use palette number */
	      argindex++;
	      tmpstr = argv[argindex];
	      palettenumber = atoi(tmpstr);
	      break;
	    case 'f':		/* use palette file */
	      argindex++;
	      palettefile = argv[argindex];
	    }
	}
      else
	{
	  usage();
	}
        argindex++;
    }      
  if (imagefile == NULL)
    usage();
  if (outfile == NULL)
    outfile = "stdout";

/* do it to it */
  image = readhdf(imagefile,imagenumber,
		  &x,&y,
		  pal,&palsize,palettenumber,palettefile);
  writexwd(outfile,image,x,y,pal,palsize);
  exit(0);
}

usage()
{
  fprintf(stderr,"Usage:\n");
  fprintf(stderr,"%s -i imagefile [-f palettefile] [-p palettenumber]\n",pgm);
  fprintf(stderr,"                      [-n imagenumber] [-o outputfile]\n");
  fprintf(stderr,"        outputfile, if not present, will default to stdout\n");
  fprintf(stderr,"        imagefile argument MUST be present\n");
  fprintf(stderr,"\n");
  fprintf(stderr,"\n");
  fprintf(stderr,"\n");
  exit(-1);
}
