/* @(#)post_vote.c 1.3 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 <stdio.h>
#include <stdlib.h>

#include "config_extern.h"
#include "error_extern.h"
#include "form_extern.h"
#include "link_extern.h"
#include "local_extern.h"
#include "remote_extern.h"
#include "str_extern.h"
#include "text_extern.h"
#include "url_extern.h"
#include "vector_extern.h"
#include "vote_extern.h"

/*
 * main()
 *	This program will process a vote request.
 */
int
main(
    int argc,
    char **argv)
{
    Str annote_db_file_name;
    Str comment_str;
    Config config;
    int do_save;
    Str email_str;
    Errors errors;
    Link link;
    Links links;
    Url local_url;
    Str local_url_str;
    Remote remote;
    Str remote_url_str;
    Remotes remotes;
    FILE *response_file;
    Text submit_text;
    Url url;
    Str vote_str;

    response_file = stdout;
    (void)fprintf(response_file,
      "Content-type: text/html\n\n"
      "<HTML>\n"
      "<Head>\n"
      "<Title>\n"
      "Vote Response\n"
      "</Title>\n"
      "</Head>\n"
      "<Body>\n");

    errors = errors_create();
    config = config_create(errors);
    if (errors_size(errors) != 0) {
	goto errors_return;
    }
    argc--;
    argv++;
    if ((argc > 0) && (argc < 5)) {
	(void)fprintf(stderr,
	  "Usage: vote <vote> <email> <comment> <remote URL> <local URL>\n");
	return 0;
    } else if (argc >= 5) {
	/* Local test */
	assert(argc >= 5);
	submit_text = (Text)0;
	vote_str = (Str)argv[0];
	email_str = (Str)argv[1];
	comment_str = (Str)argv[2];
	remote_url_str = (Str)argv[3];
	local_url_str = (Str)argv[4];
    } else {
	submit_text = form_post_read(errors);
#ifdef DEBUG
	if (submit_text != (Text)0) {
	    (void)fprintf(response_file, "Submit = '");
	    text_write(submit_text, response_file);
	    (void)fprintf(response_file, "'<Br>\n");
	}
#endif /* DEBUG */
	vote_str = form_get_value(submit_text, (Str)"Vote", errors);
	email_str = form_get_value(submit_text, (Str)"Email", errors);
	comment_str = form_get_value(submit_text, (Str)"Comment", errors);
	remote_url_str = form_get_value(submit_text, (Str)"RemoteURL", errors);
	local_url_str = form_get_value(submit_text, (Str)"LocalURL", errors);
	if (errors_size(errors) != 0) {
	    goto errors_return;
	}
    }
#ifdef DEBUG
    (void)fprintf(response_file,
      "Vote = '%s'\n<Br>"
      "E-mail = '%s'\n<Br>"
      "Comment = '%s'\n<Br>"
      "RemoteURL = '%s'\n<Br>"
      "LocalURL = '%s'\n<Br>",
      vote_str, email_str, comment_str, remote_url_str, local_url_str);
#endif /* DEBUG */

    /* Slurp in database for source file: */
    local_url = url_parse(local_url_str);
    annote_db_file_name = url_db_file_name(local_url, config);
    remotes = remotes_restore(annote_db_file_name, errors);
    if (errors_size(errors) != 0) {
	goto errors_return;
    }

    url = url_parse(remote_url_str);
    REMOTES_LOOP(remote, remotes) {
	if (url_document_equal(remote_url(remote), url)) {
	    break;
	}
    }
    if (remote == (Remote)0) {
	errors_append(errors, (Str)"Could not find voting data!");
	goto errors_return;
    }
    links = remote_links(remote);
    LINKS_LOOP(link, links) {
	if (url_document_equal(link_href(link), local_url)) {
	    break;
	}
    }
    if (link == (Link)0) {
	errors_append(errors, (Str)"Could not find voting data!");
	goto errors_return;
    }
    do_save = votes_update(link, response_file,
      vote_str, comment_str, email_str, errors);
    if (errors_size(errors) != 0) {
	goto errors_return;
    }
    if (do_save) {
	remotes_save(remotes, annote_db_file_name, errors);
	if (errors_size(errors) != 0) {
	    goto errors_return;
	}
	local_update(link_href(link), remote, config, errors);
	if (errors_size(errors) != 0) {
	    goto errors_return;
	}
    }

    (void)fprintf(response_file,
      "</Body>\n"
      "</HTML>\n");
    (void)fflush(response_file);
    return 0;
  errors_return:
    errors_write(errors, stdout, 1);
    (void)fprintf(response_file,
      "</Body>\n"
      "</HTML>\n");
    (void)fflush(response_file);
    return 1;
}




