#include <exec/types.h>
#include <exec/memory.h>
#include <clib/exec_protos.h>
#include <intuition/intuition.h>
#include <clib/intuition_protos.h>
#include <libraries/gadtools.h>
#include <clib/gadtools_protos.h>
#include <graphics/gfx.h>
#include <clib/graphics_protos.h>

#include <clib/alib_protos.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include "rp.h"

#define ALL_REACTION_CLASSES
#define ALL_REACTION_MACROS
#include <reaction/reaction.h>
#include <gadgets/texteditor.h>
#include <clib/texteditor_protos.h>
#include <pragmas/texteditor_pragmas.h>
#define ALL_REACTION_CLASSES
#include <reaction/reaction.h>

#define MAINTYPES   15
#define CATEGORIES 341
struct
{   ULONG start, end;
} offsets[MAINTYPES + 1] =
{   16,  26, // biz
    27,  47, // comm
    48,  73, // demo
    74,  90, // dev
    91,  97, // disk
    98, 105, // docs
   106, 120, // game
   121, 132, // gfx
   133, 135, // hard
   136, 140, // info
   141, 150, // misc
   151, 261, // mods
   262, 265, // mus
   266, 307, // pix
   308, 318, // text
   319, 341  // util
};

AGLOBAL struct AminetStruct aminet;

// from rp.c
IMPORT ABOOL                fillwindows;
IMPORT SBYTE                page;
IMPORT TEXT                 IOBuffer[LONGESTFIELD + 1],
                            aslresult[MEDFIELD + 1],
                            string[VLONGFIELD + 1];
IMPORT ULONG                offset;
IMPORT struct NewGadget     Gadget;
IMPORT struct SharedStruct  shared;
IMPORT struct Window*       MainWindowPtr;
IMPORT struct Library*      TextEditorBase;
IMPORT Object*              WinObject[FUNCTIONS + 1]; // note that WindowObject is a reserved macro
IMPORT struct Menu*         MenuPtr;
IMPORT struct Screen*       ScreenPtr;

MODULE void updateaminetgadgets(void);
MODULE void readaminetgadgets(void);
MODULE void updatesubtype(void);

MODULE ABOOL          MainTypeNodes     = FALSE,
                      SubTypeNodes      = FALSE;
MODULE struct List    MainTypeList,
                      SubTypeList,
                      EmptySubTypeList;

MODULE struct Gadget *aminet_gadgets[GIDS_2 + 1];

