REM >HBRepeat
REM  Harriet Bazley 2nd August 2001
REM applies a command to contents of directory specified
REM 16th October 2001: added -CommandTail, -verbose, -Task options
ON ERROR ON ERROR OFF:PROCparamerror
DIM output% 1024,block% 1024
SYS"OS_GetEnv" TO env$
REM PRINT env$
REM env$="BASIC -quit Director:Utils.HBRepeat temp -d -f -a  Sprite"
REM PRINT env$
SYS"OS_ReadArgs","Basic/A,Quit/A/K,instruction/A,pathname/A,Directories/S/K,Files/S/K,Applications/S/K,Type/K,verbose/S/K,CommandTail/K,Task/S/K",env$,output%,1024
type$="":apps%=0:dirs%=0:files%=0
recurse%=FALSE

files%=output%!20
dirs%=output%!16
apps%=output%!24
verbose%=output%!32
task%=output%!40
ON ERROR: ON ERROR OFF: PRINT REPORT$;" at line ";ERL:END

pathname$=FNs(output%!12)
command$=FNs(output%!8)
IF output%!28 THEN type$=FNs(output%!28)
tail$="":IF output%!36 THEN tail$=" "+FNs(output%!36)

IF type$<>"" THEN SYS"OS_FSControl",31,type$ TO ,,type% ELSE type%=0
IF files%=0 AND dirs%=0 AND apps%=0 THEN files%=1:dirs%=1:apps%=1
PROCscan(pathname$)
END

DEF PROCparamerror
IF ERR=490 THEN PRINT "Syntax: HBRepeat <command> <directory path> [-Directories | -Applications | -Files | -Type <type>]":END
PRINT REPORT$;" at line ";ERL:END
ENDPROC

REM Procedures written by Scott Boham 
DEF PROCscan(dir$)
LOCAL i
i=0
REPEAT
SYS "OS_GBPB",12,dir$,block%,1,i,128,0 TO ,,,read,i
IF read=1 THEN
ob$=FNs(block%+24)
CASE block%!16 OF
WHEN 1: IF block%!20 = type% OR files%>0 THEN PROCcommand(dir$+"."+ob$)
WHEN 2: IF LEFT$(ob$,1)="!" THEN
   IF apps%>0 THEN PROCcommand(dir$+"."+ob$)
 ELSE
   IF dirs%>0 THEN PROCcommand(dir$+"."+ob$)
 ENDIF
ENDCASE
ENDIF
UNTIL i=-1
ENDPROC

DEF FNs(a)
LOCAL a$
WHILE ?a>31
a$=a$+CHR$(?a)
a=a+1
ENDWHILE
=a$

DEF PROCcommand(path$)
path$=command$+" "+path$+tail$
IF verbose% THEN OSCLI"Echo HBRepeat: "+path$
IF task% THEN path$="WimpTask "+path$
OSCLI path$
ENDPROC
