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

#include "boards_lib.h" // pragmas for boards.libary

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

#define MANUFS 63

MODULE  ULONG                 querymanuf,
                              queryproduct;
MODULE  UBYTE                 boards;
MODULE  UBYTE                 whichboard;
MODULE  UWORD                 selectedmanuf   = 0,
                              selectedproduct = 0;
MODULE  struct ExpansionBase* ExpansionBase   = NULL;
// BoardsBase is declared in boards.h
MODULE  struct BoardsBase*    BoardsBase; // @@: = NULL; ?

#define MANUFS 63

// from rp1.c
IMPORT  SBYTE  page;
IMPORT struct NewGadget* Gadget;
IMPORT struct Gadget    *BU99_Right,
                        *PrevGadPtr;
IMPORT struct Window*    MainWindowPtr;

MODULE void manuf1(void);
MODULE ABOOL checklibrary(void);

MODULE struct
{   UWORD manuf;
    UBYTE product;
} board[32]; // how many boards can you have?

MODULE struct Gadget  *TE51_Desc,
                      *IN51_ManufID,
                      *TE51_ManufNames,
                      *BU51_NextBoard,
                      *BU51_PrevBoard,
                      *IN51_ProductID,
                      *TE51_ProductNames,
                      *BU51_Query;

MODULE struct
{   ULONG  number[3];
    STRPTR name;
} manuf[MANUFS + 1] =
{
 2010,  2113,    -1, "?",
 2117,    -1,    -1, "X-Pert",
 2022,    -1,    -1, "Xetec",
 2028,    -1,    -1, "Xebec",
 8215,    -1,    -1, "Vortex",
 2167,    -1,    -1, "VillageTronic",
 2128,    -1,    -1, "Triceratops",
 1056,    -1,    -1, "Supra",
 2150,    -1,    -1, "SRE",
 2034,  2035,    -1, "Spirit Technology",
32768,    -1,    -1, "SKI Peripherals",
 2055,    -1,    -1, "Serial Solutions (Checkpoint Technologies)",
 8704,    -1,    -1, "Sang/CT",
 1028,  4136,    -1, "Ronin/Imtronics",
43437,    -1,    -1, "Reis-Ware",
 8448,    -1,    -1, "ReadySoft",
 2148,    -1,    -1, "Protar",
  756,  2026,    -1, "Progressive Peripherals and Software",
 2101, 46504,    -1, "Phoenix",
 8512,    -1,    -1, "Phase 5",
  211,    -1,    -1, "Pacific Peripherals",
 2145,    -1,    -1, "Omega Datentechnik",
 2067,    -1,    -1, "NES, Incorporated",
 1010,    -1,    -1, "MicroBotics",
 1081,    -1,    -1, "Marc Michael Groth",
 2159,    -1,    -1, "Mainhattan-Data (A-Team)",
  256,  2203, 18260, "MacroSystems USA",
 1030,    -1,    -1, "Lowell University/Commodore Business Machines",
 2073,  4680,    -1, "Kupke/Golem",
 2052,    -1,    -1, "Jochheim",
 2112,    -1,    -1, "IVS",
 2078,    -1,    -1, "Interworks/Network",
 2104,    -1,    -1, "Impulse",
 2071,    -1,    -1, "ICD",
 2121,    -1,    -1, "Hydra Systems",
 2195,    -1,    -1, "Helfrich",
 2560,    -1,    -1, "Harms",
 2011,    -1,    -1, "Hacker",
 1761,  2017,  2193, "Great Valley Peripherals",
 2142,    -1,    -1, "GfxBase",
 1014,    -1,    -1, "Expansion Technologies (Pacific Cypress)",
 8290,    -1,    -1, "Expansion Systems",
 2064,    -1,    -1, "Edotronik",
 2129,    -1,    -1, "DMI",
 2012,    -1,    -1, "DKB",
 1058,    -1,    -1, "CSA",
 1006,    -1,    -1, "Comspec",
  513,   514,    -1, "Commodore Business Machines",
  515,    -1,    -1, "Combitech/MacroSystems USA",
26470, 49160,    -1, "Combitec",
 2021,    -1,    -1, "California Access (Synergy)",
 1004,  2050,    -1, "C. Ltd",
 2049,    -1,    -1, "BSC (Elaborate Bytes)",
 2092,    -1,    -1, "BSC",
 2146,    -1,    -1, "Atlantis",
 1005,    -1,    -1, "A-Squared",
 1023,    -1,    -1, "ASDG",
 2088,    -1,    -1, "Applied Engineering",
 1009,    -1,    -1, "Anakin Research",
 2176,    -1,    -1, "AmiTrix",
 1053,    -1,    -1, "Ameristar",
 2102,    -1,    -1, "Advanced Storage Systems",
 2149,    -1,    -1, "ACS",
 1012,    -1,    -1, "Access Associates"
};