MODULE struct
{   STRPTR name;
    STRPTR desc;
} 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",
"racoo", "Mods by Racoon", // new for 5.02
"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",
"henz",  "Traces by Patrick Henz", // new for 5.02
"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)
{   struct Hook Hook2Struct;

    if ((!(aminet.uploader[0])) && readin("S:Report.sender"))
    {   /* # Refer: <last>,<first> (<company> ,phone <phone>)<LF>
           # Path: <email><LF>
           # ReferID: <developer><LF> */

        offset = 0;
        parse("# Refer: ");
        parse(",");
        parse(" (");
        parse(" ,phone ");
        parse(")\n# Path: ");
        parse("\n# ReferID: ");
        strcpy(aminet.uploader, string);
    }

    /* Create the window object. */
    lockscreen();
    gadtools();
    InitHook(&Hook2Struct, Hook2Func, NULL);

if (!(WinObject[2] =
NewObject
(   WINDOW_GetClass(),                    NULL,
    WA_PubScreen,                         ScreenPtr,
    WA_ScreenTitle,                       "Report+",
    WA_Title,                             "Report+: Aminet Readme Editor",
    WA_Activate,                          TRUE,
    WA_DepthGadget,                       TRUE,
    WA_DragBar,                           TRUE,
    WA_CloseGadget,                       TRUE,
    WA_SizeGadget,                        TRUE,
    WA_IDCMP,                             IDCMP_RAWKEY,
    WINDOW_IDCMPHook,                     &Hook2Struct,
    WINDOW_IDCMPHookBits,                 IDCMP_RAWKEY,
    WINDOW_MenuStrip,                     MenuPtr,
    WINDOW_Position,                      WPOS_FULLSCREEN,
    WINDOW_ParentGroup,                   aminet_gadgets[GID_2_LY1] =
    NewObject
    (   LAYOUT_GetClass(),                NULL,
        LAYOUT_Orientation,               LAYOUT_ORIENT_VERT,
        LAYOUT_SpaceOuter,                TRUE,
        LAYOUT_DeferLayout,               TRUE,
        LAYOUT_AddChild,
        NewObject
        (   LAYOUT_GetClass(),            NULL,
            LAYOUT_Orientation,           LAYOUT_ORIENT_HORIZ,
            LAYOUT_SpaceOuter,            TRUE,
            LAYOUT_DeferLayout,           TRUE,
            LAYOUT_BevelStyle,            BVS_FIELD,
            LAYOUT_AddChild,
            NewObject
            (   LAYOUT_GetClass(),        NULL,
                LAYOUT_Orientation,       LAYOUT_ORIENT_VERT,
                LAYOUT_SpaceOuter,        TRUE,
                LAYOUT_VertAlignment,     LALIGN_CENTER,
                LAYOUT_BevelStyle,        BVS_NONE,
                LAYOUT_AddChild,
                NewObject
                (   LAYOUT_GetClass(),    NULL,
                    LAYOUT_Orientation,   LAYOUT_ORIENT_HORIZ,
                    LAYOUT_SpaceOuter,    TRUE,
                    LAYOUT_HorizAlignment,LALIGN_RIGHT,
                    LAYOUT_BevelStyle,    BVS_FIELD,
                    LAYOUT_AddImage,
                    NewObject
                    (   LABEL_GetClass(),     NULL,
                        LABEL_Text,           "_Short:",
                        LABEL_Justification,  LJ_RIGHT,
                    TAG_END),
                TAG_END),
                LAYOUT_AddChild,
                NewObject
                (   LAYOUT_GetClass(), NULL,
                    LAYOUT_Orientation,       LAYOUT_ORIENT_HORIZ,
                    LAYOUT_SpaceOuter,        TRUE,
                    LAYOUT_HorizAlignment,    LALIGN_RIGHT,
                    LAYOUT_BevelStyle,        BVS_FIELD,
                    LAYOUT_AddImage,
                    NewObject
                    (   LABEL_GetClass(),     NULL,
                        LABEL_Text,           "_Uploader:",
                        LABEL_Justification,  LJ_RIGHT,
                    TAG_END),
                TAG_END),
                LAYOUT_AddChild,
                NewObject
                (   LAYOUT_GetClass(),        NULL,
                    LAYOUT_Orientation,       LAYOUT_ORIENT_HORIZ,
                    LAYOUT_SpaceOuter,        TRUE,
                    LAYOUT_HorizAlignment,    LALIGN_RIGHT,
                    LAYOUT_BevelStyle,        BVS_FIELD,
                    LAYOUT_AddImage,          NewObject
                    (   LABEL_GetClass(),     NULL,
                        LABEL_Text,           "_Author:",
                        LABEL_Justification,  LJ_RIGHT,
                    TAG_END),
                TAG_END),
                LAYOUT_AddChild,
                NewObject
                (   LAYOUT_GetClass(),        NULL,
                    LAYOUT_Orientation,       LAYOUT_ORIENT_HORIZ,
                    LAYOUT_SpaceOuter,        TRUE,
                    LAYOUT_HorizAlignment,    LALIGN_RIGHT,
                    LAYOUT_BevelStyle,        BVS_FIELD,
                    LAYOUT_AddImage,
                    NewObject
                    (   LABEL_GetClass(),     NULL,
                        LABEL_Text,           "_Replaces:",
                        LABEL_Justification,  LJ_RIGHT,
                    TAG_END),
                TAG_END),
                LAYOUT_AddChild,
                NewObject
                (   LAYOUT_GetClass(),        NULL,
                    LAYOUT_Orientation,       LAYOUT_ORIENT_HORIZ,
                    LAYOUT_SpaceOuter,        TRUE,
                    LAYOUT_HorizAlignment,    LALIGN_RIGHT,
                    LAYOUT_BevelStyle,        BVS_FIELD,
                    LAYOUT_AddImage,
                    NewObject
                    (   LABEL_GetClass(),     NULL,
                        LABEL_Text,           "R_equires:",
                        LABEL_Justification,  LJ_RIGHT,
                    TAG_END),
                TAG_END),
                LAYOUT_AddChild,
                NewObject
                (   LAYOUT_GetClass(),        NULL,
                    LAYOUT_Orientation,       LAYOUT_ORIENT_HORIZ,
                    LAYOUT_SpaceOuter,        TRUE,
                    LAYOUT_HorizAlignment,    LALIGN_RIGHT,
                    LAYOUT_BevelStyle,        BVS_FIELD,
                    LAYOUT_AddImage,
                    NewObject
                    (   LABEL_GetClass(),     NULL,
                        LABEL_Text,           "_Version:",
                        LABEL_Justification,  LJ_RIGHT,
                    TAG_END),
                TAG_END),
                LAYOUT_AddChild,
                NewObject
                (   LAYOUT_GetClass(),        NULL,
                    LAYOUT_Orientation,       LAYOUT_ORIENT_HORIZ,
                    LAYOUT_SpaceOuter,        TRUE,
                    LAYOUT_HorizAlignment,    LALIGN_RIGHT,
                    LAYOUT_BevelStyle,        BVS_FIELD,
                    LAYOUT_AddImage,
                    NewObject
                    (   LABEL_GetClass(),     NULL,
                        LABEL_Text,           "_Distribution:",
                        LABEL_Justification,  LJ_RIGHT,
                    TAG_END),
                TAG_END),
            TAG_END),
            CHILD_WeightedWidth,              0,
            LAYOUT_AddChild,
            NewObject
            (   LAYOUT_GetClass(),    NULL,
                LAYOUT_Orientation,   LAYOUT_ORIENT_VERT,
                LAYOUT_SpaceOuter,    TRUE,
                LAYOUT_VertAlignment, LALIGN_CENTER,
                LAYOUT_HorizAlignment,LALIGN_CENTER,
                LAYOUT_BevelStyle,    BVS_NONE,
                LAYOUT_AddChild,      aminet_gadgets[GID_2_ST1] =
                NewObject
                (   STRING_GetClass(),    NULL,
                    GA_ID,                GID_2_ST1,
                    GA_TabCycle,          TRUE,
                    STRINGA_TextVal,      aminet.amishort,
                    STRINGA_MinVisible,   20,
                    STRINGA_MaxChars,     40,
                TAG_END),
                LAYOUT_AddChild,          aminet_gadgets[GID_2_ST2] =
                NewObject
                (   STRING_GetClass(),    NULL,
                    GA_ID,                GID_2_ST2,
                    GA_TabCycle,          TRUE,
                    STRINGA_TextVal,      aminet.uploader,
                    STRINGA_MinVisible,   20,
                    STRINGA_MaxChars,     40,
                TAG_END),
                LAYOUT_AddChild,          aminet_gadgets[GID_2_ST3] =
                NewObject
                (   STRING_GetClass(),    NULL,
                    GA_ID,                GID_2_ST3,
                    GA_TabCycle,          TRUE,
                    STRINGA_TextVal,      aminet.author,
                    STRINGA_MinVisible,   20,
                    STRINGA_MaxChars,     40,
                TAG_END),
                LAYOUT_AddChild,          aminet_gadgets[GID_2_ST4] =
                NewObject
                (   STRING_GetClass(),    NULL,
                    GA_ID,                GID_2_ST4,
                    GA_TabCycle,          TRUE,
                    STRINGA_TextVal,      aminet.version,
                    STRINGA_MinVisible,   20,
                    STRINGA_MaxChars,     40,
                TAG_END),
                LAYOUT_AddChild,          aminet_gadgets[GID_2_ST5] =
                NewObject
                (   STRING_GetClass(),    NULL,
                    GA_ID,                GID_2_ST5,
                    GA_TabCycle,          TRUE,
                    STRINGA_TextVal,      aminet.replaces,
                    STRINGA_MinVisible,   20,
                    STRINGA_MaxChars,     40,
                TAG_END),
                LAYOUT_AddChild,          aminet_gadgets[GID_2_ST6] =
                NewObject
                (   STRING_GetClass(),    NULL,
                    GA_ID,                GID_2_ST6,
                    GA_TabCycle,          TRUE,
                    STRINGA_TextVal,      aminet.requires,
                    STRINGA_MinVisible,   20,
                    STRINGA_MaxChars,     40,
                TAG_END),
                LAYOUT_AddChild,          aminet_gadgets[GID_2_ST7] =
                NewObject
                (   STRING_GetClass(),    NULL,
                    GA_ID,                GID_2_ST7,
                    GA_TabCycle,          TRUE,
                    STRINGA_TextVal,      aminet.distribution,
                    STRINGA_MinVisible,   20,
                    STRINGA_MaxChars,     40,
                TAG_END),
            TAG_END),
        TAG_END),
        CHILD_WeightedHeight,         0,
        LAYOUT_AddChild,
        NewObject
        (   LAYOUT_GetClass(),        NULL,
            LAYOUT_Orientation,       LAYOUT_ORIENT_HORIZ,
            LAYOUT_SpaceOuter,        TRUE,
            LAYOUT_HorizAlignment,    LALIGN_RIGHT,
            LAYOUT_BevelStyle,        BVS_NONE,
            LAYOUT_AddImage,
            NewObject
            (   LABEL_GetClass(),     NULL,
                LABEL_Text,           "_Long description:",
                LABEL_Justification,  LJ_CENTER,
            TAG_END),
        TAG_END),
        CHILD_WeightedHeight,         0,
        LAYOUT_AddChild,              aminet_gadgets[GID_2_TE1] =
        NewObject
        (   TEXTEDITOR_GetClass(),    NULL,
            GA_ID,                    GID_2_TE1,
            GA_TabCycle,              TRUE,
            GA_RelVerify,             TRUE,
            GA_TEXTEDITOR_ExportWrap, 75,
        TAG_END),
        CHILD_WeightedHeight,         70,
        LAYOUT_AddChild,
        NewObject
        (   LAYOUT_GetClass(),        NULL,
            LAYOUT_Orientation,       LAYOUT_ORIENT_HORIZ,
            LAYOUT_SpaceOuter,        TRUE,
            LAYOUT_HorizAlignment,    LALIGN_RIGHT,
            LAYOUT_BevelStyle,        BVS_NONE,
            LAYOUT_AddImage,
            NewObject
            (   LABEL_GetClass(),     NULL,
                LABEL_Text,           "Directory:",
                LABEL_Justification,  LJ_CENTER,
            TAG_END),
            CHILD_WeightedWidth,      50,
            LAYOUT_AddImage,
            NewObject
            (   LABEL_GetClass(),     NULL,
                LABEL_Text,           "Subdirectory:",
                LABEL_Justification,  LJ_CENTER,
            TAG_END),
            CHILD_WeightedWidth,      50,
        TAG_END),
        CHILD_WeightedHeight,         0,
        LAYOUT_AddChild,
        NewObject
        (   LAYOUT_GetClass(),        NULL,
            LAYOUT_Orientation,       LAYOUT_ORIENT_HORIZ,
            LAYOUT_SpaceOuter,        TRUE,
            LAYOUT_HorizAlignment,    LALIGN_CENTER,
            LAYOUT_BevelStyle,        BVS_FIELD,
            LAYOUT_AddChild,          aminet_gadgets[GID_2_LB1] =
            NewObject
            (   LISTBROWSER_GetClass(),NULL,
                GA_ID,                 GID_2_LB1,
                GA_RelVerify,          TRUE,
                GA_TextAttr,           Gadget.ng_TextAttr,
                LISTBROWSER_Labels,    (ULONG) &MainTypeList,
            TAG_END),
            LAYOUT_AddChild,           aminet_gadgets[GID_2_LB2] =
            NewObject
            (   LISTBROWSER_GetClass(),NULL,
                GA_ID,                 GID_2_LB2,
                GA_RelVerify,          TRUE,
                GA_TextAttr,           Gadget.ng_TextAttr,
                LISTBROWSER_Labels,    (ULONG) &EmptySubTypeList,
            TAG_END),
        TAG_END),
        CHILD_WeightedHeight,          30,
    TAG_END),
TAG_END)))
{   rq("Can't create ReAction gadgets!");
}
unlockscreen();
openwindow();

    SetGadgetAttrs
    (   aminet_gadgets[GID_2_TE1],    MainWindowPtr, NULL,
        GA_TEXTEDITOR_Contents, aminet.textfield
    ); // long description
    OnMenu(MainWindowPtr, FULLMENUNUM(MN_PROJECT, IN_NEW,    NOSUB));
    OnMenu(MainWindowPtr, FULLMENUNUM(MN_PROJECT, IN_OPEN,   NOSUB));
    OnMenu(MainWindowPtr, FULLMENUNUM(MN_PROJECT, IN_SAVE,   NOSUB));
    OnMenu(MainWindowPtr, FULLMENUNUM(MN_PROJECT, IN_SAVEAS, NOSUB));

    updatesubtype();
    ActivateLayoutGadget(aminet_gadgets[GID_2_LY1], MainWindowPtr, NULL, (Object) aminet_gadgets[GID_2_ST1]);
    loop();
    readaminetgadgets();
    closewindow();
    // now that the window is closed we can free lists
    aminet_exit();
}

