REM > BootEditCode:Boot

DEF PROCBoot_Scan
LOCAL Wide%

SYS"WimpExt_MiscOp", 1 TO ,,, Wide%
Wide% = 810 - Wide%

 CASE TRUE OF

  WHEN FNWind_GetIcon("Boot", "PreDesktop")
   Boot_Editing% = 1
   PROCBoot_LoadFile("PreDesktop", "small_feb")

  WHEN FNWind_GetIcon("Boot", "PreDesk")
   Boot_Editing% = 2
   PROCBoot_ScanDirectory("PreDesk")

  WHEN FNWind_GetIcon("Boot", "Desktop")
   Boot_Editing% = 3
   PROCBoot_LoadFile("Desktop", "small_fea")

  WHEN FNWind_GetIcon("Boot", "Tasks")
   Boot_Editing% = 4
   PROCBoot_ScanDirectory("Tasks")

 ENDCASE

PROCWind_IconGrey("Boot", "Remove",  1)
PROCWind_IconGrey("Boot", "Edit",    1)
PROCWind_IconGrey("Boot", "Open", -(Boot_Editing% = 1 OR Boot_Editing% = 3))

IF High% < 364 THEN High% = 364

B%!400 = 0
B%!404 = -High%
B%!408 = Wide%
B%!412 = 0
SYS"Wimp_SetExtent", FNObject_Hand(0, "BootPane", 0), B% + 400

 IF FNWind_Open("BootPane") THEN
  PROCWind_PopOpen("BootPane")
  B%!00 = FNObject_Hand(0, "BootPane", 0)
  SYS"Wimp_GetWindowState",, B%
  IF FNOpen_BootPane
  PROCWind_ForceRedraw("BootPane")
 ENDIF

BootPane_Wide% = Wide%

ENDPROC

REM 

DEF PROCBoot_LoadFile(File$, Icon$)
LOCAL List%, File%

IF NOT(FNWimp_FileExists(Boot_Path$ + ".Choices.Boot." + File$)) THEN ERROR Erro_Warn%, "%No" + File$

LOCAL ERROR
ON ERROR LOCAL RESTORE ERROR: PROCBoot_Error(File%)

PROCHeap_Lose(0, "BootList")
List% = FNHeap_Safe(4, 0, "BootList")
!List% = 0

File% = OPENIN(Boot_Path$ + ".Choices.Boot." + File$)
 WHILE NOT EOF#File%
  Line$ = GET$#File%

   IF LEFT$(Line$, 6) = "|Start" THEN
    !List% += 1
    IF NOT FNHeap_ReAlloc(List%, 4 + !List% * 80) THEN ERROR Erro_Memo%, ""
    List% = FNHeap_Find(0, "BootList")
    !(List% + 4 + 80 * (!List% - 1) + 00) = 0
    $(List% + 4 + 80 * (!List% - 1) + 04) = "S" + Icon$
    $(List% + 4 + 80 * (!List% - 1) + 20) = MID$(Line$, 8, 59)
   ENDIF

 ENDWHILE
CLOSE#File%

High% = !List% * List_High%

ENDPROC

REM 

DEF PROCBoot_Error(File%)
PROCHeap_Lose(0, "BootList")
Erro_ERL% = ERL
Erro_REPORT$ = REPORT$
Erro_ERR% = ERR
IF File% <> 0 THEN CLOSE#File%
ERROR ERR, REPORT$
ENDPROC

REM 

DEF PROCBoot_ScanDirectory(Directory$)
LOCAL Offset%, List%, Read%

IF NOT(FNWimp_FileExists(Boot_Path$ + ".Choices.Boot." + Directory$)) THEN ERROR Erro_Warn%, "%No" + Directory$

