#include <stdio.h>
#include <time.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include <dos.h>
#include <dos/dos.h>
#include <graphics/gfxmacros.h>
#include <exec/types.h>
#include <exec/io.h>
#include <exec/execbase.h>
#include <exec/nodes.h>
#include <exec/memory.h>
#include <exec/libraries.h>
#include <workbench/workbench.h>
#include <workbench/startup.h>
#include <devices/trackdisk.h>
#include <devices/scsidisk.h>
#include <devices/timer.h>
#include <devices/inputevent.h>
#include <libraries/dos.h>
#include <libraries/mui.h>
#include <libraries/diskfont.h>
#include <clib/diskfont_protos.h>
#include <clib/alib_protos.h>
#include <clib/asl_protos.h>
#include <clib/muimaster_protos.h>
#include <clib/exec_protos.h>
#include <clib/dos_protos.h>
#include <clib/utility_protos.h>
#include <clib/gadtools_protos.h>
#include <clib/graphics_protos.h>
#include <clib/icon_protos.h>
#include <clib/commodities_protos.h>
#include <clib/wb_protos.h>
#include <clib/intuition_protos.h>
#include <clib/locale_protos.h>
#include <intuition/intuition.h>
#include <intuition/gadgetclass.h>
#include <graphics/displayinfo.h>
#include <pragmas/diskfont_pragmas.h>
#include <pragmas/exec_sysbase_pragmas.h>
#include <pragmas/dos_pragmas.h>
#include <pragmas/icon_pragmas.h>
#include <pragmas/graphics_pragmas.h>
#include <pragmas/intuition_pragmas.h>
#include <pragmas/gadtools_pragmas.h>
#include <pragmas/utility_pragmas.h>
#include <pragmas/asl_pragmas.h>
#include <pragmas/muimaster_pragmas.h>
#include <pragmas/locale_pragmas.h>

extern struct Library *SysBase,*IntuitionBase,*UtilityBase,*GfxBase,*DOSBase,*IconBase,*DiskfontBase;
struct Library *MUIMasterBase, *LocaleBase;
struct Catalog *catalog;

#define VERSION "1.4"

#include "scdplocale.h"

#define STACKSIZE 16384

#define REG(x) register __ ## x
#define ASM    __asm
#define SAVEDS __saveds

/* For the fonts */
struct TextFont *normalfont=NULL;
struct TextFont *tinyfont=NULL;
struct TextFont *fixedfont=NULL;
struct TextFont *listfont=NULL;
struct TextFont *titlefont=NULL;
BOOL customfonts=FALSE;
char *normalname="topaz/8";
char *tinyname="topaz/8";
char *fixedname="topaz/8";
char *listname="topaz/8";
char *titlename="topaz/8";
/* For disks path */
char diskspath[513]="PROGDIR:disks";

struct MUI_CustomClass *CL_ProgramList=NULL;

/*************************/
/* Init & Fail Functions */
/*************************/

static VOID fail(APTR app,char *str)
{
        if (app)
                MUI_DisposeObject(app);

#ifndef _DCC
        if (MUIMasterBase)
                CloseLibrary(MUIMasterBase);
#endif
        if (LocaleBase)
        {
            CloseLibrary(LocaleBase);
            if(catalog)
                CloseCatalog(catalog);
        }

        if(normalfont)
            CloseFont(normalfont);
        if(tinyfont)
            CloseFont(tinyfont);
        if(fixedfont)
            CloseFont(fixedfont);
        if(listfont)
            CloseFont(listfont);
        if(titlefont)
            CloseFont(titlefont);

        if(CL_ProgramList)
            MUI_DeleteCustomClass(CL_ProgramList);

        if (str)
        {
                puts(str);
                exit(20);
        }
        exit(0);
}


#ifdef _DCC

int brkfunc(void) { return(0); }

int wbmain(struct WBStartup *wb_startup)
{
        extern int main(int argc, char *argv[]);
        return (main(0, (char **)wb_startup));
}

#endif


#ifdef __SASC
int CXBRK(void) { return(0); }
int _CXBRK(void) { return(0); }
void chkabort(void) {}
#endif


static VOID init(VOID)
{
#ifdef _DCC
        onbreak(brkfunc);
#endif

#ifndef _DCC
        if (!(MUIMasterBase = OpenLibrary(MUIMASTER_NAME,MUIMASTER_VMIN)))
                fail(NULL,glstr(MSG_FailMUI));
#endif
        if(LocaleBase = OpenLibrary("locale.library",38))
        {
            catalog=OpenCatalog(NULL,"scdp.catalog",NULL);
        }
}


