/* @(#)setup_public_annote.c 1.7 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 <string.h>
#include <sys/types.h>
#include <sys/stat.h>

#define ANNOTE_DOCS "annote_docs"
#define AUTHOR_NAME "Wayne C. Gramlich"
#define AUTHOR_URL "http://playground.sun.com/~gramlich/gramlich.html"
#define PAPER_TITLE "Public Annotation Systems"
#define PAPER_URL "http://playground.sun.com/~gramlich/1994/annote/index.html"

extern FILE *popen(const char *, const char *);

/*
 * This program is called from httpd to create some files for
 * supporting public annotations.  The files are:
 *
 *	/tmp/httpd.config	Info needed to support remote update cgi progs.
 *	<doc_root>/update.html	A form to add a public annotation.
 *	<doc_root>/solicit.html	A form to cause a document to request
 *				public annotations.
 */
#define MAX_STRING_LEN 2000

/* Each empty comment marks the location of a "%s" in the printf string. */

int
main(
    int argc,
    char **argv)
{
    const char *config_file_name = "/tmp/httpd.config";

    FILE *config_file;
    char *document_root;
    int errors;
    char *host_name;
    char out_file_name[MAX_STRING_LEN];
    FILE *out_file;
    char *version;
    char *sub_dir;

    /* Process the arguments: */
    argc--;
    argv++;
    if (argc < 3) {
	(void)printf("usage: setup_public_annote version doc_root hostname\n");
	return 0;
    }
    version = argv[0];
    document_root = argv[1];
    host_name = argv[2];
    errors = 0;

    /* Write out the configuration file: */
    config_file = fopen(config_file_name, "w");
    if (config_file == (FILE *)0) {
	(void)fprintf(stderr, "Could not open `%s'!\n", config_file_name);
	errors++;
    } else {
	(void)fprintf(config_file, "Version: %s\n", version);	
	(void)fprintf(config_file, "DocumentRoot: %s\n", document_root);
	(void)fprintf(config_file, "HostName: %s\n", host_name);
	(void)fclose(config_file);
    }

    /* Create the annotation documentation sub-directory: */
    sub_dir = ANNOTE_DOCS;
    (void)sprintf(out_file_name, "%s/%s", document_root, sub_dir);
    (void)mkdir(out_file_name, 0777);

    (void)sprintf(out_file_name, "%s/%s/update.html", document_root, sub_dir);
    out_file = fopen(out_file_name, "w");
    if (out_file == (FILE *)0) {
	(void)fprintf(stderr, "Could not open `%s'!\n", out_file_name);
	errors++;
    } else {
	(void)fprintf(out_file,
	  "<HTML>\n"
	  "<Head>\n"
	  "<Title>Registering a Public Annotation</Title>\n"
	  "</Head>\n"
	  "<Body>\n"
	  "<H1>Registering a Public Annotation</H1>\n"
	  "This form is used to register the existence of a\n"
/**/	  "public annotation to a document on `%s'.  Most people\n"
	  "will prefer to use the\n"
	  "    <A HRef=\"easy.html\">\n"
	  "easy method</A> for authoring public annotations\n"
	  "using Mosaic personal annotations and the\n"
	  "    <A HRef=\"post_public_man_page.html\">\n"
	  "post_public command</A>.  However, if you do not\n"
	  "use Mosaic as your Web browser, you will probably\n"
	  "want to use this form instead.\n"
	  "<P>\n"
	  "In order to add a public annotation to a document on\n"
/**/	  "`%s', you need to do the following:\n"
	  "<OL>\n"
	  "  <LI>\tCreate an HTML document on your own machine that\n"
	  "\tcontains the text of your annotation.  Somewhere in\n"
	  "\tyour public annotation, insert a hypertext link to the\n"
/**/	  "\tdocument on `%s'.  Within the hypertext link, be sure\n"
	  "\tto specify the full URL for the HREF attribute and a\n"
	  "\tunique value for the NAME attribute.  Also, please\n"
	  "\tuse a meaningful title for your annotation, since it\n"
	  "\twill be listed in the public annotation list.\n"
	  "  <LI>\tType the full URL for the HTML document on your\n"
	  "\tmachine into the form below and submit it.\n"
	  "\tThe act of submitting the URL for your public annotation\n"
	  "\tto this form will cause it to be read, scanned, and\n"
	  "\tadded to the public annotation list for the document\n"
/**/	  "\ton `%s'.\n"
	  "</OL>\n"
	  "It is that easy!\n"
	  "<P>\n"
	  "<Form Method=POST "
/**/	  "action=\"http://%s/cgi-bin/post_update\">\n"
	  "Full URL for public annotation on your machine:<Br>\n"
	  "<Input Type=\"text\" Name=\"RefURL\" Size=\"70\">\n"
	  "<Br>\n"
	  "<Input Type=\"submit\" Value=\"Submit Your Public Annotation\">\n"
	  "</Form>\n"
	  "<P>\n"
	  "For further information on public annotation systems,\n"
	  "please consult the paper \n"
/**/	  "    <A HRef=\"%s\">\n"
/**/	  "<I>%s</I></A> authored by\n"
/**/	  "    <A HRef=\"%s\">\n"
/**/	  "%s</A>.\n"
	  "</Body>\n"
	  "</HTML>\n",
	  host_name, host_name, host_name, host_name, host_name,
	  PAPER_URL, PAPER_TITLE, AUTHOR_URL, AUTHOR_NAME);
	(void)fclose(out_file);
    }

    /* Write out easy.html file: */
    (void)sprintf(out_file_name, "%s/%s/easy.html", document_root, sub_dir);
    out_file = fopen(out_file_name, "w");
    if (out_file == (FILE *)0) {
	(void)fprintf(stderr, "Could not open `%s'!\n", out_file_name);
	errors++;
    } else {
	(void)fprintf(out_file,
	  "<HTML>\n"
	  "<Head>\n"
	  "<Title>Adding Public Annotations the Easy Way</Title>\n"
	  "</Head>\n"
	  "<Body>\n"
	  "<H1>Adding Public Annotation the Easy Way</H1>\n"
	  "\n"
	  "This document describes the easy way for you to add a\n"
/**/	  "public annotation to any document on `%s' that has not\n"
	  "been explicitly disallowed.  A public annotation is\n"
	  "attached to a document using the personal annotation\n"
	  "system in Mosaic and the\n"
	  "    <A HRef=\"post_public_man_page.html\">\n"
	  "post_public command</A>.  If you are not using Mosaic as\n"
	  "your Web browser, you may use the\n"
	  "    <A HRef=\"update.html\">\n"
	  "public annotation registration form</A> instead.\n"
	  "<P>\n"
	  "<DL>\n"
	  "  <DT>\tPublic Annotation Creation:\n"
	  "  <DD>\tUse the following steps to create a public\n"
	  "\tannotation:\n"
	  "\t<OL>\n"
	  "\t  <LI>\tUsing Mosaic, fetch the\n"
	  "\t\t    <A HRef=\"post_public_man_page.html\">\n"
	  "\t\tpost_public command</A> and add it to your\n"
	  "\t\tsearch path.\n"
	  "\t  <LI>\tPosition Mosaic so that it is showing the document\n"
	  "\t\tyou wish to annotate.  The document to be annotated must\n"
/**/	  "\t\thave been fetched from `%s'.\n"
	  "\t  <LI>\tClick on the [Annotate] => [Annotate...] menu\n"
	  "\t\tat the top of the screen.\n"
	  "\t  <LI>\tEnter your annotation as a personal annotation\n"
	  "\t\tin the annotation window.  Be sure to type in a\n"
	  "\t\treasonable title.\n"
	  "\t  <LI>\tClick on the [Commit] button in the annotation\n"
	  "\t\twindow to write the personal annotation out to disk.\n"
	  "\t  <LI>\tIn a window with a Unix shell, execute the\n"
	  "\t\t`post_public' you fetched in step 1 with no options.\n"
	  "\tThe `post_public' command will take personal annotation\n"
	  "\tyou just wrote out in step 5 and cause it tb be made\n"
	  "\tpublicly available to anyone who reads the document.\n"
	  "\t(If you have not installed an HTTPD server on your machine,\n"
	  "\tyou may use the -h option to specify the host to use to\n"
	  "\tdocuments from your public_html directory.)\n"
	  "\t</OL>\n"
	  "\tIt is that easy!\n"
	  "  <P>\n"
	  "  <DT>\tEditing a Public Annotation:\n"
	  "  <DD>\tYou can modify the contents of your public annotation\n"
	  "\tby having Mosaic view the associated personal annotation\n"
	  "\tand clicking on the [Annotate]=>[Edit This Annotation]\n"
	  "\tmenu.  In addition, you can use post_pubilic command with\n"
	  "\t-e option to use your favorite editor to edit the latest\n"
	  "\tpublic annotation."
	  "  <P>\n"
	  "  <DT>\tDeleting a Public Annotation:\n"
	  "  <DD>\tDeleting public annotations is currently unimplemented.\n"
	  "\tPublic annotation deletion will eventually be implemented\n"
	  "\tvia the post_public with the -d option.\n"
	  "</DL>\n"
	  "For further information on public annotation systems,\n"
	  "please consult the paper \n"
/**/	  "    <A HRef=\"%s\">\n"
/**/	  "<I>%s</I></A> authored by\n"
/**/	  "    <A HRef=\"%s\">\n"
/**/	  "%s</A>.\n"
	  "</Body>\n"
	  "</HTML>\n",
	  host_name, host_name,
	  PAPER_URL, PAPER_TITLE, AUTHOR_URL, AUTHOR_NAME);
	(void)fclose(out_file);
    }

    /* Write out the post_public man page: */
    (void)sprintf(out_file_name, "%s/%s/post_public_man_page.html",
      document_root, sub_dir);
    out_file = fopen(out_file_name, "w");
    if (out_file == (FILE *)0) {
	(void)fprintf(stderr, "Could not open `%s'!\n", out_file_name);
	errors++;
    } else {
	FILE *uname;
	char line[1000];

	uname = popen("uname -srp", "r");
	assert(fgets(line, sizeof(line), uname) != (char *)0);
	line[strlen(line)-1] = '\0';
	(void)fprintf(out_file,
	  "<HTML>\n"
	  "<Head>\n"
	  "<Title>post_public(1)</Title>\n"
	  "</Head>\n"
	  "<Body>\n"
	  "<H1>post_public(1)</H1>\n"
	  "<DL>\n"
	  "  <DT>\tSYNOPSIS\n"
	  "  <DD>\tpost_public [ -d ] [ -e ] [ -h <I>host</I> ]\n"
	  "\t[-p <I>pattern</I>]\n"
	  "  <P>\n"
	  "  <DT>\tAVAILABILITY\n"
	  "  <DD>\tThe post_public command is available as either a\n"
	  "\t<A HRef=\"post_public.tar\">binary executable</A> compiled for\n"
/**/	  "\t`%s' or a <A HRef=\"source.tar\">complete source code tree</A>.\n"
	  "\tBoth the binary executable and source code tree are provided\n"
	  "\tin Unix file in tar format; after fetching them, they can be\n"
	  "\tunpacked by typing `tar xf <I>file_name</I>.tar'.\n"
	  "  <P>\n"
	  "  <DT>\tDESCRIPTION\n"
	  "  <DD>\tThe post_public command takes the last personal\n"
	  "\tannotation created/modified by Mosaic and makes it\n"
	  "\tpublicly available.\n"
	  "  <P>\n"
	  "  <DT>\tOPTIONS\n"
	  "  <DD>\t<DL>\n"
	  "\t  <DT>\t-d\n"
	  "\t  <DD>\t(Unimplemented) Delete a public annotation.\n"
	  "\t  <DT>\t-e\n"
	  "\t  <DD>\tInvoke an editor on the annotation prior to\n"
	  "\tmaking it public.  The EDITOR environment variable\n"
	  "\tis used to determine which editor to invoke.\n"
	  "\t  <DT>\t-h <I>hostname</I>\n"
	  "\t  <DD>\tUse <I>hostname</I> instead of local machine\n"
	  "\tto access the user's `public_html' directory.\n"
	  "\t  <DT>\t-p <I>pattern</I>\n"
	  "\t  <DD>\tMake the annotation an in-line annotation and\n"
	  "\t	attach it to <I>pattern</I> in the annotated document.\n"
	  "\t</DL>\n"
	  "  <P>\n"
	  "  <DT>\tENVIRONMENT\n"
	  "\t<DL>\n"
	  "\t  <DT>EDITOR\n"
	  "\t  <DD>Specifies which editor to use when -e option is used.\n"
	  "\t</DL>\n"
	  "  <P>\n"
	  "  <DT>\tSEE ALSO\n"
/**/	  "  <DD>\t    <A HRef=\"%s\">\n"
/**/	  "\t%s</A> by\n"
/**/	  "\t    <A HRef=\"%s\">\n"
/**/	  "\t%s</A>\n"
	  "  <P>\n"
	  "  <DT>\tBUGS\n"
	  "  <DD>\tThe -d option for deleting public annotations\n"
	  "\thas not been implemented yet.\n"
	  "\t<P>\n"
	  "</DL>\n"
	  "</Body>\n"
	  "</HTML>\n",
	  line, PAPER_URL, PAPER_TITLE, AUTHOR_URL, AUTHOR_NAME);
	(void)fclose(out_file);
    }

    /* Write out solicit.html: */
    (void)sprintf(out_file_name, "%s/%s/solicit.html",
      document_root, sub_dir);
    out_file = fopen(out_file_name, "w");
    if (out_file == (FILE *)0) {
	(void)fprintf(stderr, "Could not open `%s'!\n", out_file_name);
	errors++;
    } else {
	(void)fprintf(out_file,
	  "<HTML>\n"
	  "<Head>\n"
	  "<Title>Solicit Public Annotations Form</Title>\n"
	  "</Head>\n"
	  "<Body>\n"
	  "<H1>Solicit Public Annotations Form</H1>\n"
	  "\n"
/**/	  "This form is used to mark documents on `%s'\n"
	  "as available for public annotations.\n"
	  "<P>\n"
	  "<Form Method=POST "
/**/	  "action=\"http://%s/cgi-bin/post_solicit\">\n"
	  "Full URL for the document soliciting public annotations:<Br>\n"
	  "<Input Type=\"text\" Name=\"URL\" Size=\"70\">\n"
	  "<Br>\n"
	  "<Input Type=\"submit\" Value=\"Solicit Public Annotations\">\n"
	  "</Form>\n"
	  "</Body>\n"
	  "</HTML>\n",
	  host_name, host_name);
	(void)fclose(out_file);
    }

    /* Write out the post_solicit man page: */
    (void)sprintf(out_file_name, "%s/%s/post_solicit_man_page.html",
      document_root, sub_dir);
    out_file = fopen(out_file_name, "w");
    if (out_file == (FILE *)0) {
	(void)fprintf(stderr, "Could not open `%s'!\n", out_file_name);
	errors++;
    } else {
	(void)fprintf(out_file,
	  "<HTML>\n"
	  "<Head>\n"
	  "<Title>post_solicit(1)</Title>\n"
	  "</Head>\n"
	  "<Body>\n"
	  "<H1>post_solicit(1)</H1>\n"
	  "<DL>\n"
	  "  <DT>\tSYNOPSIS\n"
	  "  <DD>\tpost_solicit <I>url</I>\n"
	  "  <P>\n"
	  "  <DT>\tAVAILABILITY\n"
	  "  <DD>\tThe post_public command is available as part of\n"
	  "\t<A HRef=\"source.tar\">public annotations source release</A>.\n"
	  "\tThe soruce release is available in Unic tar file format;\n"
	  "\tafter fetching it, it can be unpacked by typing\n"
	  "\t`tar xf source.tar'.\n"
	  "  <P>\n"
	  "  <DT>\tDESCRIPTION\n"
	  "  <DD>\tThe post_solicit command takes a full URL for a\n"
	  "\tdocument on the local machine and makes it available for\n"
	  "\tpublic annotations.\n"
	  "  <P>\n"
	  "  <DT>\tSEE ALSO\n"
/**/	  "  <DD>\t    <A HRef=\"%s\">\n"
/**/	  "\t%s</A> by\n"
/**/	  "\t    <A HRef=\"%s\">\n"
/**/	  "\t%s</A>\n"
	  "</DL>\n"
	  "</Body>\n"
	  "</HTML>\n",
	  PAPER_URL, PAPER_TITLE, AUTHOR_URL, AUTHOR_NAME);
	(void)fclose(out_file);
    }

    /* Write out the post_update man page: */
    (void)sprintf(out_file_name, "%s/%s/post_update_man_page.html",
      document_root, sub_dir);
    out_file = fopen(out_file_name, "w");
    if (out_file == (FILE *)0) {
	(void)fprintf(stderr, "Could not open `%s'!\n", out_file_name);
	errors++;
    } else {
	(void)fprintf(out_file,
	  "<HTML>\n"
	  "<Head>\n"
	  "<Title>post_update(1)</Title>\n"
	  "</Head>\n"
	  "<Body>\n"
	  "<H1>post_update(1)</H1>\n"
	  "<DL>\n"
	  "  <DT>\tSYNOPSIS\n"
	  "  <DD>\tpost_update <I>url</I>\n"
	  "  <P>\n"
	  "  <DT>\tAVAILABILITY\n"
	  "  <DD>\tThe post_public command is available as part of\n"
	  "\t<A HRef=\"source.tar\">public annotations source release</A>.\n"
	  "\tThe soruce release is available in Unic tar file format;\n"
	  "\tafter fetching it, it can be unpacked by typing\n"
	  "\t`tar xf source.tar'.\n"
	  "  <P>\n"
	  "  <DT>\tDESCRIPTION\n"
	  "  <DD>\tThe post_update command takes a full URL for a\n"
	  "\tpublic annotation, scans it for references to documents\n"
	  "\ton the local machine, and annotates the appropriate\n"
	  "\tdocuments.\n"
	  "  <P>\n"
	  "  <DT>\tSEE ALSO\n"
/**/	  "  <DD>\t    <A HRef=\"%s\">\n"
/**/	  "\t%s</A> by\n"
/**/	  "\t    <A HRef=\"%s\">\n"
/**/	  "\t%s</A>\n"
	  "</DL>\n"
	  "</Body>\n"
	  "</HTML>\n",
	  PAPER_URL, PAPER_TITLE, AUTHOR_URL, AUTHOR_NAME);
	(void)fclose(out_file);
    }

    /* Write out the index.html file: */
    (void)sprintf(out_file_name, "%s/%s/index.html",
      document_root, sub_dir);
    out_file = fopen(out_file_name, "w");
    if (out_file == (FILE *)0) {
	(void)fprintf(stderr, "Could not open `%s'!\n", out_file_name);
	errors++;
    } else {
	(void)fprintf(out_file,
	  "<HTML>\n"
	  "<Head>\n"
	  "<Title>Public Annotation Documentation</Title>\n"
	  "</Head>\n"
	  "<Body>\n"
	  "<H1>Public Annotation Documentation</H1>\n"
	  "\n"
	  "<DL>\n"
	  "  <DT>\t<A HRef=\"easy.html\">\n"
	  "\tEasy Public Annotation Authoring</A>\n"
	  "  <DD>\tA description of how to use Mosaic personal\n"
	  "\tannotations in conjunction with the post_public\n"
	  "\tcommand to easily author public annotations.\n"
	  "  <DT>\t<A HRef=\"post_public_man_page.html\">\n"
	  "\tpost_public manual page</A>\n"
	  "  <DD>\tDocument describing the post_public command.\n"
	  "  <DT>\t<A HRef=\"update.html\">\n"
	  "\tPublic Annotation Registration Form</A>\n"
	  "  <DD>\tForm that non-Mosaic users can use to register\n"
	  "\ta public annotation.\n"
	  "  <DT>\t<A HRef=\"solicit.html\">\n"
	  "\tPublic Annotation Solicitation Form</A>\n"
	  "  <DD>\tForm that authors can use to solicit\n"
	  "\tpublic annotations for their document.\n"
	  "  <DT>\t<A HRef=\"post_solicit_man_page.html\">\n"
	  "\tpost_solicit_man_page</A>\n"
	  "  <DD>\tProgram that allows authors can use to solicit\n"
	  "\tpublic annotations for their document.\n"
	  "  <DT>\t<A HRef=\"post_update_man_page.html\">\n"
	  "\tpost_update_man_page</A>\n"
	  "  <DD>\tProgram that allows remote authors to register\n"
	  "\ta document as a public annotation.\n"
/**/	  "  <DT>\t<A HRef=\"%s\">\n"
/**/	  "\t%s</A>\n"
/**/	  "  <DD>\tA paper by <A HRef=\"%s\">\n"
/**/	  "\t%s</A> that discusses public annotation systems.\n"
	  "</DL>\n"
	  "</Body>\n"
	  "</HTML>\n",
	  PAPER_URL, PAPER_TITLE, AUTHOR_URL, AUTHOR_NAME);
	(void)fclose(out_file);
    }

    /* We're out'a here! */
    if (errors > 0) {
	(void)fflush(stderr);
	return 1;
    }
    return 0;
}





