#include <unistd.h>
#include <stdio.h>
#define ML 132

main(int argc, char **argv)
{
    int num;
    FILE *ip, *op;
    char s[ML], prev[ML], next[ML];

    sscanf(argv[1],"allgre.%d.html",&num);
    sprintf(prev,"allgre.%d.html",num-1);
    sprintf(next,"allgre.%d.html",num+1);

    ip = fopen(argv[1],"r");
    op = fopen("tmp.tmp", "w");

    fprintf(op,"<title>Allen and Greenough's New Latin Grammar, section %d</title>\n",num);
    fprintf(op,"<h2>Heavy Construction</h2>\n");
    fprintf(op,"The Allen and Greenough is still under construction;\n");
    fprintf(op,"so some links may not work quite the way you would expect.\n");
    fprintf(op,"<p>\n<hr>\n");

    fprintf(op,"\n<a href=\"allgre.contents.html\"><img src=\"img/contents.gif\"></a>\n");
    if(num>0 && access(prev,R_OK)==0)
        fprintf(op,"<a href=\"%s\"><img src=\"img/prev.gif\"></a>\n",prev);
                
    if(num<642 && access(next,R_OK)==0)
        fprintf(op,"<a href=\"%s\"><img src=\"img/next.gif\"></a>\n",next);

    if(num>0)
        fprintf(op,"<a href=\"allgre.0.html\"><img src=\"img/copyright.gif\"></a>\n");

    fprintf(op,"<hr>\n");
    while(fgets(s,ML,ip))
        fputs(s,op);

    fclose(ip);
    fprintf(op,"<hr>\n");
    fprintf(op,"\n<a href=\"allgre.contents.html\"><img src=\"img/contents.gif\"></a>\n");
    if(num>0 && access(prev,R_OK)==0)
        fprintf(op,"<a href=\"%s\"><img src=\"img/prev.gif\"></a>\n",prev);
                
    if(num<642 && access(next,R_OK)==0)
        fprintf(op,"<a href=\"%s\"><img src=\"img/next.gif\"></a>\n",next);

    if(num>0)
        fprintf(op,"<a href=\"allgre.0.html\"><img src=\"img/copyright.gif\"></a>\n");

    fclose(op);
    unlink(argv[1]);
    rename("tmp.tmp",argv[1]);
}