#define PRODUCTS 174
struct
{   ULONG  manufid,
           number[2];
    STRPTR name;
    STRPTR desc;
} product[PRODUCTS + 1] =
{
 2026, 150,  -1, "Zeus 040",                            "68040 accelerator + 32-bit expansion memory + SCSI host adapter", 1056,  12,  -1, "WordSync HD Interface",               "SCSI host adapter",
 2203,  19,  -1, "Warp Engine",                         "68040 accelerator + 32-bit expansion memory",
  256,  19,  -1, "Warp Engine",                         "68040 accelerator + 32-bit expansion memory",
 1010,  68,  -1, "VXL RAM*32",                          "32-bit expansion memory",
 1010,  69,  -1, "VXL 30",                              "68030/6888x accelerator",
 2064,   8,  -1, "VME-Bus Controller",                  "VME-bus interface board",
18260,   4,  -1, "VLab Y/UV Digitizer",                 "Video digitizer board",
 2117,   2,  -1, "Visiona",                             "Graphics board (I/O area)",
 2117,   1,  -1, "Visiona",                             "Graphics board (memory area)",
 2113, 227,  -1, "Vektor I/O",                          "I/O board",
 2112, 243,  -1, "Vector",                              "68030/68882 accelerator + 32-bit expansion memory",
 2112,  48,  -1, "TrumpCard Classic",                   "SCSI host adapter",
 2112,  52,  -1, "TrumpCard Pro/Grand Slam",            "SCSI host adapter",
 1056,  10,  -1, "SupraRAM",                            "16-bit expansion memory",
 1056,   1,  -1, "SupraDrive 4x4 SCSI",                 "SCSI host adapter",
 1010,   2,  -1, "StarDrive",                           "SCSI host adapter",
 1010,   0,  -1, "StarBoard 2",                         "16-bit expansion memory",
46504,  33,  -1, "ST506 Autoboot",                      "MFM controller",
 2101,  33,  -1, "ST506 Autoboot",                      "MFM controller",
49160,  43,  -1, "SRAM Card",                           "Memory board",
 2034,   3,  -1, "SIN500",                              "16-bit expansion memory",
 2012,   9,  -1, "SecureKey",                           "Access control board",
32768, 128,  -1, "SCSI + Dual Serial",                  "SCSI host adapter/serial board",
46504,  34,  -1, "SCSI Autoboot",                       "SCSI host adapter",
 2101,  34,  -1, "SCSI Autoboot",                       "SCSI host adapter",
 2073,   2,  -1, "SCSI",                                "SCSI host adapter",
43437,  17,  -1, "Scan King",                           "Scanner",
  514,  96,  -1, "Romulator",                           "?",
18260,  16,  -1, "Retina Z3",                           "Graphics board",
18260,   6,  -1, "Retina",                              "Graphics board",
 2129,   1,  -1, "Resolver",                            "Graphics board",
 2088,  16,  -1, "RAM Works",                           "16-bit expansion memory",
 2052,   1,  -1, "RAM Board",                           "16-bit expansion memory",
 2022,   2,  -1, "RAM Board",                           "16-bit expansion memory",
 1023,   1,   2, "RAM Board",                           "16-bit expansion memory",
 2011,   2,  -1, "Quicknet QN2000",                     "Ethernet LAN board",
 2064,   6,  -1, "PIC Prototyping Board",               "Prototyping board",
 2195,  11,  -1, "Piccolo SD64",                        "Graphics board (I/O area)",
 2195,  10,  -1, "Piccolo SD64",                        "Graphics board (memory area)",
 2195,   6,  -1, "Piccolo",                             "Graphics board (I/O area)",
 2195,   5,  -1, "Piccolo",                             "Graphics board (memory area)",
 2167,  13,  -1, "Picasso ][",                          "Graphics board (segmented area)",
 2167,  12,  -1, "Picasso ][",                          "Graphics board (I/O area)",
 2167,  11,  -1, "Picasso ][",                          "Graphics board (memory area)",
 2112,  16,  -1, "OverDrive",                           "SCSI host adapter",
 4680,   1,  -1, "Omti HD Controller",                  "MFM controller",
 2073,   1,  -1, "Omti HD Controller",                  "MFM controller",
 2092,   5,  -1, "Oktagon",                             "SCSI host adapter",
 2034,   6,  -1, "OctaByte",                            "16-bit expansion memory",
 2102,   1,  -1, "Nexus",                               "SCSI host adapter",
 2128,   1,  -1, "Multiport I/O",                       "I/O board",
 2176,   1,  -1, "Multi-I/O",                           "I/O board",
 2092,  16,  17, "MultiFaceCard",                       "Serial/parallel board",
 2092,  18,  -1, "MultiFaceCard ]I[",                   "Serial/parallel board",
 2112,  64,  -1, "Meta-4",                              "16-bit expansion memory",
 2092,   8,  -1, "Memory Master",                       "16-bit expansion memory",
 2073,   3,  -1, "Memory Expansion",                    "16-bit expansion memory",
46504, 190,  -1, "Memory Board",                        "16-bit expansion memory",
 2101, 190,  -1, "Memory Board",                        "16-bit expansion memory",
 1010, 129, 193, "MBX 1200",                            "32-bit expansion memory",
 2021,   1,  -1, "Malibu SCSI",                         "SCSI host adapter",
 1058,  17,  -1, "Magnum 040",                          "68040 accelerator + 32-bit expansion memory",
 8704,   1,  -1, "Link-Interface",                      "Transputer link interface",
 1005,   1,  -1, "LIVE! A2000",                         "Video digitizer board",
 2050,   4,  -1, "Kronos 2000",                         "SCSI host adapter",
 1004,   4,  -1, "Kronos 2000",                         "SCSI host adapter",
 2092,  65,  -1, "ISDN-Master",                         "ISDN board",
 2034,   1,  -1, "Insider IN1000",                      "16-bit expansion memory",
 2034,   2,  -1, "Insider IN500",                       "16-bit expansion memory",
 2034,   8,  -1, "Inmate SCSI/FPU/RAM",                 "?",
 2017,  32,  -1, "Impact Vision 24",                    "Graphics board",
 2017,  11,  -1, "Impact Series ][ SCSI (DPRC)",        "SCSI host adapter (or I/O extender)",
 2017,   3,  -1, "Impact Series I SCSI 16K/3",          "SCSI host adapter",
 2017,   2,  -1, "Impact Series I SCSI 16K/2",          "SCSI host adapter",
 2017,   1,  -1, "Impact Series I SCSI 4K",             "SCSI host adapter",
 1761,   8,  -1, "Impact SCSI",                         "SCSI host adapter",
 2017,   8,  13, "Impact A3001 IDE HD",                 "68030/68882 accelerator + IDE controller",
 2064,   1,  -1, "IEEE-488 Controller",                 "IEEE-488 interface board",
 2159,   1,  -1, "IDE",                                 "IDE controller",
 2121,   1,  -1, "Hydra Ethernet",                      "Ethernet LAN board",
 4136,  57,  87, "Hurricane 2800",                      "68030/6888x accelerator + 32-bit expansion memory",
 1028,  57,  87, "Hurricane 2800",                      "68030/6888x accelerator + 32-bit expansion memory",
 2064,   3,  -1, "High-Speed Multiserial Interface",    "Serial board",
  211,  10,  -1, "HD Controller",                       "SCSI host adapter",
49160,  42,  -1, "HD Controller",                       "SCSI host adapter",
 2034,   4,  -1, "HDA-506",                             "MFM controller",
 1010, 150, 158, "HardFrame/2000",                      "SCSI host adapter",
 2112,   4,  -1, "GrandSlam",                           "SCSI/parallel/serial board",
 1023, 255,  -1, "GPIB or Dual Serial Board",           "IEEE-488 or Twin-X serial card",
 8215,   8,  -1, "GoldenGate 386SX RAM",                "80386SX bridgeboard/Amiga shared RAM",
 8215,   7,  -1, "GoldenGate 386SX",                    "80386SX bridgeboard",
 2150,   1,  -1, "GoldenGate ][",                       "Zorro ][-ISA bridgeboard",
 2017, 255,  -1, "G-Force 040 I/O ('040/2000)",         "Serial/parallel board",
 2142,   0,  -1, "GDA-1 VRAM",                          "Graphics board",
 2142,   1,  -1, "GDA-1",                               "Graphics board",
 2104,   0,  -1, "Firecracker",                         "Frame buffer",
 2092,  32,  -1, "Frame Buffer",                        "Graphics board",
 2022,   1,  -1, "FastTrak HD",                         "SCSI host adapter",
 8512,  10,  -1, "FastLane Z3",                         "SCSI host adapter + 32-bit expansion memory",
 2022,   3,  -1, "FastCard + HD Controller",            "?",
 2067,   0,  -1, "Expansion RAM",                       "16-bit expansion memory",
  756,   2,  -1, "EXP8000",                             "16-bit expansion memory",
 1053,   1,  -1, "Ethernet",                            "Ethernet LAN board",
 2193,   1,  -1, "EGS Spectrum",                        "Graphics board (I/O area)",
 2193,   2,  -1, "EGS Spectrum",                        "Graphics board (memory area)",
 1004,  14,  -1, "Escort",                              "SCSI host adapter",
 1023, 254,  -1, "EB-920 LanRover",                     "Ethernet LAN board",
 1009,   1,  -1, "Easyl",                               "Graphics tablet",
 2064,   9,  -1, "DSP96000 Real Time Data Acquisition", "Data acquisition board",
 2088,  16,  -1, "DL2000",                              "Modem",
 2055,   0,  -1, "Dual Port Serial",                    "Serial board",
 2129,   6,  -1, "Digital Broadcaster 32",              "Image processing board",
 8290,   1,  -1, "DataFlyer 500 SCSI",                  "SCSI host adapter",
 8512,  34,  -1, "CyberVision 64",                      "Graphics board",
 8512,  11,  -1, "CyberStorm",                          "68040 accelerator + 32-bit expansion memory",
 2176,   2,  -1, "CD-RAM",                              "16-bit expansion memory",
 2010,   0,  -1, "Byte Box A500",                       "?",
 8512,  24,  -1, "Blizzard A2060",                      "68060 accelerator + 32-bit expansion memory",
 8512,  17,  -1, "Blizzard 1230",                       "68030/68882 accelerator + 32-bit expansion memory",
 8512,   2,  -1, "Blizzard Turbo",                      "Accelerator board (I/O area)",
 8512,   1,  -1, "Blizzard Turbo",                      "Accelerator board (memory area)",
 2034,   5,  -1, "AX-S IBM Hardware I/O Board",         "I/O board",
 1006,   1,  -1, "AX2000",                              "16-bit expansion memory",
 8448,   1,  -1, "A-Max ][+/A-Max IV",                  "Macintosh emulation board",
 2049,   3,  -1, "ALF 3 SCSI",                          "SCSI host adapter",
 2049,   2,  -1, "ALF 2 SCSI",                          "SCSI host adapter",
 2049,   1,  -1, "ALF 2",                               "MFM controller",
 2071,   1,  -1, "Advantage 2000/AdSCSI",               "SCSI host adapter",
 2167, 201,  -1, "Adriadne",                            "Ethernet LAN + I/O board",
 2071,   4,  -1, "AdRAM 2080",                          "16-bit expansion memory",
 2071,   3,  -1, "AdIDE",                               "IDE controller",
  514,  84,  -1, "A4091",                               "SCSI host adapter",
 1053,  10,  -1, "A4066",                               "Ethernet LAN board",
  514,  97,  -1, "A3000 test fixture",                  "?",
  514,  81,  -1, "A2630",                               "68030/68882 accelerator + 32-bit expansion memory",
  514,  80,  -1, "A2620",                               "68020/68851/6888x accelerator + 32-bit expansion memory",
 1030,   0,  -1, "A2410",                               "Graphics board",
  513, 103,  -1, "A2386SX",                             "80386SX bridgeboard",
  513,   2,  -1, "A2286",                               "80286 bridgeboard",
  514,  70,  -1, "A2232",                               "Serial board",
  514,  69,  -1, "A2232 prototype",                     "Serial board",
  514,   3,  -1, "A2091",                               "SCSI host adapter",
  514,   4,  -1, "A2090B",                              "Autoboot card",
  515,   3,  -1, "A2090/A2090A",                        "MFM controller + SCSI host adapter with new driver",
  514,   1,  -1, "A2090/A2090A",                        "MFM controller + SCSI host adapter",
  513,   1,  -1, "A2088/A2286",                         "8088/80286 bridgeboard",
  514, 112,  -1, "A2065",                               "Ethernet LAN board",
 1053,   9,  -1, "A2060 Arcnet",                        "Arcnet LAN board",
  514,   9,  -1, "A2060",                               "Arcnet LAN board",
  514,  10,  -1, "A205x/A2091/A590",                    "16-bit expansion memory",
 2560, 208,  -1, "A2000 Turboboard 3500 Professional",  "68030/68882 accelerator + 32-bit expansion memory",
 1056,   3,  -1, "A2000 DMA HD Interface",              "SCSI host adapter",
 2017,  22,  -1, "A2000 060 Combo",                     "68060 accelerator + 32-bit expansion memory",
 1004,  12,  -1, "A1000/A2000 SCSI",                    "SCSI host adapter",
 2050,  12,  -1, "A1000 SCSI",                          "SCSI host adapter",
 1056,   2,  -1, "A1000 RAM Expansion",                 "16-bit expansion memory",
  514,   2,  -1, "A590",                                "SCSI host adapter/XT controller",
  514,  32,  -1, "A560",                                "16-bit expansion memory",
 1056,  13,  -1, "A500 ByteSync HD Interface",          "SCSI host adapter",
 1056,   8,  -1, "A500 Autoboot HD Interface",          "SCSI host adapter",
 2073,   5,  -1, "68030 (asynchronous)",                "68030/6888x accelerator",
 2073,   4,  -1, "68030 (synchronous)",                 "68030/6888x accelerator",
 2064,   2,  -1, "8032 Development Adapter",            "CBM-8032 board?",
 1056,  16,  -1, "2400zi+",                             "Modem",
 1056,  11,  -1, "2400zi",                              "Modem",
 1056,   9,  -1, "500/2000 RX",                         "16-bit expansion memory",
 1056,   5,  -1, "500 HD/RAM",                          "SCSI host adapter + 16-bit expansion memory",
 2026, 105,  -1, "040/A2000 Board",                     "68040 accelerator + 32-bit expansion memory",
 2064,   5,  -1, "32-Bit Parallel I/O Interface",       "Parallel board",
 2064,   4,  -1, "24-Bit Real Time Video Digitizer",    "Video digitizer board",
 2064,   7,  -1, "16-Channel ADC Interface",            "Analog to digital board",
 1058,  21,  -1, "12 Gauge",                            "68030/68882 accelerator + 32-bit expansion memory",
 1010,   4,  -1, "8-UP! (Z)",                           "16-bit expansion memory",
 1010,   3,  -1, "8-UP! (A)",                           "16-bit expansion memory"
};

