#include "krcd.h"
#include <locale.h>

int main(int argc, char **argv) {
    KRCD *krcd;
    int inlineStyle=1;
    char *pt;
    char *outputname = NULL;
    char *genname = NULL;
    int i;

    setlocale(LC_ALL,"en_US.UTF-8");
    for ( i=1 ; i<argc; ++i ) {
	if ( *argv[i]=='-' || *argv[i]=='+' ) {
	    char *pt = argv[i]+1;
	    if ( *pt=='-' )
		++pt;
	    if ( (strcmp(pt,"output")==0 || strcmp(pt,"o")==0 ) && i+1<argc )
		outputname = argv[++i];
	    else if ( strcmp(pt,"inline")==0 || strcmp(pt,"inline-styles")==0 ) {
		inlineStyle = ( *argv[i]=='+' );
	    } else {
		fprintf( stderr, "Unknown argument: %s\n", argv[i] );
		fprintf( stderr, "Usage %s [-/+inline] [-o outputname] filenames\n", argv[0] );
exit( 1 );
	    }
    continue;
	}
	krcd = krcdLoadFile(argv[i]);
	if ( krcd==NULL ) {
	    fprintf( stderr, "Failed to load %s.\n", argv[i] );
exit(1);
	}
	genname = NULL;
	if ( outputname==NULL ) {
	    genname = malloc(strlen(argv[i])+20);
	    strcpy(genname,argv[i]);
	    pt = strrchr(genname,'.');
	    if ( pt==NULL )
		strcat(genname,".svg");
	    else
		strcpy(pt,".svg");
	    outputname = genname;
	}
	krcdLayout(krcd);
	krcd2Svg(krcd,NULL,outputname,0,inlineStyle);
	outputname = NULL;
	free(genname);
    }

    krcdLibrarySaveOnExit();
return(0);
}
