#include <exec/types.h>
#include <exec/memory.h>

#include <exec/io.h>
#include <utility/tagitem.h>

#include <clib/exec_protos.h>
#include <clib/alib_protos.h>
#include <clib/dos_protos.h>

#include <stdio.h>

#include <devices/cd.h>

struct MsgPort *cdport;
struct IOStdReq *cdreq;

struct TagItem newconfig[] = {
    TAGCD_READSPEED, 150,
    TAG_DONE
    };

UBYTE *Version = "$VER: CDSpeed 1.0 (14.05.95)";

void clean_up(char *text);

void main()
{
    struct CDInfo Info;

    if(!(cdport = CreateMsgPort()))
        clean_up("Could not create port");
    if(!(cdreq = CreateIORequest(cdport, sizeof(struct IOStdReq))))
        clean_up("Could not create IORequest");

    if(OpenDevice("cd.device", 0, (struct IORequest *)cdreq, 0))
        clean_up("Could not open cd.device");

    cdreq->io_Command = CD_INFO;
    cdreq->io_Data = &Info;
    cdreq->io_Length = sizeof(struct CDInfo);

    DoIO((struct IORequest *)cdreq);

    if(!cdreq->io_Error) {
        Printf("The CD's speed is %ld bytes/sec.\n", Info.ReadSpeed*2048);
        Printf("Your drive is capable of %ld bytes/sec.\n", Info.MaxSpeed*2048);
        newconfig[0].ti_Data = Info.MaxSpeed;
        cdreq->io_Command = CD_CONFIG;
        cdreq->io_Data = &newconfig;
        cdreq->io_Length = 0;
        DoIO((struct IORequest*)cdreq);
        if(!cdreq->io_Error)
            Printf("The speed is now maximized!\n");
        else
            Printf("Unable to change speed\n");
        }

    clean_up(0);

}

void clean_up(char *text)
{
    Printf("%s\n", text);

    if(cdreq) CloseDevice((struct IORequest *)cdreq);

    if(cdreq) DeleteIORequest(cdreq);
    if(cdport) DeleteMsgPort(cdport);
    exit(0);
}



