/*
 *  TestResource.c
 *      © LFSoft 1995
 *
 *      This code is fully a dommain public: Use it as you want...
 *
 *  Test resources registering and notifications.
 *
 *  22/07/1995  First version
 *  29/07/1995  Add sharing
 *
 *   Note:
 *       * All libraries (but registery.library) are openned and closed
 *   automaticaly by DICE's autoinit feature. You must add some code for
 *   others compilers.
 */

#include <proto/exec.h>
#include <proto/registry.h>
#include <dos/dos.h>    // CTRL-C
#include <stdio.h>
#include <stdlib.h>

struct Library *RegistryBase=NULL;

APTR resid=NULL;    // resource id
char opt;
BOOL succ= FALSE;

void lib( void ){   // Free allocated resources
    if(RegistryBase){
        switch(opt){    // According to the used option
        case 'l':
        case 's':
            if(succ)
                RL_UnLockResource(resid); break;
        case 'd':
            RL_EnableNotifying(resid); break;
        }
        RL_UnRegisterResource(resid);
        CloseLibrary(RegistryBase);
    }
}

void main(int ac, char **av){
    opt=FALSE;

    if(ac>1) opt = (*av[1] == '-');

    if(ac < (opt?3:2) ){ // Some help ?
        puts("Usage :\n\tTestResource [-l] Nom\n"
            "Options:\n"
            "\t-l Exclusively Lock this resource\n"
            "\t-s Share this resource\n"
            "\t-d Desable notification\n"
            "\t-n Notify\n"
            );
        exit(10);
    }

    if(!(RegistryBase = OpenLibrary("registry.library",0))){
        puts("Can't open registry.library");
        exit(20);
    }
    atexit(lib);

    printf("-> %08x\n",resid=RL_RegisterResource(av[opt?2:1]));

    if(opt) switch(opt=av[1][1]){
        case 'l':
            printf("Locking -> %d\n",succ=RL_LockResource(resid,FALSE)); break;
        case 's':
            printf("Sharing -> %d\n",succ=RL_LockResource(resid,TRUE)); break;
        case 'd':
            RL_DisableNotifying(resid); break;
        case 'n':
            RL_Notify(resid); break;
        default:
            puts("Unsupported option");
            exit(0);
    }

    puts("CTRL-C to exit");
    Wait(SIGBREAKF_CTRL_C);
    exit(0);
}
