static char rcsver[] = "$Id: dataset.c,v 2.2 1999/07/12 23:14:08 gorelick Exp $";
 
/**
 ** $Source: /tes/src/vanilla/RCS/dataset.c,v $
 **
 ** $Log: dataset.c,v $
 ** Revision 2.2  1999/07/12 23:14:08  gorelick
 ** *** empty log message ***
 **
 ** Revision 2.1  1999/02/10 04:00:50  gorelick
 ** *** empty log message ***
 **
 ** Revision 2.0  1998/12/22 22:47:04  gorelick
 ** release version
 **
 ** Revision 2.0  1998/12/18 01:26:03  gorelick
 ** release version
 **
 ** Revision 1.5  1998/12/01 22:42:06  gorelick
 ** *** empty log message ***
 **
 ** Revision 1.4  1998/11/12 22:58:55  gorelick
 ** first release version
 **
 **/

#include "header.h"
#include "proto.h"
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>


/**
 ** Load dataset stuff
 **
 ** Options:
 **
 ** 	table
 ** 	path/table
 ** 	path
 **
 **/

DATASET *
LoadDataset(DATASET *dataset, char *fname)
{
	char *path, *file, *dir, *tbl_name, *dir_name;
	struct stat sbuf;
	char tmppath[256], newpath[256], buf[256], *p;
	FILE *fp;
	int cwd;

    if (dataset == NULL) {
        dataset = calloc(1, sizeof(DATASET));
        dataset->tables = new_list();
        dataset->tablenames = new_list();
    }
    
    path = strdup(fname);
    /**
     ** Split fname into path/file
     **/
    if ((file = strrchr(path, '/')) == NULL) {
        dir = ".";
        file = path;
    } else {
        *file = '\0';
        file++;
        dir = path;
    }

    if (stat(fname, &sbuf) != 0) {
    	  fname = find_file(fname);
        if (stat(fname, &sbuf) != 0) {
            fprintf(stderr, "Bad path: %s\n", fname);
            return (NULL);
        }
    }

    if ((sbuf.st_mode & S_IFDIR) != 0) {
        dir = fname;
        file = "DATASET";
    }

    sprintf(newpath, "%s/%s", dir, file);
	 fname = find_file(newpath);

	if (access(fname, F_OK)) {
		sprintf(newpath, "%s/%s.LST", dir, file);
		fname = find_file(newpath);
	}

	if (access(fname, F_OK)) {
		sprintf(newpath, "%s/%s.TXT", dir, file);
		fname = find_file(newpath);
	}

    /**
     ** Open the DATASET file and extract its contents
     **/
    if ((fp = fopen(fname, "r")) == NULL) {
		fprintf(stderr, "Unable to open DATASET file: %s\n", fname);
		return (NULL);
    }

    while (fgets(buf, 256, fp) != NULL) {
		if ((p = strrchr(buf, '\r')) != NULL) *p = '\0';
		if ((p = strrchr(buf, '\n')) != NULL) *p = '\0';

        if (buf[strlen(buf) -1] == '/') {
        /**
	     ** This is a path to another dataset in another directory
	     **/
            LoadDataset(dataset, buf);
        } else if (strchr(buf, '/')) {
        /**
	     ** This is an explicit path to another table
	     **/
            tbl_name = (char *)basename(buf);
            dir_name = (char *)dirname(buf);

            if (buf[0] == '/') {
                LoadTable(dataset, dir_name, tbl_name);
            } else {
                sprintf(tmppath, "%s/%s", dir, dir_name);
                LoadTable(dataset, tmppath, tbl_name);
            }
        } else {
            /**
	         ** This is the name of a table, load it directly.
	         **/
            LoadTable(dataset, dir, buf);
        }
    }
	fclose(fp);
    return(dataset);
}


TABLE *
FindTable(DATASET *dataset, char *name)
{
	int i;

	for (i = 0 ; i < dataset->tables->number ; i++) {
		if (!strcmp(((char **)dataset->tablenames->ptr)[i], name)) {
			return(((TABLE **)dataset->tables->ptr)[i]);
		}
	}
	return(NULL);
}

/**
 ** Search the dataset to see if this table already exists in it.
 ** If not, make a new TABLE
 ** Add the file entries from <path> and sort.
 **/

LoadTable(DATASET *dataset, char *path, char *name)
{
    TABLE *table = FindTable(dataset, name);

    if (table == NULL) {
        table = calloc(1, sizeof(TABLE));
        table->files = LoadFilenames(path, name);
		if (table->files->number != 0) {
			list_add(dataset->tables, table);
			list_add(dataset->tablenames, strdup(name));
			table->label = LoadLabel((table->files->ptr)[0]);
			table->label->table = table;
		} else {
			free(table);		/* this should be FreeTable */
		}
    } else {
        LIST *files = LoadFilenames(path, name);
        list_merge(table->files, files);
        SortFiles(table->files);

		/* Check for duplicate file names? */
    }
}