#include <exec/types.h>
#include <exec/memory.h>
#include <intuition/intuition.h>
#include <libraries/gadtools.h>
#include <libraries/expansion.h>

#include <stdlib.h>
#include "amigan.h"

#define MAINTYPES   15
#define CATEGORIES 339
struct
{   ULONG start, end;
} offset[MAINTYPES + 1] =
{   16,  26, // biz
    27,  47, // comm
    48,  73, // demo
    74,  90, // dev
    91,  97, // disk
    98, 105, // docs
   106, 120, // games
   121, 132, // gfx
   133, 135, // hard
   136, 140, // info
   141, 150, // misc
   151, 260, // mods
   261, 264, // mus
   265, 305, // pix
   306, 316, // text
   317, 339  // util
};

// from rp1.c
IMPORT SBYTE               page;
IMPORT struct NewGadget*   Gadget;
IMPORT struct Gadget      *BU99_Left,
                          *BU99_Right,
                          *ST99_Output,
                          *BU99_OutputASL,
                          *PrevGadPtr;
IMPORT struct Window*      MainWindowPtr;
IMPORT struct SharedStruct shared;
IMPORT TEXT                IOBuffer[LONGESTFIELD + 1];
IMPORT ULONG               wbval;
IMPORT TEXT                aslresult[MEDFIELD + 1];
IMPORT STRPTR              textBuffer[MAXELEMENTS + 1];

MODULE void aminet_exit(void);

MODULE TEXT                string[VLONGFIELD + 1];
MODULE struct List        *MainTypeListPtr = NULL,
                          *SubTypeListPtr  = NULL;
MODULE struct
{   TEXT  amishort[41], // `short' is a reserved keyword, of course
          uploader[41],
          author[41],
          requires[41],
          version[41],
          distribution[41],
          type[11], // ____/_____\n
          replaces[41];
    ULONG maintype,
          subtype;
} aminet =
{    "", "", "", "", "1.0", "", "", "", 0, 0
};

MODULE struct Gadget *ST21_Short              = NULL,
                     *ST21_Uploader           = NULL,
                     *ST21_Author             = NULL,
                     *ST21_Requires           = NULL,
                     *ST21_Version            = NULL,
                     *ST21_Distribution       = NULL,
                     *ST21_Replaces           = NULL,
                     *LV21_MainType           = NULL,
                     *LV21_SubType            = NULL;

