/* @(#)local.c 1.6 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.
 */

#include "assert.h"
#include "config_extern.h"
#include "error_extern.h"
#include "html_extern.h"
#include "local_extern.h"
#include "remote_extern.h"
#include "str_extern.h"
#include "text_extern.h"
#include "util_extern.h"
#include "url_extern.h"
#include "vote_extern.h"

/*
 * local_update(local_url, remote, config, matches, errors)
 *	This routine will generate the annotated version of the
 *	document named {local_url} using the reference information
 *	in {remote}.  Any previous information associated with {local_url}
 *	is read from disk, merged in, and saved back out again.
 *	Any errors encountered are appended to {errors].
 */
void
local_update(
    Url local_url,
    Remote remote,
    Config config,
    Errors errors)
{
    Url annote_url;
    Str annote_file_name;
    FILE *annote_file;
    Str local_db_file_name;
    FILE *local_file;
    Str local_file_name;
    Html local_html;
    Text local_text;
    Matches matches;
    Url original_url;
    Remotes remotes;

    /* Read in the source document to be annotated: */
    assert(remote != (Remote)0);
    local_file_name = url_str_local_path(local_url, config);
    local_file = fopen((char *)local_file_name, "r");
    if (local_file == (FILE *)0) {
	Str message;

	message = str_printf("Could not open file `%s' on host `%s'!",
	  local_file_name, config_host_name(config));
	errors_append(errors, message);
	return;
    }
    local_text = text_read(local_file);
    assert(local_text != (Text)0);
    local_html = html_parse(local_text);

    /* Open the annotation output file: */
    annote_url = url_annote(local_url, config);
    annote_file_name = url_str_local_path(annote_url, config);
    make_path(annote_file_name);
    annote_file = fopen((char *)annote_file_name, "w");
    if (annote_file == (FILE *)0) {
	Str message;

	message = str_printf("Could not open '%s' for writing!\n",
	  annote_file_name);
	errors_append(errors, message);
	return;
    }

    /* Merge the current remote into the database. */
    local_db_file_name = url_db_file_name(local_url, config);
    remotes = remotes_restore(local_db_file_name, errors);
    remotes_merge(remotes, remote);
    votes_write(remotes, config, errors);
    if (errors_size(errors) != 0) {
	return;
    }
    remotes_save(remotes, local_db_file_name, errors);
    if (errors_size(errors) != 0) {
	return;
    }

    /* Scan for in-line annotations: */
    matches = remotes_inline_search(remotes, local_html);

    /* Write the annotation announcement: */
    html_write_to_body_start(local_html, annote_file, 1);
    (void)fprintf(annote_file,
      "This document is an annotated version of an\n"
      "<A HRef=\"");
    original_url = url_original(local_url, config);
    url_write(original_url, annote_file);
    (void)fprintf(annote_file,
      "\">original unannotated document</A>.\n"
      "<HR>\n"
      "\n");

    /* Write the document body: */
    html_write_to_body_end(local_html, annote_file, matches, 1);

    /* Now write the annotation epilog: */
    (void)fprintf(annote_file,
      "\n"
      "<HR>\n"
      "You may read the\n"
/**/  "<A HRef=\"http://%s/%s/index.html\">\n"
      "documentation about public annotations</A>\n"
      "to add your own public annotations to this document.\n"
      "<P>\n",
      config_host_name(config), "annote_docs");
    if (remotes_size(remotes) == 0) {
	(void)fprintf(annote_file,
	  "Currently, there are no annotations in this document.\n");
    } else {
	(void)fprintf(annote_file,
	  "This document contains the following annotations:\n");
	remotes_write(remotes, annote_file, 0, config);
    }

    /* Write the closing stuff: */
    html_write_remaining(local_html, annote_file, 1);
    (void)fclose(annote_file);
    return;
}

