REM >ShareFS

OSCLI"Set Director$Menu ShareFS"

ON ERROR: ON ERROR OFF: ERROR ERR,REPORT$+" at "+STR$ ERL

REM type = 1 is disc
REM type = 5 is host

max_hosts = 32
DIM host$(max_hosts),host%(max_hosts)
max_discs = 128
DIM disc$(max_discs)

Menu=FNswi_number("Director_Menu")
EndMenu=FNswi_number("Director_EndMenu")
Option=FNswi_number("Director_Option")
Command=FNswi_number("Director_Command")
Dash=FNswi_number("Director_Dash")
Freeway_Enumerate=FNswi_number("Freeway_Enumerate")

DIM buffer% 256

REM get all the hosts and their IP addresses

host%=0
hosts=0
REPEAT
  SYS Freeway_Enumerate,0,5,256,buffer%,0,0,0,host% TO ,,len%,,,,host_ip%,host%

  IF host%<>-1 THEN
    IF hosts > max_hosts THEN ERROR 1,"Too many hosts"
    ?(buffer%+len%-1)=13
    host$(hosts)=$buffer%
    host%(hosts)=host_ip%
    hosts+=1
  ENDIF
UNTIL host%=-1

REM get all the discs, and discover their hostnames

disc%=0
discs=0
REPEAT
  SYS Freeway_Enumerate,0,1,256,buffer%,0,0,0,disc% TO ,,len%,,,,disc_ip%,disc%

  IF disc%<>-1 THEN
    IF discs > max_discs THEN ERROR 1,"Too many discs"
    ?(buffer%+len%-1)=13
    name$="Unknown"
    FOR i%=0 TO hosts-1
      IF disc_ip% = host%(i%) THEN name$=host$(i%)
    NEXT i%
    disc$(discs)=name$+CHR$(9)+$buffer%
    discs+=1
  ENDIF
UNTIL disc%=-1

SYS Menu,"<Director$Menu> -temp"

IF discs>0 THEN
  REM sort the disc and host names

  PROCqsort(0,discs-1)

  REM now make them into a menu

  old_host$=""
  FOR i%=0 TO discs-1
    a%=INSTR(disc$(i%),CHR$(9))
    disc_name$=MID$(disc$(i%),a%+1)
    host_name$=LEFT$(disc$(i%),a%-1)
    IF old_host$<>host_name$ THEN
      ip$="Unknown"
      FOR j%=0 TO hosts-1
        IF host_name$ = host$(j%) THEN
          host_ip%=host%(j%)
          ip$=STR$(host_ip% AND &FF)+"."+STR$((host_ip%>>8) AND &FF)+"."+STR$((host_ip%>>16) AND &FF)+"."+STR$((host_ip%>>24) AND &FF)
        ENDIF
      NEXT j%
      SYS Option,""""+host_name$+""" -bg 7 -fg 0 -sub ""Dynamic:Set Director$Menu IP||MMenu IP -temp||MOption "+ip$+"||MEndMenu"""
      old_host$=host_name$
    ENDIF

    SYS Option,""""+disc_name$+""" -sprite small_path -sub Path:Share::"+disc_name$+".$"
    SYS Command,"Filer_OpenDir Share::"+disc_name$+".$"
  NEXT i%
ELSE
  SYS Option,"None -grey"
ENDIF

SYS EndMenu

REM OSCLI"DirectorShowMenu <Director$Menu>"

END

DEF FNswi_number(name$)
  SYS "OS_SWINumberFromString",,name$ TO A%
=A%

DEF PROCqsort(from%,to%)
  LOCAL i%,j%,pivot$
  pivot$=disc$((from%+to%)/2)
  i%=from%
  j%=to%
  REPEAT
    WHILE disc$(i%) < pivot$
      i%=i%+1
    ENDWHILE
    WHILE disc$(j%) > pivot$
      j%=j%-1
    ENDWHILE
    IF i% <= j% THEN
      SWAP disc$(i%),disc$(j%)
      i%=i%+1
      j%=j%-1
    ENDIF
  UNTIL i% > j%
  IF i% < to% THEN
    PROCqsort(i%,to%)
  ENDIF
  IF j% > from% THEN
    PROCqsort(from%,j%)
  ENDIF
ENDPROC