#define LONGESTDESC 36
MODULE struct
{   TEXT name[5 + 1];
    TEXT desc[LONGESTDESC + 1];
} category[CATEGORIES + 1] =
{ // all (0)
"biz",   "Business software",
"comm",  "Communications",
"demo",  "Gfx and sound demos",
"dev",   "Development software",
"disk",  "Disk/Harddisk tools",
"docs",  "Text documents",
"game",  "Games",
"gfx",   "Graphics",
"hard",  "Hardware",
"info",  "General FTP information",
"misc",  "Miscellaneous",
"mods",  "Music modules",
"mus",   "Musical software",
"pix",   "Pictures",
"text",  "Text related",
"util",  "Utilities",
// biz (16)
"cloan", "Cloanto software & support",
"dbase", "Data base software",
"demo",  "Demo versions of commercial software",
"dkg",   "DarkAge software & support",
"dopus", "Directory Opus files",
"haage", "Haage & Partner software & support",
"misc",  "Misc business type soft",
"patch", "Commercial software patches",
"p5",    "phase5 software & support",
"swood", "Softwood products",
"titan", "Titan Computer software",
// comm (27)
"ambos", "AmBoS (BBS) related",
"amiex", "Ami-Express BBS related",
"bbs",   "BBS programs",
"cnet",  "CNET BBS related",
"dlg",   "DLG BBS related",
"fido",  "FIDO software",
"irc",   "Internet Relay Chat software",
"mail",  "Mailing soft",
"maxs",  "MaxsBBS related",
"mebbs", "MEBBSNet BBS related",
"misc",  "Misc communications",
"mmgr",  "MailManager related",
"net",   "Networking",
"news",  "Newsreaders",
"tcp",   "AmiTCP networking",
"term",  "Terminal programs",
"thor",  "Thor mail/news system",
"ums",   "Universal Message System",
"uucp",  "UUCP software",
"www",   "World Wide Web software",
"xeno",  "Xenolink BBS related",
// demo (48)
"40k",   "40K intros",
"aga",   "Demos requiring AGA chipset",
"bse",   "Belgian Scene Event files",
"disk",  "Demos on diskettes",
"ecs",   "Demos for the ECS chip set",
"euro",  "Euro demos in file form",
"file",  "Single file demos",
"funet", "From the Funet collection",
"intro", "Intros",
"mag",   "Demo coder magazines",
"mega",  "Euro demos requiring floppies",
"slide", "Image oriented demos",
"sound", "Sound oriented demos",
"sp96",  "Saturne Party 96 demos",
"ta95",  "Demos from The Assembly 95",
"ta96",  "Demos from The Assembly 96",
"ta97",  "Demos from The Assembly 97",
"tg93",  "The Gathering 1993 demos",
"tg95",  "The Gathering 1995 demos",
"tg96",  "The Gathering 1996 demos",
"tg97",  "The Gathering 1997 demos",
"tp92",  "Demos from The Party 92",
"tp94",  "Demos from The Party 94",
"tp95",  "Demos from The Party 95",
"tp96",  "Demos from The Party 96",
"track", "Trackmos",
// dev (74)
"amos",  "AMOS BASIC compiler",
"asm",   "Assembly language related",
"basic", "BASIC language related",
"c",     "C language related",
"cross", "Cross development",
"debug", "Debugging software",
"e",     "E language related",
"gcc",   "GNU C compiler",
"gg",    "Geek Gadgets GNU software",
"gui",   "GUI builders",
"lang",  "Various languages",
"m2",    "Modula-2 related",
"misc",  "Misc development software",
"moni",  "Monitors/disassemblers",
"mui",   "MagicUserInterface related",
"obero", "Oberon related software",
"src",   "Tutorial sources",
// disk (92)
"bakup", "Backup software",
"cache", "Disk caching",
"cdrom", "CD-ROM related software",
"misc",  "Various disk related tools",
"moni",  "Disk monitors",
"optim", "Disk optimizers",
"salv",  "Disk recovery software",
// docs (99)
"anno",  "Announcement documents",
"etext", "Project Gutenberg books",
"help",  "Help on various issues",
"hyper", "Hypertext documents",
"lists", "Lists and tables",
"mags",  "Magazines",
"misc",  "Various documents",
"rview", "Amiga product reviews",
// game (107)
"2play", "2 and more player games",
"actio", "Action games",
"board", "Board games",
"data",  "Data files for games",
"demo",  "Demos of commercial games",
"gag",   "Gag programs",
"hint",  "Game hint documents",
"jump",  "Jump-n-run games",
"misc",  "Miscellaneous games",
"patch", "Patches for games",
"role",  "Role, adventure games",
"shoot", "Shoot-em-up games",
"text",  "Text based adventures",
"think", "Mind games",
"wb",    "Workbench games",
// gfx  (122)
"3d",    "3D graphics software",
"3dobj", "3D objects",
"aga",   "AGA chip set software",
"board", "Custom gfx board software",
"conv",  "Gfx conversion tools",
"edit",  "Paint programs",
"fract", "Fractals and Mandelbrots",
"ifx",   "ImageFX related files",
"misc",  "Misc graphics software",
"pbm",   "Pbm+ gfx conversion",
"show",  "Image/anim displayers",
"x11",   "Amiga X-Windows",
// hard (134)
"drivr", "Drivers for hardware",
"hack",  "Selfmade hardware",
"misc",  "Misc hardware related files",
// info (137)
"adt",   "Machine readable filelists",
"index", "Various Aminet index files",
"sites", "Info on other FTP sites",
"start", "Beginner information",
"stats", "Statistics about this site",
// misc (142)
"amag",  "German Amiga Mag PD",
"antiq", "Nostalgic stuff",
"edu",   "Educational software",
"emu",   "Emulations",
"imdb",  "Internet Movie Database",
"kids",  "Children's software",
"math",  "Mathematics",
"misc",  "Uncategorizable",
"sci",   "Scientific software",
"unix",  "Amiga related UNIX software",
// mods (152)
"32bit", "MP3s by 32bit choonz",
"4mat",  "Mods by 4mat",
"8voic", "8-voiced songs",
"airon", "Mods by Airon",
"alpma", "Mods by Johan Alpmar",
"atmos", "Atmospheric music",
"aweso", "Mods by Dr Awesome",
"bhead", "Mods by Basehead",
"bladr", "Mods by Black Dragon",
"blk",   "Mods by Black Shadow",
"blkha", "Mods by Blakkhar",
"boing", "Boing collection",
"boray", "Mods by Boray",
"casef", "Mods by Case and Filippetto",
"cato",  "Mods by Don Cato",
"cels",  "Mods by Celsius",
"chart", "Module hits",
"chip",  "Mods without samples",
"chrom", "Mods by Chromag",
"chryl", "Mods by Chrylian",
"clawz", "Mods by Clawz",
"cloud", "Mods by Cloud",
"crash", "Mods by Crash Noise Division",
"crn",   "Mods by Corrosion",
"ctp",   "Mods by Counterpoint",
"cutc",  "Mods by Cutcreator",
"darde", "Mods by Fredius Darde",
"dean",  "Mods by Dean",
"delor", "Mods by Delorean",
"demo",  "Demo style music",
"dizzy", "Mods by Dizzy",
"dlusn", "Mods by D.Lusion",
"doh",   "Mods by Doh",
"dremr", "Mods by Dreamer",
"dsx",   "Mods by DSX",
"elbie", "Mods by Elbie",
"emax",  "Mods by Emax",
"ephnx", "Mods by Euphonix",
"evrim", "Mods by Evrimsson",
"exprt", "Mods by Expert",
"facet", "Mods by Facet",
"fby",   "Mods by Fabio Barzagli",
"fermx", "Mods by Fermix",
"fox2",  "Mods by TheFoxII",
"funet", "Funet collection",
"funk",  "Funky/jazzy mods",
"hardc", "Hardcore techno",
"hje",   "Mods by H.J. Ehrentraud",
"huezo", "Mods by Huezo",
"house", "House modules",
"hw",    "Mods by Hillware",
"inst",  "Instruments for modules",
"instr", "Instrumental music",
"jogei", "Mods by Jogeir Liljedahl",
"jorma", "Mods by Da Jormas (techno)",
"jungl", "Jungle/Breakbeat mods",
"k4k",   "Mods by k4k",
"kaa",   "Mods by Redd Kaa",
"lead",  "Mods by Lead",
"mark",  "Mods by Mark Salud",
"maxym", "Mods by Maxym",
"med",   "MED modules",
"melod", "Melodic modules",
"melom", "Mods by MeloManiac",
"midi",  "Music in MIDI format",
"misc",  "Miscellaneous songs",
"mnc",   "Mods by Meanace",
"mpg",   "Music in MPEG audio format",
"mryo",  "Mods by Mr. Yo",
"mvp",   "Mods by MoPlayaz",
"ncase", "Mods by Nutcase",
"neuro", "Neurodancer's modules",
"nork",  "Mods by Nork",
"ooze",  "Mods by Ooze",
"otis",  "Mods by Otis",
"panik", "Mods by Panik",
"pdy",   "Mods by Prodigy",
"pete",  "Mods by Pirate Pete",
"piano", "Piano mods",
"pop",   "Pop music",
"pro",   "Protracker songs",
"przk",  "Mods by Parsec",
"punn",  "Mods by Punnik",
"purg",  "Mods by Purgatory",
"rated", "Misc rated modules",
"rebel", "Mods by Rebel Riffs",
"rock",  "Rock'n'Roll",
"roz",   "Mods by Roz",
"s3m",   "Multichannel s3m mods",
"sbc",   "Modules by Substance",
"sets",  "Module collections",
"sidew", "Mods by Sidewinder",
"slc",   "Mods by Slice",
"slow",  "Slow, melodic modules",
"smg",   "Modes by esemgy",
"smpl",  "Sound samples",
"sonor", "Mods by Sonority",
"spark", "Mods by P.D. Spark",
"stame", "Mods by Stamen",
"synth", "Synthesizer modules",
"techn", "Techno music",
"toady", "Mods by Olivier Gonneau",
"tranc", "Trance techno mods",
"tp96",  "Mods from The Party 1996",
"uns",   "Mods by UnIsOn",
"voice", "Music with voices",
"voy",   "Mods by Voyager",
"wmr",   "Mods reviewed by WMR",
"xceed", "Mods by X-Ceed (ex Snoopy)",
"xm",    "Multichannel xm mods",
// mus (259)
"edit",  "Composing software",
"midi",  "MIDI software",
"misc",  "Miscellaneous",
"play",  "Sound players",
// pix (263)
"3dani", "3D animations",
"addic", "Pictures by 3DAddict",
"anim",  "Animations",
"art",   "Hand drawn imagery",
"astro", "Astronomy pictures",
"back",  "Workbench backdrop pictures",
"bill",  "Bill Graham traces",
"boot",  "Boot pictures",
"clip",  "Clip art",
"eps",   "Structured EPS images",
"eric",  "Eric Schwartz anims",
"fauna", "Animals",
"fract", "Fractals",
"gicon", "GlowIcons",
"glenn", "Traces by Glenn J. Schworak",
"guard", "Guardians traces",
"heiko", "Traces by Heiko Popp",
"icon",  "Workbench icons",
"illu",  "Illustrations",
"imagi", "Imagine traces",
"irc",   "The people on IRC",
"jake",  "The Ragtime Gallery",
"jason", "Jason Brown illustration",
"map",   "Images of maps",
"mark",  "Traces by Markus Kuhnen",
"misc",  "Miscellaneous images",
"mpg",   "MPEG animations",
"mwb",   "MagicWB icons",
"nevil", "Pics by Neville Rolle",
"nicon", "NewIcons",
"park",  "Maps of US national parks",
"real3", "Real3D traces",
"sport", "Sports pictures",
"textu", "Textures",
"tp96",  "Pics from The Party 1996",
"trace", "Raytraced pictures",
"unrl",  "Animations by Unreal",
"vehic", "Vehicle images",
"views", "Scenery, countries",
"wb",    "Example WB screens",
"wfm",   "Artwork of William F. Maddock",
// text (303)
"bfont", "Bitmap font collection",
"dtp",   "Desk top publishing",
"edit",  "Editors",
"font",  "Fonts of all formats and related too", // ..."ls"
"hyper", "Hypertext tools",
"ifont", "Intellifont collection",
"misc",  "Misc text software",
"pfont", "Postscript font collection",
"print", "Printing tools",
"show",  "Text displayers",
"tex",   "TeX formatting package",
// util (314)
"app",   "Appicons, appwindows",
"arc",   "Archivers",
"batch", "Script software",
"blank", "Screen blankers",
"boot",  "Startup sequence software",
"cdity", "Commodity type software",
"cli",   "Command line interface",
"conv",  "File conversion",
"crypt", "Encryption tools",
"dir",   "Directory utilities",
"dtype", "Datatypes for OS 3.x",
"gnu",   "GNU utilities",
"libs",  "Libraries",
"misc",  "Miscellaneous",
"moni",  "System monitors",
"mouse", "Mouse accelerators",
"pack",  "Packers",
"rexx",  "ARexx tools",
"shell", "Shells",
"sys",   "Common file replacements",
"time",  "Clocks and timing",
"virus", "Virus checkers",
"wb",    "Workbench utilities"
};

