/*
 * mtd_file.y:
 *
 * Copyright (c) 2000 James McKenzie <james@fishsoup.dhs.org>,
 * All rights reserved.
 *
 */

%{
static char rcsid[] = "$Id: mtd_file.y,v 1.8 2001/10/12 13:51:11 root Exp $";

/*
 * $Log: mtd_file.y,v $
 * Revision 1.8  2001/10/12 13:51:11  root
 * #
 *
 * Revision 1.7  2001/10/04 18:21:16  root
 * #
 *
 * Revision 1.6  2001/10/04 18:00:36  root
 * #
 *
 * Revision 1.5  2000/10/07 18:30:03  root
 * #
 *
 * Revision 1.4  2000/09/24 01:12:43  root
 * #
 *
 * Revision 1.3  2000/09/19 05:06:22  root
 * #
 *
 * Revision 1.2  2000/09/19 03:53:27  root
 * #
 *
 * Revision 1.1  2000/09/13 15:13:40  root
 * #
 *
 */


#include "project.h"
#include <ctype.h>


static void mtd_make_group(char *filename,Flock el);

// state machine stuff

typedef enum {START, BRACE_IN,BRACE_OUT,GROUP,TEXT,URI,AFILE,WORK,NOWORK} THING ;
typedef enum {INIT, FILE_LIST } STATE ;

extern int mtd_read(char *buf,int len);



%}

%token BRACE_IN BRACE_OUT STRING TEXT URI GROUP EWEID EWEID_VAL WORK NOWORK

%union {
		char string[1024];
		struct Eweid_struct eweid;
	}

%start decl_list

%%

decl_list:	decl 
	|	decl_list decl 
	;

decl:		GROUP STRING    file_list  ';'			{ do_stuff(GROUP,     $2.string, NULL); }
	|	TEXT  STRING    STRING	';'			{ do_stuff(TEXT,      $2.string, $3.string ); }	
	|	URI   STRING    STRING	';'			{ do_stuff(URI,       $2.string, $3.string ); }	
	|	WORK   STRING    ';'			{ do_stuff(WORK,       $2.string,NULL ); }	
	|	NOWORK   STRING    ';'			{ do_stuff(NOWORK,       $2.string, NULL ); }	
	|	EWEID EWEID_VAL STRING STRING STRING STRING ';' 	{ do_otherstuff (eweid_dup(&$2.eweid),$3.string,$4.string,$5.string,$6.string); }
	|	';'
        ;

file_list:	BRACE_IN file_list_bit BRACE_OUT 
        ;

file_list_bit:	file			
	|	file_list_bit file			
        ;

file:	STRING							{ do_stuff(AFILE,     $1.string, NULL	   ); }
        ;


%%

extern int line;
extern char *yytext;
extern char *mtd_filename;

int state=INIT;


void yyerror(char *s)
{
	fprintf(stderr, "mtd_file: %s line %d: %s near `%s'\n", mtd_filename,line, s, yytext);
}

Flock el=NULL;
 
#define THROW_ERROR(a)	do { fprintf(stderr, "mtd_file: %s line %d: %s near `%s'\n", mtd_filename,line, (a), c1); fprintf(stderr,"last error was FATAL: database is incomplete\n"); exit(1); } while(0)

void do_otherstuff(Eweid e,char *file,char*text,char*uri,char *num)
{
  int i=atoi(num);

  inode_add(e,file);

  if (strlen(text)) {
    text_add(e,text);
  } else {
    char *foo=text_guess_name_from_file(file);
    text_add(e,foo); 
    free(foo);
  }

  if (strlen(uri)) {
    uri_add(e,uri);
  }

  if (!i) work_remove(e);
    else work_add(e);

  Obj_Delete(e);


}

void do_stuff(THING thing, char *c1, char *c2)
{
Eweid e;
char *f;

switch(thing) {
case START:
	state=INIT;
	break;
case WORK:
case NOWORK:
        f=de_relativise(c1,mtd_filename);
	e=filename_to_eweid(f);
	free(f);

        if (EWEID_IS_NONE(e)) {
		fprintf(stderr,"%s: %s doesn't exist\n",mtd_filename,c1);
		return;
	} else {
		switch(thing) {
		case WORK:
			mtd_add_work(e);
			break;
		case NOWORK:
			mtd_remove_work(e);
			break;
		default:
		;
		}
	}
	Obj_Delete(e);
	
break;
case TEXT:
	f=de_relativise(c1,mtd_filename);
	e=filename_to_eweid(f);
	free(f);

        if (EWEID_IS_NONE(e)) {
		fprintf(stderr,"%s: %s doesn't exist\n",mtd_filename,c1);
		return;
	} else {
	mtd_add_text(e,c2);
        mtd_add_work(e);
	}
	Obj_Delete(e);
	
	break;
case URI:
	f=de_relativise(c1,mtd_filename);
	e=filename_to_eweid(f);
	free(f);

//        mtd_remove_text(e);
        mtd_add_text(e," ");

        if (EWEID_IS_NONE(e)) {
		fprintf(stderr,"%s: %s doesn't exist\n",mtd_filename,c1);
		return;
	} else {
/*FIXME: are these files or uris?*/
	f=de_relativise(c2,mtd_filename);
	mtd_add_uri(e,f);
        free(f);
	}
	Obj_Delete(e);
	break;
case GROUP:
	if (state==FILE_LIST) THROW_ERROR("Error 1");
	state=INIT;
	f=de_relativise(c1,mtd_filename);
	mtd_make_group(f,el);
	free(f);
	Obj_Delete(el);
	el=NULL;
	break;
case AFILE:
	f=de_relativise(c1,mtd_filename);
	e=filename_to_eweid(f);
	free(f);

	if (!el) el=CREATE_LIST(Eweid);


        if (EWEID_IS_NONE(e)) {
		fprintf(stderr,"%s: %s doesn't exist\n",mtd_filename,c1);
	return;
	}

//        mtd_remove_text(e);
        mtd_add_text(e," ");
        mtd_remove_work(e);


	Obj_Insert(el,e,ObjInsertTail);

	break;

default:
	break;
}
}



static void mtd_make_group(char *filename,Flock el)
{
Eweid e;

make_group(filename,el);

e=filename_to_eweid(filename);
mtd_add_group(e,filename);
mtd_add_work(e);

Obj_Delete(e);

}