AGLOBAL void saveaminet(ABOOL saveas)
{   /*  Short:        <short>
        Uploader:     <uploader>
        Author:       <author>
        Type:         <type>
        Replaces:     <replaces>
        Requires:     <requires>
        Version:      <version>
        Distribution: <distribution>
		
        <long description> */

    readaminetgadgets();

    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 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");
    strcat(IOBuffer, aminet.textfield);

    if (!aminet.output[0] || saveas) // if we need a filename
    {   if (saveasl("Save Aminet Readme", "#?.readme"))
        {   strcpy(aminet.output, aslresult);
            writeout(aminet.output);
    }   }
    writeout(aminet.output);
}

AGLOBAL void aminet_exit(void)
{   if (SubTypeNodes)
    {   clearlist(&SubTypeList);
        SubTypeNodes = FALSE;
}   }

AGLOBAL void aminet_loop(ULONG gid)
{   ULONG code;

    switch(gid)
    {
    case GID_2_LB1:
        /* code is the position within the list, starting from 0. */
        if (!(GetAttr
        (   LISTBROWSER_Selected, aminet_gadgets[GID_2_LB1], (ULONG *) &code
        )))
        {   rq("Unsupported inquiry!"); // should never happen
        }
        aminet.maintype = code;
        aminet.subtype  = 0;
        updatesubtype();
    break;
    case GID_2_LB2:
        /* code is the position within the list, starting from 0. */
        if (!(GetAttr
        (   LISTBROWSER_Selected, aminet_gadgets[GID_2_LB2], (ULONG *) &code
        )))
        {   rq("Unsupported inquiry!"); // should never happen
        }
        aminet.subtype = code;
    break;
    default:
    break;
}   }

