REM Title:      TreePickSM
REM Purpose:    To really confuse the author! :-)
REM Version:    0.03 19 September 2002
REM Copyright:  (C) Philip Ludlam 2002

REM This program is free software; you can redistribute it and/or modify it
REM under the terms of the GNU General Public License as published by the Free
REM Software Foundation; either version 2 of the License, or (at your option)
REM any later version.
REM
REM This program is distributed in the hope that it will be useful, but WITHOUT
REM ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
REM FITNESS FOR APARTICULAR PURPOSE. See the GNU General Public License for more
REM details.
REM
REM You should have received a copy of the GNU General Public License along with
REM this program; if not, write to the Free Software Foundation, Inc., 59 Temple
REM Place - Suite 330, Boston, MA 02111-1307, USA

DIM block% 1023
ON ERROR REPORT:PRINT ERL:END

REM Get command line
SYS "OS_GetEnv" TO comm%
SYS "OS_ReadArgs","name,quit,type,dir,spr,comm",comm%,block%,1023

IF block%!20=0 PRINT "TreePickSM: <filetype> <file, dir or system variable> <sprite> <command>":END
type$=FNstring(block%!8)
dirpath$=FNstring(block%!12)
sprite$=FNstring(block%!16)
command$=FNstring(block%!20)

IF LEFT$(dirpath$,1)="<" AND RIGHT$(dirpath$,1)=">" THEN
  SYS "XOS_ReadVarVal",MID$(dirpath$,2,LEN(dirpath$)-2),block%,1023 TO ,,r%;f%
  block%?r%=0
ELSE
  SYS "XOS_FSControl",37,dirpath$,block%,0,0,1023 TO ;f%
ENDIF
IF (f% AND 1)=1 THEN END
dirpath$=FNstring(block%)

C%=0:end%=LEN(dirpath$)+1
REPEAT
  oldC%=C%:C%=INSTR(dirpath$,",",C%+1)
  IF C%=0 C%=end%
  dir$=MID$(dirpath$,oldC%+1,C%-oldC%-1)
  SYS "XOS_FSControl",37,dir$,block%,0,0,1023 TO ;f%
  IF (f% AND 1)=0 THEN
    dir$=FNstring(block%)
    SYS "OS_File",17,dir$ TO exist%
    IF exist%>1 THEN
      IF INSTR(FNlcase(command$),"dynamic:") >0 THEN
        SYS "Director_Option",""""+FNleaf(dir$)+""" -sprite "+sprite$+" -sub ""Dynamic:/Director:Menus.Files.TreePick -type "+type$+" -dir "+dir$+" -submenu -command """""+command$+""""""""
      ELSE
        SYS "Director_Option",""""+FNleaf(dir$)+""" -sprite "+sprite$+" -sub ""Dynamic:/Director:Menus.Files.TreePick -type "+type$+" -dir "+dir$+" -command """""+command$+""""""""
      ENDIF
    ELSE
      IF exist%=1 THEN
        SYS "OS_FSControl",31,type$ TO ,,filetype%
        filetype$ = "small_"+RIGHT$("000"+STR$~filetype%,3)
        SYS "OS_SubstituteArgs"," ",block%,1023,command$,LEN(command$)
        command$=FNstring(block%)
        SYS "OS_SubstituteArgs"," ",block%,1023,command$,LEN(command$)
        command$=FNstring(block%)
        SYS "OS_SubstituteArgs",dir$,block%,1023,command$,LEN(command$)
        IF INSTR(FNlcase(command$),"dynamic:") >0 THEN
          SYS "Director_Option",""""+FNleaf(dir$)+""" -sprite "+filetype$+" -sub"""+FNstring(block%)+""""
        ELSE
          SYS "Director_Option",""""+FNleaf(dir$)+""" -sprite "+filetype$
          SYS "Director_Command",FNstring(block%)
        ENDIF
      ENDIF
    ENDIF
  ENDIF
UNTIL C%=end%

END

DEF FNstring(ptr%):LOCAL a$:a$=""
WHILE ?ptr%>31
  a$+=CHR$(?ptr%):ptr%+=1
ENDWHILE:=a$

DEF FNleaf(f$)
LOCAL l%
  l%=LEN f$:REPEAT l%-=1:UNTIL MID$(f$,l%,1)="."ORl%<1
=MID$(f$,l%+1)

DEF FNlcase(text$)
LOCAL Loop%,text2$,chr%
text2$=""
FOR Loop%=1 TO LEN(text$)
  chr%=ASC(MID$(text$,Loop%,1))
  IF chr%>64 AND chr%<91 chr%=chr%+32
  text2$+=CHR$(chr%)
NEXT
=text2$

