/*
 * This file is part of "VirtuaMUnstaz Demo Distribution".
 *
 * This is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * "VirtuaMUnstaz Demo Distribution" is distributed in the hope that it will be 
 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this software; see the file COPYING.  If not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

/* $Id: vmu.c,v 2.6 2000/03/03 11:33:29 leo Exp $ */

#include <stdio.h>

#if !defined (sparc) && !defined(__sparc)
  #include <getopt.h>
#endif 

#include "vmi.h"

/**
 * display a help message with possible options and syntax
 */
void help(char *prog) {
  printf("usage: %s [options] [file]\n\n", prog);
  printf("to view info contained in a VMI file:\n");
  printf("  -i 		view information contained in vmi or vms file\n");
  printf("  -p command	use command as converter for VMS icons\n");
  printf("\n");
  printf("to create a new vmi file from an VMS file:\n");
  printf("  -r name	the file name of the resource (8 chars)\n");
  printf("  -f name	the file name as seen on the VMU (12 chars)\n");
  printf("  -d text	description of the file (32 chars)\n");
  printf("  -c text	author and copyright (32 chars)\n"); 
  printf("  -s		create SEGA checksum\n"); 
  printf("  -a		create AGES checksum\n"); 
  printf("  -g		set file type to GAME\n"); 
  printf("\n");
  printf("  -v		display software version\n");
  printf("  -h		this help text\n");
  printf("\n");
  printf("please read the documentation to learn more about the options.\n");
}

/**
 * main: start function and action selection according to the arguments
 */
int main(int argc, char **argv) {
  int info = 0, c, index;
  int checksum = 0, type = 0x00, script = 0;
  char *fname = NULL, *description = NULL, *copyright = NULL;
  char *resource = NULL, *vmufname = NULL, *command = NULL;

  /* print copyright information */
  printf("vmu data creator (c) 1999,2000 by VirtuaMUnstaz\n");
  printf("check out other VMU applications: http://VirtuaMUnstaz.de/\n");

  /* check arguments and select appropriate action */
  while(1) {
    c = getopt(argc, argv, "ip:r:f:d:c:sagtvh");
    if(c == -1) break;
    switch(c) {
      case 'i': /* display vmi/vms information */
	info = 1;
	break;
      case 'p': /* get the command for icon conversion */
	command = optarg;
	break;
      case 'r': /* ressource name */
	resource = optarg;
	break;
      case 'f': /* file name on the vmu */
	vmufname = optarg;
	break;
      case 'd': /* vms description string */
	description = optarg;
	break;
      case 'c': /* vms copyright string */
	copyright = optarg;
	break;
      case 's': /* SEGA checksum */
	checksum |= SEGA;
	break;
      case 'a': /* AGES checksum */
	checksum |= AGES;
	break;
      case 'g': /* type is GAME */
	type = 0x03;
	break;
      case 't': /* script hack */
	script = 1;
	break;
      case 'v': /* print revision of sub modules */
	printf("  $Id: vmu.c,v 2.6 2000/03/03 11:33:29 leo Exp $\n  %s\n  %s\n",
	       vmi_revision(), vms_revision());
	exit(0);
      case 'h': /* display help */
	help(argv[0]);
	exit(0);
      default:
	printf("unknown option: %c\n", c);
    }
  }

  /* the rest of the arguments must be non-options, thus files */
  if(optind < argc) {
    fname = argv[optind++];
    if(info)
      if(!strncasecmp(fname+strlen(fname)-4, ".vmi", 4))
	  vmi_print(fname, command);
      else
        vms_print(fname, command);
    else
      vmi_create(fname, 
	         resource, vmufname, description, copyright, checksum, type,
	         command, script);
    exit(0);
  }

  /* if the user forgot the file, display help and exit with an error */
  help(argv[0]);
  exit(-1);
}