PROCHeap_Lose(0, "BootList")
List% = FNHeap_Safe(4, 0, "BootList")
!List% = 0

 REPEAT
  SYS"OS_GBPB", 12, Boot_Path$ + ".Choices.Boot." + Directory$, B% + 400, 1, Offset%, 112 TO ,,, Read%, Offset%

   IF Read% = 1 THEN
    !List% += 1
    IF NOT FNHeap_ReAlloc(List%, 4 + !List% * 80) THEN ERROR Erro_Memo%, ""
    List% = FNHeap_Find(0, "BootList")

     CASE B%!420 OF

      WHEN &1000
       Icon$ = "small_dir"

      WHEN &2000
       Icon$ = "sm" + LEFT$(FNWimp_GetString(B% + 424), 10)
       SYS"XWimp_SpriteOp", 40,, Icon$ TO ;PFlg%
       IF (PFlg% AND 1) THEN Icon$ = "small_app"

      OTHERWISE
       Icon$ = "small_" + RIGHT$("00" + STR$~(B%!420), 3)
       SYS"XWimp_SpriteOp", 40,, Icon$ TO ;PFlg%
       IF (PFlg% AND 1) THEN Icon$ = "small_xxx"

     ENDCASE

    !(List% + 4 + 80 * (!List% - 1) + 00) = 0
    $(List% + 4 + 80 * (!List% - 1) + 04) = "S" + Icon$
    $(List% + 4 + 80 * (!List% - 1) + 20) = FNWimp_GetString(B% + 424)
   ENDIF

 UNTIL Offset% = -1

High% = !List% * List_High%

ENDPROC

REM 

DEF PROCBoot_Edited(File$)
LOCAL File%, In%, Out%, Line$, In$, Done%

File% = OPENIN("Pipe:$." + File$)
IF EOF#File% THEN ENDPROC
Line$ = GET$#File%

In% = OPENIN(Boot_Path$ + ".Choices.Boot." + File$)
Out% = OPENOUT("<Wimp$ScrapDir>.BootEdit")

 WHILE NOT EOF#In%
  In$ = GET$#In%

   IF (In$ = Line$) AND NOT(Done%) THEN
     REPEAT
      IF In$ <> "" THEN BPUT#Out%, In$
      In$ = GET$#File%
     UNTIL In$ = "|End"
     REPEAT
      Line$ = GET$#In%
     UNTIL Line$ = "|End"
    Done% = TRUE
   ENDIF

  IF In$ <> "" THEN BPUT#Out%, In$
 ENDWHILE

 IF NOT Done% THEN
  BPUT#Out%, Line$
  REPEAT
   In$ = GET$#File%
   IF In$ <> "" THEN BPUT#Out%, In$
  UNTIL In$ = "|End"
 ENDIF

CLOSE#File%
CLOSE#In%
CLOSE#Out%

OSCLI"Copy <Wimp$ScrapDir>.BootEdit " + Boot_Path$ + ".Choices.Boot." + File$ + " ~CDFL~V"
 IF File$ = "PreDesktop" THEN
  OSCLI"SetType " + Boot_Path$ + ".Choices.Boot." + File$ + " FEB"
 ELSE
  OSCLI"SetType " + Boot_Path$ + ".Choices.Boot." + File$ + " FEA"
 ENDIF

PROCBoot_Scan

ENDPROC

REM 

DEF PROCBoot_Remove(File$)
LOCAL List%, Item%, In%, Out%, Line$

List% = FNHeap_Find(0, "BootList")

In% = OPENIN(Boot_Path$ + ".Choices.Boot." + File$)
Out% = OPENOUT("<Wimp$ScrapDir>.BootEdit")

 WHILE NOT EOF#In%
  Line$ = GET$#In%

   IF LEFT$(Line$, 6) = "|Start" THEN

    Item% = 1
     WHILE ($(List% + 4 + 80 * (Item% - 1) + 20) <> MID$(Line$, 8)) AND (Item% <= !List%)
      Item% += 1
     ENDWHILE

     IF (($(List% + 4 + 80 * (Item% - 1) + 20) = MID$(Line$, 8))) AND (!(List% + 4 + 80 * (Item% - 1) + 00) AND 1) THEN
       REPEAT
        Line$ = GET$#In%
       UNTIL Line$ = "|End"
      Line$ = GET$#In%
     ENDIF

   ENDIF

  IF Line$ <> "" THEN BPUT#Out%, Line$
 ENDWHILE

CLOSE#In%
CLOSE#Out%

OSCLI"Copy <Wimp$ScrapDir>.BootEdit " + Boot_Path$ + ".Choices.Boot." + File$ + " ~CDFL~V"
 IF File$ = "PreDesktop" THEN
  OSCLI"SetType " + Boot_Path$ + ".Choices.Boot." + File$ + " FEB"
 ELSE
  OSCLI"SetType " + Boot_Path$ + ".Choices.Boot." + File$ + " FEA"
 ENDIF

