/*
 * PVVMUD a 3D MUD
 * Copyright (C) 1998-1999  Programvareverkstedet (pvv@pvv.org)
 *
 * 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.
 *
 * This program 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 program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */
#include "globals.h"
#include "parse.h"

FILE * source;
FILE * outfile;
FILE * ndbFile;

TreeNode_t * parseTree;

int main(int argc, char * argv[]){
  char geofile[256];
  char bogfile[256];
  char ndbfile[256];

  if (argc != 2) {
    fprintf(stderr,"usage: %s <filename>\n",argv[0]);
    exit(1);
  }

  strcpy(geofile,argv[1]);
  if (strrchr (geofile, '/') != NULL){
    if (strchr (strrchr(geofile,'/'), '.') == NULL)
      strcat(geofile,".geo");
  } else if (strchr (geofile, '.') == NULL)
    strcat(geofile,".geo");

  source = fopen(geofile,"r");
  if (source == NULL){
    fprintf(stderr,"File %s not found\n",geofile);
    exit(1);
  }

  if (strrchr(geofile,'/') != NULL){
    strcpy(bogfile,strrchr(geofile,'/')+1);
  } else strcpy(bogfile,geofile);

  strcpy(strchr(bogfile,'.'),".bog");

  outfile = fopen(bogfile,"w");

  if (outfile == NULL){
    fprintf(stderr,"Error: Failed to open output file %s\n",bogfile);
    exit(1);
  }

  strcpy(ndbfile,bogfile);
  strcpy(strchr(ndbfile,'.'),".ndb");


  parseTree = parse();

  fclose(source);

  ndbFile = fopen(ndbfile,"w");

  if (ndbFile == NULL){
    fprintf(stderr,"Error: Failed to open output file %s\n",ndbfile);
    exit(1);
  }


  writebogfile(parseTree,outfile,ndbFile);

  fclose(outfile);
  fclose(ndbFile);

/* printTree(parseTree); */

  return 0;
}