/***************************************************************************
 *                                                                         *
 *  NAME    :   LibTest.c                                                  *
 *                                                                         *
 *                                                                         *
 *  SYNOPSIS:   This is the file system extension test program.            *
 *                                                                         *
 *                                                                         *
 *  FUNCTION:                                                              *
 *                                                                         *
 *                                                                         *
 *  RESULTS :                                                              *
 *                                                                         *
 *                                                                         *
 *  SEE ALSO:                                                              *
 *                                                                         *
 ***************************************************************************
 *                                                                         *
 *  REVISION:                                                              *
 *                                                                         *
 *  Date        Author          Description                                *
 *  --------    --------------  -----------------------------------------  *
 *  12-31-89    A.M.Purtle      New Code                                   *
 *                                                                         *
 *                                                                         *
 ***************************************************************************/

#ifndef EXEC_TYPES_H
#include <exec/types.h>
#endif !EXEC_TYPES_H

#ifndef DOS_FILEHANDLER_H
#include <DOS/filehandler.h>
#endif !DOS_FILEHANDLER_H

#ifndef EXEC_MEMORY_H
#include <exec/memory.h>
#endif !EXEC_MEMORY_H

#ifndef DOS_DOS_H
#include <DOS/dos.h>
#endif !DOS_DOS_H

#ifndef DOS_DOSEXTENS_H
#include <DOS/dosextens.h>
#endif !DOS_DOSEXTENS_H

#ifndef CDROM_HSG_H
#include <CDROM/HSG.h>
#endif !CDROM_HSG_H

#ifndef CDROM_ISO_H
#include <CDROM/ISO.h>
#endif !CDROM_ISO_H

#ifndef CDROM_CDEXTENS_H
#include <CDROM/CDExtens.h>
#endif !CDROM_CDEXTENS_H

#ifdef AZTEC_C
    #include <functions.h>

    #ifdef PROTOS
        #include "LibTest.p"
        #include <CDROM/CDExtens.p>
    #endif PROTOS

#endif AZTEC_C

#ifdef LATTICE_50
    #ifdef PROTO
        #include <stdio.h>
        #include <proto/dos.h>
        #include <proto/exec.h>
        #include "LibTest.p"
        #include <CDROM/CDExtens.p>
    #endif PROTO

    #define NOT   !
    #define NOTEQ !=
#endif LATTICE_50

/***********************************
 *                                 *
 * Global Library Pointer          *
 *                                 *
 ***********************************/

    struct CDRomBase    *CDRomBase = NULL;

/***************************************************************************
 *                                                                         *
 *  NAME    :                                                              *
 *                                                                         *
 *                                                                         *
 *  SYNOPSIS:                                                              *
 *                                                                         *
 *                                                                         *
 *                                                                         *
 *                                                                         *
 *  FUNCTION:                                                              *
 *                                                                         *
 *                                                                         *
 *                                                                         *
 *                                                                         *
 *  RESULTS :                                                              *
 *                                                                         *
 *                                                                         *
 *                                                                         *
 *  BUGS    :                                                              *
 *                                                                         *
 *                                                                         *
 *                                                                         *
 *  SEE ALSO:                                                              *
 *                                                                         *
 ***************************************************************************
 *                                                                         *
 *  REVISION:                                                              *
 *                                                                         *
 *  Date        Author          Description                                *
 *  --------    --------------  -----------------------------------------  *
 *  00-00-90    A.M.Purtle      New Code                                   *
 *                                                                         *
 *                                                                         *
 ***************************************************************************/

main(LONG argc, BYTE **argv)

{ /* main */

    BPTR    lock;
    UBYTE   *buffer;


    if(argc NOTEQ 2)
        {
        printf("Usage: LibTest <Volume/Device>\n");

        return(RETURN_FAIL);
        }

    if(NOT (CDRomBase = (struct CDRomBase*)OpenLibrary(CDROMNAME,NULL) ) )
        {
        printf("Unable To Open %s\n",CDROMNAME);

        return(RETURN_FAIL);
        }

    if(NOT (lock = Lock(argv[1],SHARED_LOCK) ) )
        {
        printf("Object Not Found %s\n",argv[1]);

        CloseLibrary(CDRomBase);

        return(RETURN_FAIL);
        }

    if(NOT (buffer = (UBYTE*)AllocMem(CDBLOCKSIZE,MEMF_CLEAR | MEMF_PUBLIC) ) )
        {
        printf("No Free Store\n");

        UnLock(lock);

        CloseLibrary(CDRomBase);

        return(RETURN_FAIL);
        }

    printf("TEST FOR DISK TYPE\n\n");

    TestDiskType(lock);

    printf("TEST FOR VOLUME DESCRIPTORS\n\n");

    TestForVD(lock,buffer);

    printf("TEST DIRECTORY SCAN\n\n");

    TestDirScan(lock,buffer,argv[1]);

    FreeMem(buffer,CDBLOCKSIZE);

    UnLock(lock);

    CloseLibrary(CDRomBase);

    return(RETURN_OK);

} /* main */