AGLOBAL void aminet1(void)
{   ULONG i, j, temp;
    // easiest to use SWORDs as they are the standard coordinate system

    for (i = 0; i <= MAXELEMENTS; i++)
        textBuffer[i] = NULL;

    /* PAGE 21 ************************************************************ */

    navigate();

    /* output */
    if (!(shared.output[0]))
        strcpy(shared.output, "RAM:.readme");
    setgadget(150, 22, 320, 12, "_Output pathname:", NULL);
    ST99_Output = PrevGadPtr = (struct Gadget *) CreateGadget
    (   STRING_KIND,
        PrevGadPtr,
        &Gadget,
        GTST_String, &(shared.output),
        GTST_MaxChars, VLONGFIELD,
        GA_TabCycle, TRUE,
        GA_Immediate, TRUE,
        GT_Underscore, '_',
        TAG_DONE
    );
    /* output... */
    setgadget(472, 22, 28, 12, "_...", NULL);
    BU99_OutputASL = PrevGadPtr = (struct Gadget *) CreateGadget
    (   BUTTON_KIND,
        PrevGadPtr,
        &Gadget,
        GT_Underscore, '_',
        TAG_DONE
    );

        setgadget(150, 46, 350, 12, "_Short:", NULL);
        ST21_Short = PrevGadPtr = (struct Gadget *) CreateGadget
	(	STRING_KIND,
		PrevGadPtr,
		&Gadget,
                GTST_String, &(aminet.amishort),
		GTST_MaxChars, 40,
		GA_TabCycle, TRUE,
		GA_Immediate, TRUE,
                GT_Underscore, '_',
		TAG_DONE
	);

        setgadget(150, 58, 350, 12, "_Uploader:", NULL);
        ST21_Uploader = PrevGadPtr = (struct Gadget *) CreateGadget
	(	STRING_KIND,
		PrevGadPtr,
		&Gadget,
                GTST_String, &(aminet.uploader),
		GTST_MaxChars, 40,
		GA_TabCycle, TRUE,
		GA_Immediate, TRUE,
                GT_Underscore, '_',
		TAG_DONE
	);

        setgadget(150, 70, 350, 12, "_Author:", NULL);
        ST21_Author = PrevGadPtr = (struct Gadget *) CreateGadget
	(	STRING_KIND,
		PrevGadPtr,
		&Gadget,
                GTST_String, &(aminet.author),
		GTST_MaxChars, 40,
		GA_TabCycle, TRUE,
		GA_Immediate, TRUE,
                GT_Underscore, '_',
		TAG_DONE
	);

        setgadget(150, 82, 350, 12, "_Replaces:", NULL);
        ST21_Replaces = PrevGadPtr = (struct Gadget *) CreateGadget
	(	STRING_KIND,
		PrevGadPtr,
		&Gadget,
                GTST_String, &(aminet.replaces),
		GTST_MaxChars, 40,
		GA_TabCycle, TRUE,
		GA_Immediate, TRUE,
                GT_Underscore, '_',
		TAG_DONE
	);

        setgadget(150, 94, 350, 12, "R_equires:", NULL);
        ST21_Requires = PrevGadPtr = (struct Gadget *) CreateGadget
	(	STRING_KIND,
		PrevGadPtr,
		&Gadget,
                GTST_String, &(aminet.requires),
		GTST_MaxChars, 40,
		GA_TabCycle, TRUE,
		GA_Immediate, TRUE,
                GT_Underscore, '_',
		TAG_DONE
	);

        setgadget(150, 106, 350, 12, "_Version:", NULL);
        ST21_Version = PrevGadPtr = (struct Gadget *) CreateGadget
	(	STRING_KIND,
		PrevGadPtr,
		&Gadget,
                GTST_String, &(aminet.version),
		GTST_MaxChars, 40,
		GA_TabCycle, TRUE,
		GA_Immediate, TRUE,
                GT_Underscore, '_',
		TAG_DONE
	);

        setgadget(150, 118, 350, 12, "_Distribution:", NULL);
        ST21_Distribution = PrevGadPtr = (struct Gadget *) CreateGadget
	(	STRING_KIND,
		PrevGadPtr,
		&Gadget,
                GTST_String, &(aminet.distribution),
		GTST_MaxChars, 40,
		GA_TabCycle, TRUE,
		GA_Immediate, TRUE,
                GT_Underscore, '_',
		TAG_DONE
	);

        if (!(MainTypeListPtr = AllocMem(sizeof(struct List), MEMF_CLEAR)))
            rq("Out of memory!");
        NewList(MainTypeListPtr);
        for (i = 0; i <= MAINTYPES; i++)
        {   strcpy(string, category[i].name);
            temp = 4 - strlen(category[i].name);
            for (j = 1; j <= temp; j++)
                strcat(string, " ");
            strcat(string, " ");
            strcat(string, category[i].desc);
            AddNameToTail(MainTypeListPtr, string);
        }
        setgadget(16, 154, 238, 74, "Directory:", NULL);
        LV21_MainType = PrevGadPtr = (struct Gadget *) CreateGadget
        (   LISTVIEW_KIND,
            PrevGadPtr,
            &Gadget,
            GTLV_Labels, MainTypeListPtr,
            GTLV_ShowSelected, NULL,
            GTLV_Selected, aminet.maintype,
            TAG_DONE
        );

        if (!(SubTypeListPtr = AllocMem(sizeof(struct List), MEMF_CLEAR)))
            rq("Out of memory!");
        NewList(SubTypeListPtr);
        for (i = offset[aminet.maintype].start; i <= offset[aminet.maintype].end; i++)
        {   strcpy(string, category[i].name);
            temp = 5 - strlen(category[i].name);
            for (j = 1; j <= temp; j++)
                strcat(string, " ");
            strcat(string, " ");
            strcat(string, category[i].desc);
            AddNameToTail(SubTypeListPtr, string);
        }
        setgadget(270, 154, 354, 74, "Subdirectory:", NULL);
        LV21_SubType = PrevGadPtr = (struct Gadget *) CreateGadget
        (   LISTVIEW_KIND,
            PrevGadPtr,
            &Gadget,
            GTLV_Labels, SubTypeListPtr,
            GTLV_ShowSelected, NULL,
            GTLV_Selected, aminet.subtype,
            TAG_DONE
        );

	newwindow
        (   640, AMINET1HEIGHT,
            "Report+: Aminet Readme Details",
            BUTTONIDCMP | STRINGIDCMP | LISTVIEWIDCMP
	);
        ActivateGadget(ST99_Output, MainWindowPtr, NULL);
	loop();
	strcpy
        (       shared.output,
                ((struct StringInfo *) ST99_Output->SpecialInfo)->Buffer
	);
	strcpy
        (       aminet.amishort,
                ((struct StringInfo *) ST21_Short->SpecialInfo)->Buffer
	);
	strcpy
        (       aminet.uploader,
                ((struct StringInfo *) ST21_Uploader->SpecialInfo)->Buffer
	);
	strcpy
        (       aminet.author,
                ((struct StringInfo *) ST21_Author->SpecialInfo)->Buffer
	);
	strcpy
        (       aminet.replaces,
                ((struct StringInfo *) ST21_Replaces->SpecialInfo)->Buffer
	);
	strcpy
        (       aminet.requires,
                ((struct StringInfo *) ST21_Requires->SpecialInfo)->Buffer
	);
	strcpy
        (       aminet.version,
                ((struct StringInfo *) ST21_Version->SpecialInfo)->Buffer
	);
	strcpy
        (       aminet.distribution,
                ((struct StringInfo *) ST21_Distribution->SpecialInfo)->Buffer
	);
    strcpy(aminet.type, category[aminet.maintype].name);
    strcat(aminet.type, "/");
    strcat(aminet.type, category[offset[aminet.maintype].start + aminet.subtype].name);

    closewindow();
    // now that the window is closed we can free lists

    aminet_exit();
}