AGLOBAL void runquery(ULONG querymanuf, ULONG queryproduct)
{   SWORD i, j; /* i must be signed and at least 16 bits wide */

    selectedmanuf = (UWORD) -1;
    selectedproduct = (UWORD) -1;
    if (querymanuf != (ULONG) -1 && queryproduct != (ULONG) -1)
    {   for (i = 0; i <= MANUFS; i++)
            for (j = 0; j <= 2; j++)
                if (querymanuf == manuf[i].number[j])
                    selectedmanuf = i;
        if (selectedmanuf != (UWORD) -1)
        {   // found manufacturer

            GT_SetGadgetAttrs
            (   TE51_ManufNames,
                MainWindowPtr,
                NULL,
                GTTX_Text, manuf[selectedmanuf].name,
                TAG_DONE
            );

            // now look for product too

            for (i = 0; i <= PRODUCTS; i++)
                if (querymanuf == product[i].manufid)
                for (j = 0; j <= 1; j++)
                    if (queryproduct == product[i].number[j])
                        selectedproduct = i;
            if (selectedproduct != (UWORD) -1)
            {   // found product too

                GT_SetGadgetAttrs
                (   TE51_ProductNames,
                    MainWindowPtr,
                    NULL,
                    GTTX_Text, product[selectedproduct].name,
                    TAG_DONE
                );
                GT_SetGadgetAttrs
                (   TE51_Desc,
                    MainWindowPtr,
                    NULL,
                    GTTX_Text, product[selectedproduct].desc,
                    TAG_DONE
                );
            } else
            {   /* can't find product, but did find (and print) manufacturer */
                if (checklibrary())
                {   return;
                }
                GT_SetGadgetAttrs
                (   TE51_ProductNames,
                    MainWindowPtr,
                    NULL,
                    GTTX_Text, "-",
                    TAG_DONE
                );
                GT_SetGadgetAttrs
                (   TE51_Desc,
                    MainWindowPtr,
                    NULL,
                    GTTX_Text, "-",
                    TAG_DONE
                );
        }   }
        else
        {   /* can't find manufacturer, let alone product */
            if (checklibrary())
            {   return;
            }
            GT_SetGadgetAttrs
            (   TE51_ManufNames,
                MainWindowPtr,
                NULL,
                GTTX_Text, "-",
                TAG_DONE
            );
            GT_SetGadgetAttrs
            (   TE51_ProductNames,
                MainWindowPtr,
                NULL,
                GTTX_Text, "-",
                TAG_DONE
            );
            GT_SetGadgetAttrs
            (   TE51_Desc,
                MainWindowPtr,
                NULL,
                GTTX_Text, "-",
                TAG_DONE
            );
}   }   }