/****** / ********************************************************************
*
*   NAME
*
*   SYNOPSIS
*
*   FUNCTION
*
*   INPUTS
*
*   RESULT
*
*   EXAMPLE
*
*   NOTES
*
*   BUGS
*
*   SEE ALSO
*
******************************************************************************
*                                                                            *
*   REVISION:                                                                *
*                                                                            *
*   Date        Author          Description                                  *
*   --------    --------------  -----------------------------------------    *
*   09-12-90    A.M.Purtle      New Code                                     *
*                                                                            *
*                                                                            *
*****************************************************************************/

VOID
TestDiskType(BPTR lock)

{ /* TestDiskType */

    LONG    disktype;

    disktype = GetDiskType(lock);

    switch(disktype)
        {
        case HSG:

                printf("    HiSierra Groupie Disc\n\n");
                break;

        case ISO:

                printf("    ISO 9660\n\n");
                break;

        case CDDA:

                printf("    Compact Disc Digital Audio\n\n");
                break;

        case ID_NO_DISK_PRESENT:

                printf("    ID_NO_DISK_PRESENT\n\n");
                break;

        case ID_UNREADABLE_DISK:

                printf("    ID_UNREADABLE_DISK\n\n");
                break;

        case ID_DOS_DISK:

                printf("    ID_DOS_DISK\n\n");
                break;

        case ID_FFS_DISK:

                printf("    ID_FFS_DISK\n\n");
                break;

        case ID_NOT_REALLY_DOS:

                printf("    ID_NOT_REALLY_DOS\n\n");
                break;

        case ID_KICKSTART_DISK:

                printf("    ID_KICKSTART_DISK\n\n");
                break;
        }

} /* TestDiskType */

/***************************************************************************
 *                                                                         *
 *  NAME    :                                                              *
 *                                                                         *
 *                                                                         *
 *  SYNOPSIS:                                                              *
 *                                                                         *
 *                                                                         *
 *                                                                         *
 *                                                                         *
 *  FUNCTION:                                                              *
 *                                                                         *
 *                                                                         *
 *                                                                         *
 *                                                                         *
 *  RESULTS :                                                              *
 *                                                                         *
 *                                                                         *
 *                                                                         *
 *  BUGS    :                                                              *
 *                                                                         *
 *                                                                         *
 *                                                                         *
 *  SEE ALSO:                                                              *
 *                                                                         *
 ***************************************************************************
 *                                                                         *
 *  REVISION:                                                              *
 *                                                                         *
 *  Date        Author          Description                                *
 *  --------    --------------  -----------------------------------------  *
 *  00-00-90    A.M.Purtle      New Code                                   *
 *                                                                         *
 *                                                                         *
 ***************************************************************************/

VOID
TestForVD(BPTR lock, UBYTE *buffer)

{ /* TestVD */

    LONG    instance;

    for(instance = 1L;instance < 6;instance++)
        {
        if( NOT (GetVD(lock,buffer,BOOT,instance) ) )
            printf("Unable To Find BootVD %ld\n",instance);
        else
            printf("    Found BootVD %ld\n",instance);

        if( NOT (GetVD(lock,buffer,PRIMARY,instance) ) )
            printf("Unable To Find PrimaryVD %ld\n",instance);
        else
            printf("    Found PrimaryVD %ld\n",instance);

        if( NOT (GetVD(lock,buffer,SUPPLEMENTARY,instance) ) )
            printf("Unable To Find SupplementaryVD %ld\n",instance);
        else
            printf("    Found SupplementaryVD %ld\n",instance);

        if( NOT (GetVD(lock,buffer,PARTITION,instance) ) )
            printf("Unable To Find PartitionVD %ld\n",instance);
        else
            printf("    Found PartitionVD %ld\n",instance);

        if( NOT (GetVD(lock,buffer,TERMINATING,instance) ) )
            printf("Unable To Find TerminatingVD %ld\n\n",instance);
        else
            printf("    Found TerminatingVD %ld\n\n",instance);
        }

} /* TestVD */

/***************************************************************************
 *                                                                         *
 *  NAME    :                                                              *
 *                                                                         *
 *                                                                         *
 *  SYNOPSIS:                                                              *
 *                                                                         *
 *                                                                         *
 *                                                                         *
 *                                                                         *
 *  FUNCTION:                                                              *
 *                                                                         *
 *                                                                         *
 *                                                                         *
 *                                                                         *
 *  RESULTS :                                                              *
 *                                                                         *
 *                                                                         *
 *                                                                         *
 *  BUGS    :                                                              *
 *                                                                         *
 *                                                                         *
 *                                                                         *
 *  SEE ALSO:                                                              *
 *                                                                         *
 ***************************************************************************
 *                                                                         *
 *  REVISION:                                                              *
 *                                                                         *
 *  Date        Author          Description                                *
 *  --------    --------------  -----------------------------------------  *
 *  00-00-90    A.M.Purtle      New Code                                   *
 *                                                                         *
 *                                                                         *
 ***************************************************************************/