AGLOBAL void newaminet(ABOOL display)
{   aminet.amishort[0]     =
    aminet.uploader[0]     =
    aminet.author[0]       =
    aminet.requires[0]     =
    aminet.distribution[0] =
    aminet.type[0]         =
    aminet.replaces[0]     =
    aminet.output[0]       =
    aminet.textfield[0]    =
    aminet.maintype        =
    aminet.subtype         = 0;
    strcpy(aminet.version, "1.0");

    if (display)
        updateaminetgadgets();
}

AGLOBAL void openaminet(void)
{   if (asl("#?.readme"))
    {   loadaminet();
        updateaminetgadgets();
}   }

AGLOBAL void loadaminet(void)
{   ABOOL done = FALSE;
    ULONG i, j, slashpos = 0;
    TEXT  tempstring[13];

    if (readin(aslresult))
    {   strcpy(aminet.output, aslresult);
        offset = 0;

        // Clear all fields.
        aminet.amishort[0]     =
        aminet.uploader[0]     =
        aminet.author[0]       =
        aminet.requires[0]     =
        aminet.distribution[0] =
        aminet.type[0]         =
        aminet.replaces[0]     =
        aminet.output[0]       =
        aminet.textfield[0]    =
        aminet.maintype        =
        aminet.subtype         =
        aminet.version[0]      = 0;

        do
        {   parse("\n");
            if (string[0] == 0 || (string[0] == CR && string[1] == 0)) // CR support is for IBM-PC compatibility
            {   done = TRUE;
                parsetoend();
                strcpy(aminet.textfield, string);
            } elif (!strncmp(string, "Short:",         6))
            {   for (i =  6; i < strlen(string); i++)
                {   if (string[i] != ' ')
                    {   strcpy(aminet.amishort,     &string[i]);
                        break;
            }   }   }
            elif (!strncmp(string, "Uploader:",      9))
            {   for (i =  9; i < strlen(string); i++)
                {   if (string[i] != ' ')
                    {   strcpy(aminet.uploader,     &string[i]);
                        break;
            }   }   }
            elif (!strncmp(string, "Author:",        7))
            {   for (i =  7; i < strlen(string); i++)
                {   if (string[i] != ' ')
                    {   strcpy(aminet.author,       &string[i]);
                        break;
            }   }   }
            elif (!strncmp(string, "Type:",          5))
            {   for (i =  5; i < strlen(string); i++)
                {   if (string[i] != ' ')
                    {   // OK, we are now at the start of useful data
                        strcpy(aminet.type, &string[i]);
                        while (aminet.type[slashpos] != '/')
                        {   slashpos++;
                        }
                        // slashpos is now offset of '/'
                        strncpy(tempstring, aminet.type, slashpos);                        tempstring[slashpos] = 0;
                        for (j = 0; j <= MAINTYPES; j++)
                        {   if (!strcmp(tempstring, category[j].name))
                            {   aminet.maintype = j; // set up aminet.maintype
                                break;
                        }   }
                        slashpos++; // move past '/'
                        for (j = offsets[aminet.maintype].start; j <= offsets[aminet.maintype].end; j++)
                        {   if (!strcmp(&aminet.type[slashpos], category[j].name))
                            {   aminet.subtype = j - offsets[aminet.maintype].start;
                                break;
                        }   }
                        break;
            }   }   }
            elif (!strncmp(string, "Replaces:",      9))
            {   for (i =  9; i < strlen(string); i++)
                {   if (string[i] != ' ')
                    {   strcpy(aminet.replaces,     &string[i]);
                        break;
            }   }   }
            elif (!strncmp(string, "Requires:",      9))
            {   for (i =  9; i < strlen(string); i++)
                {   if (string[i] != ' ')
                    {   strcpy(aminet.requires,     &string[i]);
                        break;
            }   }   }
            elif (!strncmp(string, "Version:",       8))
            {   for (i =  8; i < strlen(string); i++)
                {   if (string[i] != ' ')
                    {   strcpy(aminet.version,      &string[i]);
                        break;
            }   }   }
            elif (!strncmp(string, "Distribution:", 13))
            {   for (i = 13; i < strlen(string); i++)
                {   if (string[i] != ' ')
                    {   strcpy(aminet.distribution, &string[i]);
                        break;
        }   }   }   }
        while (!done);
}   }

