/***************************************************************************
                          ihxconv.c 

Intel Hex to emulator format (lolo's emulator and POEMS) conversion for
Sharp pocket computers

                             -------------------
    begin                : 2004.05.07
    copyright            : (C) 2004 by Edgar Puehringer
    email                : edgar_pue@yahoo.com
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program 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 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include "ihxconv.h"

#include <string.h>
#include <stdlib.h>

void usage() {
  fprintf(stderr, "usage: ihx2emu [-m 1245|1250|1251|1255|1260|1261|1262|1350] [-p romfile] [-o emufile] [ihxfile]\n");
  exit(-1);
}


void duplicate(char * param) {
  fprintf(stderr, "duplicate parameter '%s'\n", param);
  usage();
}


void unknown(char * param) {
  fprintf(stderr, "unknown parameter '%s'\n", param);
  usage();
}


void missing() {
  fprintf(stderr, "missing file name for emulator file");
  usage();
}


void dupfile(char * param) {
  fprintf(stderr, "duplicate input file '%s'\n", param);
  usage();
}


void wrongmodel(char * param) {
  fprintf(stderr, "unknown model '%s'\n", param);
  usage();
}


int main(int argc, char * argv[]) {
  int mode       = MODE_LOLO;
  char * emuname = NULL;
  char * srcname = NULL;
  char * romname = NULL;
  FILE * emufile;
  FILE * romfile = NULL;
  FILE * srcfile; 
  int i     = 1;
  int model = MODEL_1350;

  while (i < argc) {
    if (strcmp(argv[i], "-o") == 0) {
      if (emuname) duplicate(argv[i]);
      if (i == (argc - 1)) missing();
      emuname = argv[++i];
    } else if (strcmp(argv[i], "-p") == 0) {
      if (romname) duplicate(argv[i]);
      if (i == (argc - 1)) missing();
      romname = argv[++i];
      mode    = MODE_POEM;
    } else if (strcmp(argv[i], "-m") == 0) {
      if (i == (argc - 1)) missing();
      i++;
      if (strcmp(argv[i], "1245") == 0) { 
        model = MODEL_1245; 
      } else if (strcmp(argv[i], "1250") == 0) {
        model = MODEL_1250;
      } else if (strcmp(argv[i], "1251") == 0) {
        model = MODEL_1251;
      } else if (strcmp(argv[i], "1255") == 0) {
        model = MODEL_1255;
      } else if (strcmp(argv[i], "1260") == 0) {
        model = MODEL_1260;
      } else if (strcmp(argv[i], "1261") == 0) {
        model = MODEL_1261;
      } else if (strcmp(argv[i], "1262") == 0) {
        model = MODEL_1262;
      } else if (strcmp(argv[i], "1350") == 0) {
        model = MODEL_1350; 
      } else {
        wrongmodel(argv[i]);
      }
    } else if (strncmp(argv[i], "-", 1) == 0) {
      unknown(argv[i]);
    } else {
      if (srcname) dupfile(argv[i]);
      srcname = argv[i];
    }
    i++;
  }
  if (emuname) emufile = fopen(emuname, "w");
    else emufile = stdout;
  if (!emufile) {
    perror("ihx2emu");
    exit(-2);
  }
  if (srcname) {
    srcfile = fopen(srcname, "r");
    if (!srcfile) {
      perror("ihx2emu");
      exit(-3);
    }
  }
  else {
    srcfile = stdin;
  }
  if (romname) {
    romfile = fopen(romname, "r");
    if (!romfile) {
      perror("ihx2emu");
      exit(-4);
    }
  }

  i = ihxconv(srcfile, emufile, mode, romfile, model); 

  if (srcname) fclose(srcfile);  
  if (emufile != stdout) fclose(emufile);
  if (romfile) fclose(romfile);
  exit(i);
}
 
