/* DRVSET.C -- set attrib of drive given on command line */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include "currdir.h"

void fail(char *s) { puts(s); exit(1); }

main(int argc, char *argv[])
{
    CDS far *drv;
    unsigned drive;
    int drvoff;

    /* to just turn off drives, program can be renamed DRVOFF */
    drvoff = strstr(strupr(argv[0]), "DRVOFF");
    
    /* what drive do they want? (accepts letters and numbers) */
    if (argc < 2)
    {
        if (drvoff)
            fail("usage: drvoff [drive]");
        else
            fail("usage: drvset [drive] <NET|PHYS|SUBST|JOIN|OFF>");
    }
    else if (argv[1][0] >= 'A')
        drive = toupper(argv[1][0]) - 'A';
    else
        drive = atoi(argv[1]);

    if (! (drv = currdir(drive)))
        fail("can't get current directory structure");

    /* just turn drive off */
    if (drvoff)
    {
        drv->flags = 0;
        return 0;
    }

    /* change drive state */
    if (argc > 2)
    {
        strupr(argv[2]);
        if (strstr(argv[2], "OFF"))     drv->flags = 0;
        if (strstr(argv[2], "NET"))     drv->flags |= NETWORK;
        if (strstr(argv[2], "SUBST"))   drv->flags |= SUBST;
        if (strstr(argv[2], "JOIN"))    drv->flags |= JOIN;
        if (strstr(argv[2], "PHYS"))    drv->flags |= PHYSICAL;
    }

    /* print current drive state */
    if (! drv->flags)           fputs("INVALID ", stdout);
    if (drv->flags & NETWORK)   fputs("NET ", stdout);
    if (drv->flags & SUBST)     fputs("SUBST ", stdout);
    if (drv->flags & JOIN)      fputs("JOIN ", stdout);
    if (drv->flags & PHYSICAL)  fputs("PHYSICAL ", stdout);
    putchar('\n');

    return 0;
}