MODULE void updateaminetgadgets(void)
{   SetGadgetAttrs
    (   aminet_gadgets[GID_2_ST1], MainWindowPtr, NULL,
        GTST_String, aminet.amishort,
        TAG_END
    );
    SetGadgetAttrs
    (   aminet_gadgets[GID_2_ST2], MainWindowPtr, NULL,
        GTST_String, aminet.uploader,
        TAG_END
    );
    SetGadgetAttrs
    (   aminet_gadgets[GID_2_ST3], MainWindowPtr, NULL,
        GTST_String, aminet.author,
        TAG_END
    );
    SetGadgetAttrs
    (   aminet_gadgets[GID_2_ST4], MainWindowPtr, NULL,
        GTST_String, aminet.version,
        TAG_END
    );
    SetGadgetAttrs
    (   aminet_gadgets[GID_2_ST5], MainWindowPtr, NULL,
        GTST_String, aminet.replaces,
        TAG_END
    );
    SetGadgetAttrs
    (   aminet_gadgets[GID_2_ST6], MainWindowPtr, NULL,
        GTST_String, aminet.requires,
        TAG_END
    );
    SetGadgetAttrs
    (   aminet_gadgets[GID_2_ST7], MainWindowPtr, NULL,
        GTST_String, aminet.distribution,
        TAG_END
    );
    SetGadgetAttrs // `Type'
    (   aminet_gadgets[GID_2_LB1],
        MainWindowPtr, NULL,
        LISTBROWSER_Selected, aminet.maintype,
        TAG_END
    );

    updatesubtype();

    SetGadgetAttrs
    (   aminet_gadgets[GID_2_TE1], MainWindowPtr, NULL,
        GA_TEXTEDITOR_Contents, aminet.textfield,
        TAG_DONE
    );
    // It would be polite to scroll the listviews so that the selected
    // entries are visible.
}
MODULE void readaminetgadgets(void)
{   STRPTR stringptr;

    if (!(GetAttr
    (   STRINGA_TextVal, aminet_gadgets[GID_2_ST1], (ULONG *) &stringptr
    )))
    {   rq("Unsupported inquiry!"); // should never happen
    }
    strcpy(aminet.amishort, stringptr);

    if (!(GetAttr
    (   STRINGA_TextVal, aminet_gadgets[GID_2_ST2], (ULONG *) &stringptr
    )))
    {   rq("Unsupported inquiry!"); // should never happen
    }
    strcpy(aminet.uploader, stringptr);

    if (!(GetAttr
    (   STRINGA_TextVal, aminet_gadgets[GID_2_ST3], (ULONG *) &stringptr
    )))
    {   rq("Unsupported inquiry!"); // should never happen
    }
    strcpy(aminet.author, stringptr);

    if (!(GetAttr
    (   STRINGA_TextVal, aminet_gadgets[GID_2_ST4], (ULONG *) &stringptr
    )))
    {   rq("Unsupported inquiry!"); // should never happen
    }
    strcpy(aminet.replaces, stringptr);

    if (!(GetAttr
    (   STRINGA_TextVal, aminet_gadgets[GID_2_ST5], (ULONG *) &stringptr
    )))
    {   rq("Unsupported inquiry!"); // should never happen
    }
    strcpy(aminet.requires, stringptr);

    if (!(GetAttr
    (   STRINGA_TextVal, aminet_gadgets[GID_2_ST6], (ULONG *) &stringptr
    )))
    {   rq("Unsupported inquiry!"); // should never happen
    }
    strcpy(aminet.version, stringptr);

    if (!(GetAttr
    (   STRINGA_TextVal, aminet_gadgets[GID_2_ST7], (ULONG *) &stringptr
    )))
    {   rq("Unsupported inquiry!"); // should never happen
    }
    strcpy(aminet.distribution, stringptr);

    strcpy(aminet.type, category[aminet.maintype].name);
    strcat(aminet.type, "/");
    strcat(aminet.type, category[offsets[aminet.maintype].start + aminet.subtype].name);

    stringptr = (STRPTR) DoGadgetMethod
    (   aminet_gadgets[GID_2_TE1], MainWindowPtr, NULL,
        GM_TEXTEDITOR_ExportText, NULL
    );
    strcpy(aminet.textfield, stringptr); // long description
    FreeVec((APTR) stringptr);
    // textBuffer = NULL;
}