VOID
TestDirScan(BPTR lock, UBYTE *buffer, UBYTE *Path)

{ /* TestDirScan */

    struct CDFileInfoBlock  *cdfib;
    struct FileInfoBlock    *fib;
    BPTR                    filelock;
    UBYTE                   *Buffer;
    UBYTE                   Name[256];


    if(cdfib = (struct CDFileInfoBlock*)AllocMem((LONG)sizeof(struct CDFileInfoBlock),MEMF_CLEAR | MEMF_PUBLIC) )
        {
        fib = &cdfib->FIB;

        CDExamine(lock,cdfib);

        printf("CD_EXAMINE Len   = %3ld EARLen = %ld LenFI  = %3ld Ver = %ld Name = %s\n",cdfib->Length,cdfib->EARLen,strlen(fib->fib_FileName),cdfib->Version,fib->fib_FileName);

        if(cdfib->LenSU)
            printf("CD_EXAMINE LenSU = %3ld DataSU = %s\n",cdfib->LenSU,cdfib->DataSU);

        while( CDExNext(lock,cdfib) )
            {
            printf("CD_EXNEXT  Len   = %3ld EARLen = %ld LenFI  = %3ld Ver = %ld Name = %s\n",cdfib->Length,cdfib->EARLen,strlen(fib->fib_FileName),cdfib->Version,fib->fib_FileName);

            if(cdfib->LenSU)
                printf("CD_EXNEXT  LenSU = %3ld DataSU = %s\n",cdfib->LenSU,cdfib->DataSU);


            if(cdfib->EARLen)
                {
                strcpy(Name,Path);

                if(Name[strlen(Name)-1] == ':')
                    strcat(Name,fib->fib_FileName);
                else
                    {
                    strcat(Name,"/");

                    strcat(Name,fib->fib_FileName);
                    }

                if(filelock = Lock(Name,SHARED_LOCK))
                    {
                    Buffer = (UBYTE*)AllocMem((LONG)CDBLOCKSIZE * cdfib->EARLen,MEMF_CLEAR);

                    if( GetExtAttRec(filelock,Buffer,(LONG)cdfib->EARLen) )
                        DisplayISOEar((struct ISOExtAttrRec *)Buffer);

                    FreeMem(Buffer,(LONG)CDBLOCKSIZE * cdfib->EARLen);

                    UnLock(filelock);
                    }
                }
            }

        FreeMem((UBYTE*)cdfib,(LONG)sizeof(struct CDFileInfoBlock));
        }

} /* TestDirScan */

/****** / ***
*
*   NAME
*
*
*   SYNOPSIS
*
*
*   FUNCTION
*
*
*   INPUTS
*
*
*   RESULT
*
*
*   EXAMPLE
*
*
*   NOTES
*
*
*   BUGS
*
*
*   SEE ALSO
*
*
******************************************************************************
*                                                                            *
*   REVISION:                                                                *
*                                                                            *
*   Date        Author          Description                                  *
*   --------    --------------  -----------------------------------------    *
*   00-00-90    A.M.Purtle      New Code                                     *
*                                                                            *
*                                                                            *
*****************************************************************************/

VOID
DisplayISOEar(struct ISOExtAttrRec *ISOEar)

{ /* DisplayISOEAR */

    UBYTE   systemid[33];
    UBYTE   systemuse[65];

    printf("EXTENDED ATTRIBUTE RECORD\n");

    printf("    OwnerID %ld  GroupID %ld  Permissions %ld\n",ISOEar->OwnerIDM,ISOEar->GroupIDM,ISOEar->Permissions);
    printf("    RecFmt  %ld  RecAttr %ld  RecLen %ld  ",ISOEar->RecFormat,ISOEar->RecAttrib,ISOEar->RecLenM);
    printf("EARVer  %ld  LenEsc  %ld  LenApp  %ld\n",ISOEar->EARVer,ISOEar->LenEscSeq,ISOEar->LenAppM);

    CopyMem(ISOEar->SysID,systemid,32L);
    systemid[32] = 0;
    printf("    SysID   %s\n",systemid);


    CopyMem(ISOEar->SysUse,systemuse,64L);
    systemuse[64] = 0;
    printf("    SysUse  %s\n",systemuse);

    printf("    Reserv  %s\n",ISOEar->Reserverd);
    printf("    AppUse  %s\n",ISOEar->AppUse);

    /*
    printf("    EscSeq  %s\n" ,ISOEar->EscSeq);
    */

} /* DisplayISOEAR */