#ifndef MAKE_ID
#define MAKE_ID(a,b,c,d) ((ULONG) (a)<<24 | (ULONG) (b)<<16 | (ULONG) (c)<<8 | (ULONG) (d))
#endif

 
#define BYTES_PER_LINE  16
#define SENSE_LEN 252
#define MAX_DATA_LEN 252
#define MAX_TOC_LEN 804         /* max TOC size = 100 TOC track descriptors */
#define PAD 0
#define LINE_BUF        (128)
#define NUM_OF_CDDAFRAMES 75    /* 75 frames per second audio */
#define CDDALEN 2448            /* 1 frame has max. 2448 bytes (subcode 2) */

#define OFFS_KEY 2
#define OFFS_CODE 12

typedef struct MsgPort MSGPORT;
typedef struct IOStdReq IOSTDREQ;
typedef struct List LIST;
typedef struct Node NODE;
typedef struct SCSICmd SCSICMD;

typedef struct
 {
   BYTE   code;
   UBYTE  *ptr;
 } IDTOSTRING;

typedef struct
 {
   UBYTE  opcode;
   UBYTE  b1;
   UBYTE  b2;
   UBYTE  b3;
   UBYTE  b4;
   UBYTE  control;
 } SCSICMD6;

typedef struct
 {
   UBYTE  opcode;
   UBYTE  b1;
   UBYTE  b2;
   UBYTE  b3;
   UBYTE  b4;
   UBYTE  b5;
   UBYTE  b6;
   UBYTE  b7;
   UBYTE  b8;
   UBYTE  control;
 } SCSICMD10;

typedef struct
 {
   UBYTE  opcode;
   UBYTE  b1;
   UBYTE  b2;
   UBYTE  b3;
   UBYTE  b4;
   UBYTE  b5;
   UBYTE  b6;
   UBYTE  b7;
   UBYTE  b8;
   UBYTE  b9;
   UBYTE  b10;
   UBYTE  control;
 } SCSICMD12;

#define SCSI_CMD_TUR    0x00
#define SCSI_CMD_RZU    0x01
#define SCSI_CMD_RQS    0x03
#define SCSI_CMD_FMU    0x04
#define SCSI_CMD_RAB    0x07
#define SCSI_CMD_RD     0x08
#define SCSI_CMD_WR     0x0A
#define SCSI_CMD_SK     0x0B
#define SCSI_CMD_INQ    0x12
#define SCSI_CMD_MSL    0x15
#define SCSI_CMD_RU     0x16
#define SCSI_CMD_RLU    0x17
#define SCSI_CMD_MSE    0x1A
#define SCSI_CMD_SSU    0x1B
#define SCSI_CMD_RDI    0x1C
#define SCSI_CMD_SDI    0x1D
#define SCSI_CMD_PAMR   0x1E
#define SCSI_CMD_RCP    0x25
#define SCSI_CMD_RXT    0x28
#define SCSI_CMD_WXT    0x2A
#define SCSI_CMD_SKX    0x2B
#define SCSI_CMD_WVF    0x2E
#define SCSI_CMD_VF     0x2F
#define SCSI_CMD_RDD    0x37
#define SCSI_CMD_WDB    0x3B
#define SCSI_CMD_RDB    0x3C

#define SCSI_CMD_COPY           0x18
#define SCSI_CMD_COMPARE        0x39
#define SCSI_CMD_COPYANDVERIFY  0x3A
#define SCSI_CMD_CHGEDEF        0x40
#define SCSI_CMD_READSUBCHANNEL 0x42
#define SCSI_CMD_READTOC        0x43
#define SCSI_CMD_READHEADER     0x44
#define SCSI_CMD_PLAYAUDIO12    0xA5
#define SCSI_CMD_PLAYAUDIOTRACKINDEX    0x48
#define SCSI_CMD_PAUSERESUME    0x4B

#define CheckMarkH(selected,help)\
        ImageObject,\
                ImageButtonFrame,\
                MUIA_InputMode        , MUIV_InputMode_Toggle,\
                MUIA_Image_Spec       , MUII_CheckMark,\
                MUIA_Image_FreeVert   , TRUE,\
                MUIA_Selected         , selected,\
                MUIA_Background       , MUII_ButtonBack,\
                MUIA_ShowSelState     , FALSE,\
                MUIA_ShortHelp        , help,\
                End

