/*
 * Author:      Pierangelo Masarati
 * E-mail:      masarati@aero.polimi.it
 * Date:        August, 1999
 * Version:     1.0
 * Description:
 *              LDAP client to build a BibTeX database out of
 *              a Directory Server, with migration tools.
 *
 * Redistribution and use in source and binary forms are permitted
 * provided that the above copyright notice and this paragraph are
 * duplicated in all such forms and that any documentation,
 * advertising materials, and other materials related to such
 * distribution and use acknowledge that the software was developed
 * by Pierangelo Masarati.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 */

#include <stdlib.h>
#include <stdio.h>
#include <malloc.h>
#include <string.h>
#include <getopt.h>

#include "bibtex2ldif.h"
extern int yydebug;
char *base;		/* LDAP base for the BibTeX entries */
char *bibtex_oc;	/* BibTeX objectClass */
char *dn_form;		/* form to printf the dn of the entries */

FILE *fd_out;

static void
bibtex2ldif_usage(FILE *fd, char *optstring)
{
	fprintf(fd, 
		"\n"
		"bibtex2ldif:\tcreates an LDIF(5) file"
		" from a BibTeX(1) database to generate\n"
		"\t\ta gdbm(3) database for slapd(8) LDAP(3) server\n"
		"\t\twith ldif2ldbm(8).\n usage:\n\n");
	fprintf(fd, "\tbibtex2ldif [%s]\n\n", optstring);
	fprintf(fd, "  -b base\tbase dn for the search"
		" of bibliographic entries (default: \"\")\n");
	fprintf(fd, "  -f bibtex_oc\tBibTeX objectclass"
		" for the search filter (default: bibtex)\n");
	fprintf(fd, "  -i in-file\tBibTeX database"
		" input file name (default: stdin)\n");   
	fprintf(fd, "  -o out-file\tLDIF output file name"
		" (default: stdout)\n");
	fprintf(fd, "  -?\t\tthis message\n");
	fprintf(fd, "\n");
}

int
main(int argn, char *argv[])
{
	char *optstring = "b:f:i:o:?:d";
	char *in_file_name = NULL;
	FILE *fd_in;
	char *out_file_name = NULL;
	int rc;

	
	base = strdup("");
	bibtex_oc = strdup("bibtexEntry");
	
	while (1) {
		rc = getopt(argn, argv, optstring);
		if (rc == EOF) {
			break;
		}
		
		switch (rc) {
		case 'b':
			if (base != NULL) {
				free(base);
			}
			base = strdup(optarg);
			break;
		
		case 'f':
			if (bibtex_oc != NULL) {
				free(bibtex_oc);
			}
			bibtex_oc = strdup(optarg);
			break;
		
		case 'i':
			if (in_file_name != NULL) {
				free(in_file_name);
			}
			in_file_name = strdup(optarg);
			break;
		
		case 'o':
			if (out_file_name != NULL) {
				free(out_file_name);
			}
			out_file_name = strdup(optarg);
			break;
		case '?':
			bibtex2ldif_usage(stderr, optstring);
			_exit(EXIT_SUCCESS);
		case 'd':
		  yydebug = 1;
		  break;
		default:
			break;
		
		}
	}
	
	if (base != NULL && base[0] != '\0') {
		int len = strlen(base)+sizeof("dn: cn=%s,\n")+1;
		dn_form = (char *)malloc(len);
		snprintf(dn_form, len, "dn: cn=%%s,%s\n", base);
	} else {
		dn_form = strdup("dn: cn=%s\n");
	}
	
	if (in_file_name != NULL) {
		fd_in = fopen(in_file_name, "r");
		if (fd_in == NULL) {
			fprintf(stderr, "unable to open input file \"%s\"\n",
				in_file_name);
			_exit(EXIT_FAILURE);
		}
	} else {
		fd_in = stdin;
	}
	
	if (out_file_name != NULL) {
		fd_out = fopen(out_file_name, "w");
		if (fd_out == NULL) {
			fprintf(stderr, "unable to open output file \"%s\"\n",
				out_file_name);
			_exit(EXIT_FAILURE);
		}
	} else {
		fd_out = stdout;
	}
	
	yyin = fd_in;
	yyparse();
	
	return 0;
}