AGLOBAL void aminet2(void)
{       navigate();
        newwindow
        (   640, AMINET2HEIGHT,
            "Report+: Finished Aminet Readme",
            BUTTONIDCMP
	);

	/*	Short:        <short>
		Uploader:     <uploader>
		Author:       <author>
		Type:         <type>
		Replaces:     <replaces>
		Requires:     <requires>
		Version:      <version>
		Distribution: <distribution>
		
		<description> */

	strcpy(IOBuffer, "Short:        ");
        strcat(IOBuffer, aminet.amishort);
	strcat(IOBuffer, "\n");
        if (aminet.uploader[0])
	{	strcat(IOBuffer, "Uploader:     ");
                strcat(IOBuffer, aminet.uploader);
		strcat(IOBuffer, "\n");
	}
        if (aminet.author[0])
        {   strcat(IOBuffer, "Author:       ");
            strcat(IOBuffer, aminet.author);
            strcat(IOBuffer, "\n");
	}
        // it's now compulsory to select a type
        {   strcat(IOBuffer, "Type:         ");
            strcat(IOBuffer, aminet.type);
            strcat(IOBuffer, "\n");
	}
    if (aminet.replaces[0])
    {   strcat(IOBuffer, "Replaces:     ");
        strcat(IOBuffer, aminet.replaces);
        strcat(IOBuffer, "\n");
    }
    if (aminet.requires[0])
    {   strcat(IOBuffer, "Requires:     ");
        strcat(IOBuffer, aminet.requires);
        strcat(IOBuffer, "\n");
    }
    if (aminet.version[0])
    {   strcat(IOBuffer, "Version:      ");
        strcat(IOBuffer, aminet.version);
        strcat(IOBuffer, "\n");
    }
    if (aminet.distribution[0])
    {   strcat(IOBuffer, "Distribution: ");
        strcat(IOBuffer, aminet.distribution);
        strcat(IOBuffer, "\n");
    }
    strcat(IOBuffer, "\n");

    if (wbval < 44)
        writeout(shared.output);
    launcheditor();
    if (wbval >= 44)
    {   strcat(IOBuffer, textBuffer[0]);
        writeout(shared.output);
    }
    aminet_exit();
    strcpy(string, "Completed readme is ");
    strcat(string, shared.output);
    strcat(string, ".");
    say(string, WHITE);
    say("Upload this readme to Aminet with your submission.", WHITE);
    loop();
    closewindow();
}