AGLOBAL void aminet_init(void)
{   ULONG        i, j, temp;
    struct Node* ListBrowserNodePtr;

    NewList(&MainTypeList);
    MainTypeNodes = TRUE;

    for (i = 0; i <= MAINTYPES; i++)
    {   strcpy(string, category[i].name);
        temp = 5 - strlen(category[i].name);
        for (j = 1; j <= temp; j++)
            strcat(string, " ");
        strcat(string, category[i].desc);

        if (!(ListBrowserNodePtr = AllocListBrowserNode
        (    1,                   // columns,
             LBNA_Column,         0,
             LBNCA_CopyText,      TRUE,
             LBNCA_Text,          string,
             TAG_END
        )))
        {   rq("Can't create ReAction listbrowser.gadget node(s)!");
        }
        AddTail(&MainTypeList, ListBrowserNodePtr); // AddTail() has no return code
    }

    SetGadgetAttrs // `Type'
    (   aminet_gadgets[GID_2_LB1],
        MainWindowPtr, NULL,
        LISTBROWSER_Labels, &MainTypeList,
        LISTBROWSER_Selected, aminet.maintype,
        TAG_END
    );

    NewList(&SubTypeList);
    NewList(&EmptySubTypeList);
}

AGLOBAL void aminet_die(void)
{   if (MainTypeNodes)
    {   clearlist(&MainTypeList);
        MainTypeNodes = FALSE;
}   }

