
#include <local/xmisc.h>

static long OpenMask = 0;

long GfxBase;
long IntuitionBase;
long ExpansionBase;
long DiskfontBase;
long TranslatorBase;
long IconBase;
long MathBase;
long MathTransBase;
long MathIeeeDoubBasBase;
long MathIeeeSingBasBase;
long LayersBase;
long ClistBase;
long PotgoBase;
long TimerBase;
long DResBase;
long xfiller15;

struct OLI {
    char *name;
    long *var;
};

struct OLI strvar[] = {
  "graphics",           &GfxBase,
  "intuition",          &IntuitionBase,
  "expansion",          &ExpansionBase,
  "diskfont",           &DiskfontBase,
  "translator",         &TranslatorBase,
  "icon",               &IconBase,
  "mathffp",            &MathBase,
  "mathtrans",          &MathTransBase,
  "mathieeedoubbas",    &MathIeeeDoubBasBase,
  "mathieeesingbas",    &MathIeeeSingBasBase,
  "layers",             &LayersBase,
  "clist",              &ClistBase,
  "potgo",              &PotgoBase,
  "timer",              &TimerBase,
  "dres",               &DResBase,
  "x15",                &xfiller15
};


openlibs(mask)
unsigned short mask;
{
    register struct OLI *sv;
    register short i;
    char buf[64];

    for (i = 0; i < sizeof(strvar)/sizeof(strvar[0]); ++i) {
	sv = strvar + i;
	if (mask & (1 << i)) {
	    strcpy(buf, sv->name);
	    strcat(buf, ".library");
	    if (*sv->var == 0 && (*sv->var = OpenLibrary(buf, 0L)) == 0) {
		closelibs(mask);
		return(0);
	    }
	    OpenMask |= 1 << i;
	}
    }
    return(1);
}

/*
 * CLOSELIBS(mask)
 *
 *	Close the indicated libraries.	Does not close libraries which
 *	have not been openned with OPENLIBS()
 */

closelibs(mask)
unsigned short mask;
{
    register struct OLI *sv;
    register short i;

    for (i = 0; i < sizeof(strvar)/sizeof(strvar[0]); ++i) {
	sv = strvar + i;
	if ((mask & (1 << i)) && *sv->var && (OpenMask & (1 << i))) {
	    CloseLibrary(*sv->var);
	    *sv->var = 0L;
	    OpenMask &= ~(1 << i);
	}
    }
}

