/* @(#)config.c 1.5 95/09/16 */

/*
 * Copyright (c) 1994, 1995 by Wayne C. Gramlich.  All rights reserved.
 *
 * Permission to use, copy, modify, distribute, and sell this software
 * for any purpose is hereby granted without fee provided that the above
 * copyright notice and this permission are retained.  The author makes
 * no representations about the suitability of this software for any purpose.
 * It is provided "as is" without express or implied warranty.
 */

/* LINTLIBRARY */

#include "assert.h"
#include "config_extern.h"
#include "error_extern.h"
#include "memory_extern.h"
#include "str_extern.h"

struct config_struct {
    Str annote_directory;	/* Name of annotation sub-directory */
    Str http_root;		/* Root of http documents */
    Str home_root;		/* Place where "~" maps to */
    Str host_name;		/* (E.g. "Www.Sun.Com") */
    Str original_directory;	/* Name of psuedo directory containing orig. */
};

/*
 * config_annote_directory(config)
 *	This routine will return the annotation directory associated
 *	with {config}.
 */
Str
config_annote_directory(
    Config config)
{
    return config->annote_directory;
}

/*
 * config_create(errors)
 *	This routine will create and return an initialized {Config} object.
 */
Config
config_create(
    Errors errors)
{
    Config config;
    FILE *config_file;
    Chr host_name[1000];    
    Chr http_root[1000];    
    unsigned size;
    int version;

    config = (Config)memory_allocate_zeroed(sizeof *config);
    config->annote_directory = (Str)"Annote";
    config->original_directory = (Str)"ORIGINAL";
    config->home_root = (Str)"/export/home";	/* Crock! */

    config_file = fopen("/tmp/httpd.config", "r");
    if (config_file == (FILE *)0) {
	errors_append(errors, (Str)"Version of NCSA HTTPD "
	  "that supports remote annotations is not running!");
	return config;
    } else {
	assert(fscanf(config_file, "Version: %d\n", &version) == 1);
	assert(version == 1);
	assert(fscanf(config_file, "DocumentRoot: %s\n", http_root) == 1);
	assert(fscanf(config_file, "HostName: %s\n", host_name) == 1);
	(void)fclose(config_file);
    }

    /*
     * The following temporary hack works around the fact that the
     * Gary Adams/Roger Riggs postinstall script just throws a "..."
     * onto the end of the host name.
     */
    size = str_size(host_name);
    if ((size > 3) && (str_equal(host_name + size - 3, (Str)"..."))) {
	host_name[size - 3] = '\0';
    }

    /* Finish filling in the config object: */
    config->host_name = str_copy(host_name);
    config->http_root = str_copy(http_root);
    return config;
}

/*
 * config_http_root(config)
 *	This routine will return the annotation directory associated
 *	with {config}.
 */
Str
config_http_root(
    Config config)
{
    return config->http_root;
}

/*
 * config_home_root(config)
 *	This routine will return the annotation directory associated
 *	with {config}.
 */
Str
config_home_root(
    Config config)
{
    return config->home_root;
}

/*
 * config_host_name(config)
 *	This routine will return the annotation directory associated
 *	with {config}.
 */
Str
config_host_name(
    Config config)
{
    return config->host_name;
}

/*
 * config_original_directory(config)
 *	This routine will return the pseudo directory associated with
 *	original from {config}.
 */
Str
config_original_directory(
    Config config)
{
    return config->original_directory;
}

