/*-=-=-=-=-=-=-=-=-=-=-=- RCCL/RCI Notice of Copyright -=-=-=-=-=-=-=-=-=-=-*
 *									    *
 *	Copyright (C) 1987, 1991 by John E. Lloyd.			    *
 *									    *
 *	Information on copyright permissions, as well as a complete	    *
 *	disclaimer of warranty, is given in the file COPYRIGHT.		    *
 *									    *
 *-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/

/* program to make create a binary JLS file */

#if !defined(VXWORKS_CFG)

#include <stdio.h>

#include "../h/robot_jls.h"
#include "../h/cdefs.h"

main(argc, argv)
int argc;
char *argv[];
{
	JLS jls;
	FILE *fi, *fo;
	char *infile = NULL;
	char *outfile = NULL;

	int i, checksum;
	int *p;

	int verbose = 0;

	argc--; argv++;

	while (argc--)
	 { if (ARGUMENT ("-v"))
	    { verbose = 1;
	    }
	   else if (!infile)
	    { infile = *argv;
	      if ((fi = fopen (infile, "r")) == NULL)
	       { fatalMsg (-1, "cannot open input file %s\n", infile);
	       }
	    }
	   else if (!outfile)
	    { outfile = *argv;
	      if ((fo = fopen (outfile, "w")) == NULL)
	       { fatalMsg (-1, "cannot open output file %s\n", outfile);
	       }
	    }
	   else
	    { 
usage:
	      usageMsg (-1, "makejls [-v] <input_file> <output_file>\n");
	    }
	   argv++;
	 }
	if (!infile || !outfile)
	 { goto usage;
	 }

	if (readJls (&jls, fi) < 0)
	 { printErrors();
	   unlink (outfile);
	   exit (-1);
	 }

	checksum = 0;
	p = (int*)&jls;
	for (i=0; i<sizeof(JLS)/sizeof(int); i++)
	 { checksum += p[i];
	 }

	if ((fwrite (&jls, sizeof(JLS), 1, fo) == 0) ||
	    (fwrite (&checksum, sizeof(int), 1, fo) == 0) )
	 { unlink (outfile);
	   fatalMsg (-1, "cannot write to output file %s\n", outfile);
	 }

	if (verbose)
	 { printJls (&jls);
	 }
	exit (0);
}

#endif
