#include <stdlib.h>
#include <sys/types.h>
#include <string.h>

#include <stdio.h>
#include <proto/exec.h>
#include <libraries/multiuser.h>
#include <proto/multiuser.h>
#include <dos/dos.h>
#include <proto/dos.h>
#include <clib/irmenlib_protos.h>

struct muBase *muBase;

int filepath(char *file,char *path);
BOOL hookFunc(struct FileInfoBlock *FIB, char *fullName, short recurlevel);

char version[]="$VER: chmod 1.0 (06.01.96) ©Thomas Zander";
ULONG bits=0;
BOOL mufs=TRUE;

/// main
void main(int argc,char* argv[])
{
    ULONG lang;
    BOOL rec=FALSE;
    if(*argv[1]=='?' || argc<2)
    {
        puts("Usage: chmod protection/a file\n\nprotection is a 4 digit hex number:\n The first digit stands for Amiga bits: Uspa\n The second digit stands for: rwed owner\n The third digit stands for: rwed group\n The fourth Digit stands for: rwed other\n\n");
        return;
    }
    sscanf(argv[1],"%x",&lang);
    if(lang>0xffff)
    {
        puts("4 digits would be enough");
        return;
    }
    if(argc>3 && !stricmp(argv[3],"all"))
        rec=TRUE;


    if(!(muBase = (struct muBase *)OpenLibrary("multiuser.library", 39)))
    {
		mufs=FALSE;
        puts("No friggin' multiuser.library");
        return;
    }

    // lang is dan 1234; 1:extra 2:not(standaard) 3:group 4: otr
    // moet worden 4312 met 2 geinverteerd.

    // not op standaard.
    lang=lang^0x0F00;

    bits=lang>>8;
    lang=lang<<4;
    bits=bits | (lang & 0x0F00);
    lang=lang<<8;
    bits=bits | (lang & 0xF000);
    if(bits & 0x0080)
        bits^=0x80000080;
    DirScan(argv[2],rec,hookFunc);
}
///

/// hookFunc
BOOL hookFunc(struct FileInfoBlock *FIB, char *fullName, short recurlevel)
{
    char error[128];
	if (mufs=TRUE)
	    if (muSetProtection(fullName,bits)==NULL) // unprotect if MuFS.
		{
        	Fault(IoErr(),fullName,error,128);
	        puts(error);
    	}
	else
    	if(SetProtection(fullName,bits)==NULL)  // Proctect if != MuFS.
	    {
	        Fault(IoErr(),fullName,error,128);
	        puts(error);
	    }
	return(TRUE);
}
///
