/* uid_object.c : example file for 6.837 F97 PS3A */
#include <stdio.h>
#include <pwd.h>
#include <sys/types.h>
#include <unistd.h>
#include <time.h>
#include "arg_parse.h"

/* common to all programs */
int  debug, helponly;
char *defname = NULL;

void
emit_help( void )
{
  extern char helpmsg[];  /* defined below */
  fprintf (stderr, helpmsg);
}

/* output legal inventor file header, with */
/* timestamp, creator id, and command line */
void
emit_header( int argc,
			 char *argv[])
{
struct passwd *pwent = NULL;
time_t cl;

  /* output inventor header */
  printf ("#Inventor V2.1 ascii\n");
  /* output date/timestamp */
  cl = time(NULL);
  printf ("# %s", asctime (localtime(&cl)));
  /* output creator uid */
  pwent = getpwuid(getuid());
  printf ("# Created by %s using\n#", pwent->pw_name );
  /* echo command line */
  while (argc--) printf ( " %s", *argv++ ); printf ("\n");
}

/* YOU SHOULD NOT NEED TO EDIT ANYTHING ABOVE */
/* THIS LINE, BUT YOU MAY DO SO IF YOU WISH. */

/* extended help message */
char helpmsg[] = "\
Usage: uid_object [options] ('-' prints all options; -help prints this msg)\n\
This program generates a file in SGI Inventor 2.1 format\n\
which represents an object from a family of my object type.\n\
This family of objects is parametrized by several attributes:\n\
aspect ratio, etc., etc.\n";

/* parameters specific to object you are defining */
int number = 4;
float thickness = 0.025, depth = 0.25, width = 0.8;
float redcomp = 0.4, greencomp = 0.5, bluecomp = 0.6;

int main(
  int argc,
  char *argv[])
{
  /* parse command line arguments */
  extern int arg_parse(int ac, char **av, ...);
  if (arg_parse(argc, argv,
    "-help",              ARG_FLAG(&helponly),
                          "output an extensive help message and quit",
    "-debug",             ARG_FLAG(&debug),
                          "set debug flag",
    "-number %d",         &number,
				          "set number of shelves",
    "-thickness %f",      &thickness,
				          "set shelf thickness",
    "-depth %f",          &depth,
				          "set shelf depth (height = 1 unit)",
    "-width %f",          &width,
				          "set shelf width (height = 1 unit)",
    "-color %f %f %f",    &redcomp, &greencomp, &bluecomp,
                          "shelf rgb components (0.0 .. 1.0)",
    "-defname %S",        &defname,
                          "inventor definition name",
    0) < 0) exit(1);

  /* give help if requested */
  if ( helponly ) { emit_help(); exit(0); }

  /* emit inventor header */
  emit_header( argc, argv );

  /* FILL IN CODE WHICH USES EFFECTIVE STATE */
  /* TO GENERATE PARAMETRIZED INVENTOR MODEL */

  /* for now, we just echo all program state */
  fprintf (stderr, "\tdebug is %d\n", debug);  
  fprintf (stderr, "\tnumber is %d\n", number);
  fprintf (stderr, "\tthickness is %f\n", thickness);
  fprintf (stderr, "\tdepth is %f\n", depth);
  fprintf (stderr, "\twidth is %f\n", width);
  fprintf (stderr, "\tcolor is %f %f %f\n", redcomp, greencomp, bluecomp);
  if (defname == NULL) defname = "null";
  fprintf (stderr, "\tdefname is '%s'\n", defname);

  /* return OK status */
  return 0;
}