MODULE void updatesubtype(void)
{   ULONG        i, j, temp;
    struct Node* ListBrowserNodePtr;
     
    SetGadgetAttrs // `Subtype'
    (   aminet_gadgets[GID_2_LB2],
        MainWindowPtr, NULL,
        LISTBROWSER_Labels, NULL, // detach it
        TAG_END
    );

    if (SubTypeNodes)
    {   clearreactionlist(&SubTypeList);
    }

    SubTypeNodes = TRUE;
    for
    (   i = offsets[aminet.maintype].start;
        i <= offsets[aminet.maintype].end;
        i++
    )
    {   strcpy(string, category[i].name);
        temp = 6 - strlen(category[i].name);
        for (j = 1; j <= temp; j++)
            strcat(string, " ");
        strcat(string, category[i].desc);

        if (!(ListBrowserNodePtr = AllocListBrowserNode
        (    1,                   // columns,
             LBNCA_CopyText,      TRUE,
             LBNCA_Text,          string,
             TAG_END
        )))
        {   rq("Can't create ReAction listbrowser.gadget node(s)!");
        }
        AddTail(&SubTypeList, ListBrowserNodePtr); // AddTail() has no return code
    }

    SetGadgetAttrs // `Subtype'
    (   aminet_gadgets[GID_2_LB2],
        MainWindowPtr, NULL,
        LISTBROWSER_Labels, &SubTypeList,
        LISTBROWSER_Selected, aminet.subtype,
        TAG_END
    );
}