AGLOBAL void updatemanuf(void)
{   if (whichboard == 0)
        GT_SetGadgetAttrs
        (   BU51_PrevBoard,
            MainWindowPtr, NULL,
            GA_Disabled, TRUE,
            TAG_DONE
        );
    else
        GT_SetGadgetAttrs
        (   BU51_PrevBoard,
            MainWindowPtr, NULL,
            GA_Disabled, FALSE,
            TAG_DONE
        );

    if (whichboard < boards)
    {   GT_SetGadgetAttrs
        (   BU51_Query,
            MainWindowPtr, NULL,
            GA_Disabled, TRUE,
            TAG_DONE
        );
        GT_SetGadgetAttrs
        (   BU51_NextBoard,
            MainWindowPtr, NULL,
            GA_Disabled, FALSE,
            TAG_DONE
        );
        GT_SetGadgetAttrs
        (   IN51_ManufID,
            MainWindowPtr, NULL,
            GA_Disabled, TRUE,
            GTIN_Number, board[whichboard].manuf,
            TAG_DONE
        );
        GT_SetGadgetAttrs
        (   IN51_ProductID,
            MainWindowPtr, NULL,
            GA_Disabled, TRUE,
            GTIN_Number, board[whichboard].product,
            TAG_DONE
        );
        runquery(board[whichboard].manuf, board[whichboard].product);
    } else
    {   GT_SetGadgetAttrs
        (   BU51_Query,
            MainWindowPtr, NULL,
            GA_Disabled, FALSE,
            TAG_DONE
        );
        GT_SetGadgetAttrs
        (   BU51_NextBoard,
            MainWindowPtr, NULL,
            GA_Disabled, TRUE,
            TAG_DONE
        );
        GT_SetGadgetAttrs
        (   IN51_ManufID,
            MainWindowPtr, NULL,
            GA_Disabled, FALSE,
            GTIN_Number, querymanuf,
            TAG_DONE
        );
        GT_SetGadgetAttrs
        (   IN51_ProductID,
            MainWindowPtr, NULL,
            GA_Disabled, FALSE,
            GTIN_Number, queryproduct,
            TAG_DONE
        );
}   }

