/*
 * AmiGet.c - very quick hack to create amigaguide docs from search output
 * Urban Mueller, umueller@wuarchive.wustl.edu, umueller@amiga.icu.net.ch
 */

#define INDEXNAME   "AMINET_0294:INDEX"
#define INDEXLEN    700000
#define AMINETFILES "AMINET_0294:Aminet/"
#define MORE        "sys:utilities/more"

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <exec/types.h>
#include <dos/dostags.h>
#include <libraries/dos.h>

char *path = AMINETFILES;
char *index= INDEXNAME;

char name[200], dir[200], fill[200], readme[200], sys[200], str[200];
char low[256], head[1000], *hp=head, tempnam[200];

FILE *g;
BPTR f;

char *linebeg (char *str)
{
  while (*--str != '\n')
    ;
  return str+1;
}

char *stristr(str1, str2)
  char *str1, *str2;
{
  char c = low[(unsigned char)*str1], l = strlen(str1);

  for (;;) {
    while (low[(unsigned char)*str2++] != c)
      ;
    str2--;

    if (!strnicmp(str1, str2, l))
      return str2;

    str2++;
  }

  return 0;
}

char dnbuf[50];

char *
dirhead(str)
  char *str;
{
  char *t=dnbuf;

  while (*str && *str!='/')
    *t++ = *str++;
  *t=0;

  return dnbuf;
}

mysystem( char *nam )
{
  char buf[200];
  long *arr={TAG_DONE};

  sprintf (buf, "%s %s", nam, tempnam);

  return System (buf, arr);
}

main(int argc, char *argv[])
{
  char buf[200], cdir[200], fdir[200], *s, *d;
  char *mem, *pos, *end, *desc;
  int  mlen, nlen, find=0, i, n=0;

  sprintf (tempnam,"t:netguide.%x",buf);
  *cdir=*fdir=0;

  if (!(mem=malloc(INDEXLEN+400)))
    printf("Out of memory, try adapting INDEXLEN\n"), exit(20);
  
  if (!(f=Open(index,MODE_OLDFILE)))
    printf("Cannot open Aminet %s for input\n",index), exit(20);

  mlen=Read(f,mem+16,INDEXLEN);
  Close(f);

  mem[15]='\n';
  mem+=16;

  if (!(g=fopen(tempnam,"w")))
    printf("Cannot open %s for output\n",tempnam), exit(20);

  for (i=0; i<256; i++)
    low[i]= i>='A' && i<='Z' ? i-'A'+'a' : i;

  if( argc>1)
    strcpy (str, argv[1]), find=1;


  if (argc==0) {
    printf("Enter string to look for:\n");
    printf("(or hit RETURN to see full index)\n> ");
    fflush(stdout);
    gets(str);
    find= *str;
    printf("Processing...\n");
    fflush(stdout);
  }


  if (find) {

    fprintf (g,"@database Aminet\n@node main \"Aminet files matching '%s'\"\n", str);
    fprintf (g, "Click name to unpack to RAM:, or click description to read .readme\n\n");
    strlwr(str);
    strcpy  ( mem+mlen, str);

  } else {

    hp+= sprintf (hp,"@database Aminet\n@node main \"Complete Aminet index\"\n");
    hp+= sprintf (hp,"Please pick a directory\n\n");
    for (i=0; i<30; i++)
      fprintf (g, "                               \n");

  }

  end= mem+mlen;
  pos= find ? linebeg ( stristr(str, mem) ) : mem;

  while (pos<end) {

    if (*pos=='|') {
      pos= strchr(pos,'\n')+1;
      continue;
    }

    /*--------------------- formatting ---------------- */

    for ( s=pos, d=name; *s && *s!=' '; )
      *d++ = *s++;
    *d= 0;
    nlen=s-pos;

    for ( s=pos+nlen, d=fill; s<pos+37; )
      *d++ = *s++;
    *d= 0;

    for ( s=pos+21, d=dir; *s!=' '; )
      *d++ = *s++;
    *d= 0;

    for ( s=pos+21; *s!='\n'; s++ )
      if (*s=='\"')
        *s='\'';
    *s=0;

    strcpy(readme, name);
    if (nlen>3)
      strcpy(readme+nlen-3,"readme");

    desc= pos+37;

    /*--------------------- titles -------------------- */
    if (!find) {
      if (strcmp(cdir,dirhead(dir))) {
        strcpy(cdir,dirhead(dir));
        strcpy(fdir, dir);
        hp += sprintf(hp,"  @{\" %-4.4s \" link \"%s\"}\n", cdir, cdir);

        fprintf (g,"@endnode\n\n");
        fprintf (g,"@node %s \"Directory %s\"\n", cdir, cdir);
        fprintf (g, "Click name to unpack to RAM:, or click description to read .readme\n\n");

      } else if (strcmp (fdir, dir)) {

        strcpy  (fdir, dir);
        fprintf (g, "\n"); 

      }
    }

    fprintf(g,"@{\"%s\" system \"lha e %s%s/%s RAM:\"}%s@{\"%s\" system \"%s %s%s/%s\"}\n",
            name, path, dir, name, fill, desc, MORE, path, dir, readme);

    *s++='\n';
    if (find)
      pos= linebeg( stristr ( str, s));
    else
      pos= s;

    n++;
  }

  if (find) {

    fprintf (g, "\n%d files listed\n", n);
    fprintf (g, "@endnode\n");

  } else {

    fprintf (g, "@endnode\n");
    fseek   (g, 0, 0);
    fprintf (g, head);

  }

  free(mem-16);
  fclose(g);

  if (n || argc==0) {

    if( mysystem("sys:utilities/multiview"))
      if( mysystem("sys:utilities/amigaguide"))
        if( mysystem("multiview"))
          if( mysystem("amigaguide"))
            printf("Could not start AmigaGuide\n"), Delay(120), exit(20);

  } else 
    printf("No matching files found\n");
      
  unlink(tempnam);

}