PROCBoot_Scan

ENDPROC

REM 

DEF PROCBoot_Delete(Directory$)
LOCAL List%, Item%

List% = FNHeap_Find(0, "BootList")

 FOR Item% = 1 TO !List%
   IF (!(List% + 4 + 80 * (Item% - 1) + 00) AND 1) THEN
    OSCLI("Wipe " + Boot_Path$ + ".Choices.Boot." + Directory$ + "." + $(List% + 4 + 80 * (Item% - 1) + 20) + " ~CFR~V")
   ENDIF
 NEXT

PROCBoot_Scan

ENDPROC

REM 

DEF PROCBoot_Edit(File$, Item%)
LOCAL List%, In%, Out%

List% = FNHeap_Find(0, "BootList")

In% = OPENIN(Boot_Path$ + ".Choices.Boot." + File$)
Out% = OPENOUT("Pipe:$." + File$)

 IF Item% = 0 THEN

  Item% = 1
   WHILE (!(List% + 4 + 80 * (Item% - 1) + 00) AND 1) = 0
    Item% += 1
   ENDWHILE

 ENDIF

 WHILE NOT EOF#In%
  Line$ = GET$#In%

   IF LEFT$(Line$, 6) = "|Start" THEN
     IF MID$(Line$, 8) = $(List% + 4 + 80 * (Item% - 1) + 20) THEN
      BPUT#Out%, Line$
       REPEAT
        Line$ = GET$#In%
        BPUT#Out%, Line$
       UNTIL Line$ = "|End"
     ENDIF
   ENDIF

 ENDWHILE

CLOSE#In%
CLOSE#Out%

OSCLI("SetType Pipe:$." + File$ + " FFF")
OSCLI("Filer_Run Pipe:$." + File$)

ENDPROC

REM 

DEF PROCBoot_Load(File$)
LOCAL File%, In%, Line$

IF File$ = "PreDesktop" AND B%!40 <> &FEB THEN ERROR Erro_Warn%, "%NotFEB"
IF File$ = "Desktop"    AND B%!40 <> &FEA THEN ERROR Erro_Warn%, "%NotFEA"

File% = OPENIN(FNWimp_GetString(B% + 44))
IF EOF#File% THEN CLOSE#File%: ERROR Erro_Warn%, "%NoHeader"
Line$ = GET$#File%
IF LEFT$(Line$, 6) <> "|Start" THEN CLOSE#File%: ERROR Erro_Warn%, "%NoHeader"
 WHILE NOT EOF#File%
  Line$ = GET$#File%
 ENDWHILE
IF Line$ <> "|End" THEN CLOSE#File%: ERROR Erro_Warn%, "%NoEnd"

PTR#File% = 0

In% = OPENUP(Boot_Path$ + ".Choices.Boot." + File$)
PTR#In% = EXT#In%

 WHILE NOT EOF#File%
  BPUT#In%, GET$#File%
 ENDWHILE

CLOSE#In%
CLOSE#File%

FOR A% = 0 TO !B% STEP 4: !(B% + 200 + A%) = B%!A%: NEXT
PROCBoot_Scan
FOR A% = 0 TO B%!200 STEP 4: B%!A% = !(B% + 200 + A%): NEXT

ENDPROC

REM 

DEF PROCBoot_Copy(Directory$)
LOCAL File$

File$ = FNWimp_GetString(B% + 44)
Leaf$ = FNWimp_GetLeaf(File$)
IF FNWimp_FileExists(Boot_Path$ + ".Choices.Boot." + Directory$ + "." + Leaf$) THEN ERROR Erro_Warn%, "%AlreadyExists"

SYS"Wimp_StartTask", "Copy " + File$ + " " + Boot_Path$ + ".Choices.Boot." + Directory$ + "." + Leaf$ + " ~C~D~FLR~V"

FOR A% = 0 TO !B% STEP 4: !(B% + 200 + A%) = B%!A%: NEXT
PROCBoot_Scan
FOR A% = 0 TO B%!200 STEP 4: B%!A% = !(B% + 200 + A%): NEXT

ENDPROC