AGLOBAL void manuf0(void)
{   struct ConfigDev* ThisConfigDev;
    
    if (ExpansionBase = (struct ExpansionBase *) OpenLibrary(EXPANSIONNAME, 0))
    {   /* Use FindConfigDev() to get info on the first expansion board on
        the list maintained by Exec. */

        if (ThisConfigDev = FindConfigDev(NULL, -1, -1))
        {   board[0].manuf = ThisConfigDev->cd_Rom.er_Manufacturer;
            board[0].product = ThisConfigDev->cd_Rom.er_Product;
            boards = 1;

            /* OK, there is at least one board, so loop and get the
            entire list. */
            while (ThisConfigDev = FindConfigDev(ThisConfigDev, -1, -1))
            {   board[boards].manuf = ThisConfigDev->cd_Rom.er_Manufacturer;
                board[boards++].product = ThisConfigDev->cd_Rom.er_Product;
        }   }
        else boards = 0;
        CloseLibrary((struct Library *) ExpansionBase);
    }
    BoardsBase = (struct Library *) OpenLibrary("boards.library", 0L);

    manuf1();
}

MODULE void manuf1(void)
{   verynewwindow
    (   MANUF1WIDTH, MANUF1HEIGHT,
        "Report+: ID Database",
        BUTTONIDCMP
    );
    SetAPen(MainWindowPtr->RPort, 0);
    RectFill(MainWindowPtr->RPort, 130, 28, 130 + 390, 28 + 12);
    RectFill(MainWindowPtr->RPort, 130, 42, 130 + 390, 42 + 12);
    RectFill(MainWindowPtr->RPort, 130, 56, 130 + 390, 56 + 12);

    /* manufacturer name */
    setgadget(130, 28, 390, 12, "Manufacturer:", NULL);
    TE51_ManufNames = PrevGadPtr = (struct Gadget *) CreateGadget
    (   TEXT_KIND,
        PrevGadPtr,
        &Gadget,
        GTTX_Text, "-",
        GTTX_Border, TRUE,
        TAG_DONE
    );
    /* product name */
    setgadget(130, 42, 390, 12, "Product:", NULL);
    TE51_ProductNames = PrevGadPtr = (struct Gadget *) CreateGadget
    (   TEXT_KIND,
        PrevGadPtr,
        &Gadget,
        GTTX_Text, "-",
        GTTX_Border, TRUE,
        TAG_DONE
    );
    /* product description */
    setgadget(130, 56, 390, 12, "Description:", NULL);
    TE51_Desc = PrevGadPtr = (struct Gadget *) CreateGadget
    (   TEXT_KIND,
        PrevGadPtr,
        &Gadget,
        GTTX_Text, "-",
        GTTX_Border, TRUE,
        TAG_DONE
    );

    /* manufacturer ID */
    setgadget(560, 28, 64, 12, "_ID:", NULL);
    IN51_ManufID = PrevGadPtr = (struct Gadget *) CreateGadget
    (   INTEGER_KIND,
        PrevGadPtr,
        &Gadget,
        GTIN_Number, querymanuf,
        GA_TabCycle, TRUE,
        STRINGA_ReplaceMode, TRUE,
        STRINGA_Justification, GACT_STRINGRIGHT,
        GT_Underscore, '_',
        TAG_DONE
    );

    /* product ID */
    setgadget(560, 42, 64, 12, "I_D:", NULL);
    IN51_ProductID = PrevGadPtr = (struct Gadget *) CreateGadget
    (   INTEGER_KIND,
        PrevGadPtr,
        &Gadget,
        GTIN_Number, queryproduct,
        GA_TabCycle, TRUE,
        STRINGA_ReplaceMode, TRUE,
        STRINGA_Justification, GACT_STRINGRIGHT,
        GT_Underscore, '_',
        TAG_DONE
    );

    /* query */
    setgadget(560, 56, 64, 12, "_Query", NULL);
    BU51_Query = PrevGadPtr = (struct Gadget *) CreateGadget
    (   BUTTON_KIND,
        PrevGadPtr,
        &Gadget,
        GT_Underscore, '_',
        TAG_DONE
    );

    /* previous board */
    setgadget(130, 70, 195, 12, "_Previous Board", NULL);
    BU51_PrevBoard = PrevGadPtr = (struct Gadget *) CreateGadget
    (   BUTTON_KIND,
        PrevGadPtr,
        &Gadget,
        GT_Underscore, '_',
        TAG_DONE
    );
    /* next board */
    setgadget(325, 70, 195, 12, "_Next Board", NULL);
    BU51_NextBoard = PrevGadPtr = (struct Gadget *) CreateGadget
    (   BUTTON_KIND,
        PrevGadPtr,
        &Gadget,
        GT_Underscore, '_',
        TAG_DONE
    );

    /* boards is the actual number of boards found. 0 to (board - 1). */
    /* We actually use the [board] index as our 'spare' 'queryable' index. */

    /* There are 11 gadgets:
       BU99_Right        1
       TE51_ManufNames   1
       TE51_ProductNames 1
       TE51_Desc         1
       IN51_ManufID      2
       IN51_ProductID    2
       BU51_Query        1
       BU51_PrevBoard    1
       BU51_NextBoard    1

    In the old way of doing windows, we first called navigate(), then
    set up the gadgets, then called newwindow().
        In the new way, we first call verynewwindow() (which calls
    navigate() automatically), then set up the gadgets, then call
    drawgadgets(). */

    drawgadgets(11);

    ActivateGadget(IN51_ManufID, MainWindowPtr, NULL);
    updatemanuf();

    loop();
    closewindow();
}