AGLOBAL ULONG Hook2Func(struct Hook *h, VOID *o, VOID *msg)
{   /* "When the hook is called, the data argument points to the 
    window object and message argument to the IntuiMessage." */

    UWORD code, qual;
    ULONG scroll = 0;

    geta4(); // wait till here before doing anything

    code = ((struct IntuiMessage *) msg)->Code;
    qual = ((struct IntuiMessage *) msg)->Qualifier;

    switch(code)
    {
    case SCAN_HELP:
        helpabout();
    break;
    case SCAN_ESCAPE:
        if ((qual & IEQUALIFIER_LSHIFT) || (qual & IEQUALIFIER_RSHIFT))
        {   cleanexit(EXIT_SUCCESS);
        } else page = 0;
    break;
    case SCAN_UP:
        if (qual & IEQUALIFIER_CONTROL)
        {   scroll = LBP_TOP;
        } elif (qual & IEQUALIFIER_LSHIFT || qual & IEQUALIFIER_RSHIFT)
        {   scroll = LBP_PAGEUP;
        } else scroll = LBP_LINEUP;
    break;
    case SCAN_DOWN:
        if (qual & IEQUALIFIER_CONTROL)
        {   scroll = LBP_BOTTOM;
        } elif (qual & IEQUALIFIER_LSHIFT || qual & IEQUALIFIER_RSHIFT)
        {   scroll = LBP_PAGEDOWN;
        } else scroll = LBP_LINEDOWN;
    break;
    case SCAN_S:
        ActivateLayoutGadget(aminet_gadgets[GID_2_LY1], MainWindowPtr, NULL, (Object) aminet_gadgets[GID_2_ST1]);
    break;
    case SCAN_U:
        ActivateLayoutGadget(aminet_gadgets[GID_2_LY1], MainWindowPtr, NULL, (Object) aminet_gadgets[GID_2_ST2]);
    break;
    case SCAN_A:
        ActivateLayoutGadget(aminet_gadgets[GID_2_LY1], MainWindowPtr, NULL, (Object) aminet_gadgets[GID_2_ST3]);
    break;
    case SCAN_R:
        ActivateLayoutGadget(aminet_gadgets[GID_2_LY1], MainWindowPtr, NULL, (Object) aminet_gadgets[GID_2_ST4]);
    break;
    case SCAN_E:
        ActivateLayoutGadget(aminet_gadgets[GID_2_LY1], MainWindowPtr, NULL, (Object) aminet_gadgets[GID_2_ST5]);
    break;
    case SCAN_V:
        ActivateLayoutGadget(aminet_gadgets[GID_2_LY1], MainWindowPtr, NULL, (Object) aminet_gadgets[GID_2_ST6]);
    break;
    case SCAN_D:
        ActivateLayoutGadget(aminet_gadgets[GID_2_LY1], MainWindowPtr, NULL, (Object) aminet_gadgets[GID_2_ST7]);
    break;
    case SCAN_L:
        ActivateLayoutGadget(aminet_gadgets[GID_2_LY1], MainWindowPtr, NULL, (Object) aminet_gadgets[GID_2_TE1]);
    break;
    default:
    break;
    }

    if (code == SCAN_UP || code == SCAN_DOWN)
    {   SetGadgetAttrs
        (   aminet_gadgets[GID_2_LB2],    // pointer to gadget
            MainWindowPtr,                // pointer to window (not window object!)
            NULL,                         // pointer to requester
            LISTBROWSER_Position, scroll, // tags
            TAG_DONE                      // done
        );
    }

    return(1);
}