MODULE void aminet_exit(void)
{   if (SubTypeListPtr)
    {   FreeNameNodes(SubTypeListPtr);
        FreeMem(SubTypeListPtr, sizeof(struct List));
        SubTypeListPtr = NULL;
    }
    if (MainTypeListPtr)
    {   FreeNameNodes(MainTypeListPtr);
        FreeMem(MainTypeListPtr, sizeof(struct List));
        MainTypeListPtr = NULL;
    }
    if (textBuffer[0])
    {   FreeVec((APTR) textBuffer[0]);
        textBuffer[0] = NULL;
}   }

AGLOBAL void aminet_loop(ULONG class, struct Gadget* addr, UWORD code)
{   ULONG i, j, temp;

    if (page == 21)
    {   if (class == IDCMP_INTUITICKS)
        {   if (((struct StringInfo *) ST21_Short->SpecialInfo)->Buffer[0])
            {   GT_SetGadgetAttrs
                (   BU99_Right,
                    MainWindowPtr, NULL,
                    GA_Disabled, FALSE,
                    TAG_DONE
                );
            } else
            {   GT_SetGadgetAttrs
                (   BU99_Right,
                    MainWindowPtr, NULL,
                    GA_Disabled, TRUE,
                    TAG_DONE
                );
        }   }
        elif (class == IDCMP_GADGETUP)
        {   if (addr == BU99_Left)
            {   page = 0;
            } elif (addr == BU99_Right)
            {   page = 22;
            } elif (addr == BU99_OutputASL)
            {   asl();
                GT_SetGadgetAttrs
                (   ST99_Output,
                    MainWindowPtr,
                    NULL,
                    GTST_String, aslresult,
                    TAG_DONE
                );
            } elif (addr == LV21_MainType)
            {   /* code is the position within the list, starting from 0. */
                aminet.maintype = code;
                aminet.subtype  = 0;

                // detach sublistview gadget
                GT_SetGadgetAttrs
                (   LV21_SubType,
                    MainWindowPtr, NULL,
                    GTLV_Labels, (UWORD) ~0,
                    TAG_DONE
                );

                // free the current sublist
                FreeNameNodes(SubTypeListPtr);

                // now set up a new sublist
                NewList(SubTypeListPtr);
                for (i = offset[aminet.maintype].start; i <= offset[aminet.maintype].end; i++)
                {   strcpy(string, category[i].name);
                    temp = 5 - strlen(category[i].name);
                    for (j = 1; j <= temp; j++)
                        strcat(string, " ");
                    strcat(string, " ");
                    strcat(string, category[i].desc);
                    AddNameToTail(SubTypeListPtr, string);
                }
                GT_SetGadgetAttrs
                (   LV21_SubType,
                    MainWindowPtr, NULL,
                    GTLV_Labels, SubTypeListPtr,
                    TAG_DONE
                );
            } elif (addr == LV21_SubType)
            {   aminet.subtype = code;
        }   }
        elif (class == IDCMP_VANILLAKEY)
        {   code = toupper(code);
            if (code == ESCAPE)
                page = 0;
            elif (code == '.')
            {   asl();
                GT_SetGadgetAttrs
                (   ST99_Output,
                    MainWindowPtr,
                    NULL,
                    GTST_String, aslresult,
                    TAG_DONE
                );
            } elif (code == 'O')
            {   ActivateGadget(ST99_Output, MainWindowPtr, NULL);
            } elif (code == 'S')
            {   ActivateGadget(ST21_Short, MainWindowPtr, NULL);
            } elif (code == 'A')
            {   ActivateGadget(ST21_Author, MainWindowPtr, NULL);
            } elif (code == 'U')
            {   ActivateGadget(ST21_Uploader, MainWindowPtr, NULL);
            } elif (code == 'R')
            {   ActivateGadget(ST21_Replaces, MainWindowPtr, NULL);
            } elif (code == 'E')
            {   ActivateGadget(ST21_Requires, MainWindowPtr, NULL);
            } elif (code == 'V')
            {   ActivateGadget(ST21_Version, MainWindowPtr, NULL);
            } elif (code == 'D')
            {   ActivateGadget(ST21_Distribution, MainWindowPtr, NULL);
    }   }   }
    else
    {   // assert(page == 22);
        if (class == IDCMP_GADGETUP)
        {   if (addr == BU99_Right)
            {   page = 0;
        }   }
        elif (class == IDCMP_VANILLAKEY)
        {   if (code == ESCAPE)
            {   page = 0;
}   }   }   }