AGLOBAL void manuf_loop(ULONG class, struct Gadget* addr, UWORD code)
{   if (class == IDCMP_VANILLAKEY)
    {   code = toupper(code);
        if (code == 'Q')
        {   querymanuf = ((struct StringInfo *) IN51_ManufID->SpecialInfo)->LongInt;
            queryproduct = ((struct StringInfo *) IN51_ProductID->SpecialInfo)->LongInt;
            runquery(querymanuf, queryproduct);
        } elif (code == ESCAPE)
        {   page = 0;
        } elif (code == 'N')
        {   whichboard++;
            updatemanuf();
        } elif (code == 'P')
        {   whichboard--;
            updatemanuf();
        } elif (code == 'I')
        {   ActivateGadget(IN51_ManufID, MainWindowPtr, NULL);
        } elif (code == 'D')
        {   ActivateGadget(IN51_ProductID, MainWindowPtr, NULL);
    }   }
    elif (class == IDCMP_GADGETUP)
    {   if (addr == BU99_Right)
        {   page = 0;
        } elif (addr == BU51_Query)
        {   querymanuf = ((struct StringInfo *) IN51_ManufID->SpecialInfo)->LongInt;
            queryproduct = ((struct StringInfo *) IN51_ProductID->SpecialInfo)->LongInt;
            runquery(querymanuf, queryproduct);
        } elif (addr == BU51_NextBoard)
        {   whichboard++;
            updatemanuf();
        } elif (addr == BU51_PrevBoard)
        {   whichboard--;
            updatemanuf();
}   }   }

MODULE ABOOL checklibrary(void)
{   TEXT manuf_buffer[49], product_buffer[49];

    if (BoardsBase)
    {   if (GetBoardName(manuf_buffer, product_buffer, NULL, querymanuf, queryproduct))
        {   GT_SetGadgetAttrs
            (   TE51_ManufNames,
                MainWindowPtr,
                NULL,
                GTTX_Text, manuf_buffer,
                TAG_DONE
            );
            GT_SetGadgetAttrs
            (   TE51_ProductNames,
                MainWindowPtr,
                NULL,
                GTTX_Text, product_buffer,
                TAG_DONE
            );
            GT_SetGadgetAttrs
            (   TE51_Desc,
                MainWindowPtr,
                NULL,
                GTTX_Text, "-",
                TAG_DONE
            );
            return TRUE;
        } else return FALSE;
}   }

AGLOBAL void manuf_exit(void)
{   if (BoardsBase)
    {   CloseLibrary(BoardsBase);
        BoardsBase = NULL;
}   }

AGLOBAL void manuf_init(void)
{   // Only ever called at the start of the program.

    BoardsBase = NULL;
}
