/*************************************************************************/
/* $VER: PicSort.rexx 1.0 (25.06.96)                                     */
/* Script to sort pictures into several directories                      */
/* Usage: rx PicSort                                                     */
/*************************************************************************/

IF ~(Exists("Libs:reqtools.library")&Exists("Libs:rexxreqtools.library")) THEN
      ADDRESS COMMAND "ASSIGN LIBS: "||Pragma("D")||"/Libs ADD"
IF ~Exists("C:VAREXX") THEN ADDRESS COMMAND "ASSIGN C: "||Pragma("D")||"/bin ADD"

gui_file = "Varexx_GUI:PicSort.GUI"
viewer = 'sys:Utilities/FastView %s '

path1 = ""
path2 = ""
path3 = ""
path4 = ""
path5 = ""
path6 = ""
path7 = ""

button1 = ""
button2 = ""
button3 = ""
button4 = ""
button5 = ""
button6 = ""
button7 = ""

result = ""

IF EXISTS("libs:rexxsupport.library") THEN DO
    IF ~SHOW("L","rexxsupport.library") THEN
        IF ~ADDLIB("rexxsupport.library",0,-30,0) THEN EXIT
END
ELSE EXIT

IF EXISTS("libs:rexxreqtools.library") THEN DO
    IF ~SHOW("L","rexxreqtools.library") THEN
        IF ~ADDLIB("rexxreqtools.library",0,-30) THEN EXIT
END
ELSE EXIT

OPTIONS RESULTS
OPTIONS FAILAT 10

SIGNAL ON SYNTAX
SIGNAL ON FAILURE 

/* Check Varexx is loaded if not load it */

IF SHOW( 'p', 'VAREXX' ) ~= 1 THEN DO
    ADDRESS COMMAND "run >NIL: varexx"
    ADDRESS COMMAND "WaitForPort VAREXX"
END
ADDRESS VAREXX

IF OPENPORT("HOLLY") = 0 THEN DO
    CALL rtezrequest "Could not open a port.",, "Varexx Error"
    EXIT
END

  'load ' gui_file 'HOLLY'

vhost = RESULT
ADDRESS VALUE vhost

show

/**************************************************************************/
/* MAIN LOOP -- Check for GUI events                                      */
/**************************************************************************/

DO FOREVER
    CALL WAITPKT("HOLLY")
    packet = GETPKT("HOLLY")
    IF packet ~= '00000000'x THEN DO
        class = GETARG(packet)
        event = SUBWORD(class,1,1)
        parameter = SUBWORD(class,2)
        if event = "CLOSEWINDOW" THEN LEAVE
        if event = "QUIT" THEN LEAVE
        if event = "CONFIG" THEN config()
        if event = "SORT" THEN sort()
    END
END

    'hide unload'    

    CALL CLOSEPORT( "HOLLY" )
EXIT

/*
** Subroutines
*/

config:
  BUSY SET
  SPAWN "HOLLY"
  ADDRESS value RESULT

  'show' "PicSort_CONFIG"
  show_config()

  DO FOREVER
      CALL WAITPKT("HOLLY")
      packet = GETPKT("HOLLY")
      IF packet ~= '00000000'x THEN DO
          class = GETARG(packet)
          event = SUBWORD(class,1,1)
          parameter = SUBWORD(class,2)

          if event = "CLOSEWINDOW" THEN LEAVE
          if event = "BUTTON1" THEN DO
              button1 = parameter
              path1 = getdir(path1)
          END
          if event = "BUTTON2" THEN DO
              button2 = parameter
              path2 = getdir(path2)
          END
          if event = "BUTTON3" THEN DO
              button3 = parameter
              path3 = getdir(path3)
          END
          if event = "BUTTON4" THEN DO
              button4 = parameter
              path4 = getdir(path4)
          END
          if event = "BUTTON5" THEN DO
              button5 = parameter
              path5 = getdir(path5)
          END
          if event = "BUTTON6" THEN DO
              button6 = parameter
              path6 = getdir(path6)
          END
          if event = "BUTTON7" THEN DO
              button7 = parameter
              path7 = getdir(path7)
          END
          if event = "GET1" THEN path1 = getdir(path1)
          if event = "GET2" THEN path2 = getdir(path2)
          if event = "GET3" THEN path3 = getdir(path3)
          if event = "GET4" THEN path4 = getdir(path4)
          if event = "GET5" THEN path5 = getdir(path5)
          if event = "GET6" THEN path6 = getdir(path6)
          if event = "GET7" THEN path7 = getdir(path7)
          if event = "VIEWER" THEN viewer = parameter
          if event = "OKAY" THEN LEAVE
          if event = "SAVE" THEN save_config()
          if event = "LOAD" THEN load_config()
          show_config()
      END
  END

  'hide'
  ADDRESS VALUE vhost
  BUSY
RETURN 0

load_config:
  call rtfreefilebuffer()
  drop config_file result
  tag = "rtfi_flags = freqf_patgad rtfi_matchpat = #?.cfg"
  config_file = rtfilerequest("","picsort.cfg","Load Configuration","Load",tag,result)
  if config_file ~= "" THEN DO
      OPEN(config,config_file,"READ")
        test = ReadLn(config)
        IF test = "PicSort_CONFIG" THEN DO
          button1 = ReadLn(config)
          path1 = ReadLn(config)
          button2 = ReadLn(config)
          path2 = ReadLn(config)
          button3 = ReadLn(config)
          path3 = ReadLn(config)
          button4 = ReadLn(config)
          path4 = ReadLn(config)
          button5 = ReadLn(config)
          path5 = ReadLn(config)
          button6 = ReadLn(config)
          path6 = ReadLn(config)
          button7 = ReadLn(config)
          path7 = ReadLn(config)
          viewer = ReadLn(config)
        END
        ELSE dummy=rtez_request("That's no config file","Okay","Error:","",dummy)
      CLOSE(config)
  END

RETURN 0

save_config:
  call rtfreefilebuffer()
  drop config_file result
  tag = "rtfi_flags = freqf_patgad rtfi_matchpat = #?.cfg"
  config_file = rtfilerequest("","picsort.cfg","Save Configuration","Save",tag,result)
  if config_file ~= "" THEN DO
      OPEN(config,config_file,"WRITE")
        WriteLn(config,"PicSort_CONFIG")
        WriteLn(config,button1)
        WriteLn(config,path1)
        WriteLn(config,button2)
        WriteLn(config,path2)
        WriteLn(config,button3)
        WriteLn(config,path3)
        WriteLn(config,button4)
        WriteLn(config,path4)
        WriteLn(config,button5)
        WriteLn(config,path5)
        WriteLn(config,button6)
        WriteLn(config,path6)
        WriteLn(config,button7)
        WriteLn(config,path7)
        WriteLn(config,viewer)
      CLOSE(config)
  END

RETURN 0

show_config:
  Settext "BUTTON1" button1
  Settext "DEST1" path1
  Settext "BUTTON2" button2
  Settext "DEST2" path2
  Settext "BUTTON3" button3
  Settext "DEST3" path3
  Settext "BUTTON4" button4
  Settext "DEST4" path4
  Settext "BUTTON5" button5
  Settext "DEST5" path5
  Settext "BUTTON6" button6
  Settext "DEST6" path6
  Settext "BUTTON7" button7
  Settext "DEST7" path7
  Settext "VIEWER" viewer
RETURN 0

sort:
  BUSY SET
  SPAWN "HOLLY"
  ADDRESS value RESULT

  Setlabel "SORT1" button1
  Setlabel "SORT2" button2
  Setlabel "SORT3" button3
  Setlabel "SORT4" button4
  Setlabel "SORT5" button5
  Setlabel "SORT6" button6
  Setlabel "SORT7" button7

  'show' "PicSort_SORT"

  if path1="" THEN SET "SORT1" DISABLE
  if path2="" THEN SET "SORT2" DISABLE
  IF path3="" THEN SET "SORT3" DISABLE
  if path4="" THEN SET "SORT4" DISABLE
  if path5="" THEN SET "SORT5" DISABLE
  IF path6="" THEN SET "SORT6" DISABLE
  IF path7="" THEN SET "SORT7" DISABLE

  call rtfreefilebuffer()
  drop result bild
  tag = "rtfi_flags = freqf_multiselect|freqf_patgad rtfi_matchpat = #?"
  result = rtfilerequest("Sys:","","Select Pictures to sort","Sort",tag,bild)

  if result ~= "" THEN DO

    DO i = 1 to bild.count

      viewstring = INSERT( bild.i , viewer , POS("%s", viewer )+1 )
      viewstring = DELSTR( viewstring , POS("%s",viewstring) , 2 )

      Settext "FILE" bild.i
      address command viewstring

      DO FOREVER
          CALL WAITPKT("HOLLY")
          packet = GETPKT("HOLLY")
          IF packet ~= '00000000'x THEN DO
              class = GETARG(packet)
              event = SUBWORD(class,1,1)
              parameter = SUBWORD(class,2)

              if event = "CLOSEWINDOW" THEN LEAVE
              SELECT
                 WHEN class = "KEYBOARD F1" THEN event = "SORT1"
                 WHEN class = "KEYBOARD F2" THEN event = "SORT2"
                 WHEN class = "KEYBOARD F3" THEN event = "SORT3"
                 WHEN class = "KEYBOARD F4" THEN event = "SORT4"
                 WHEN class = "KEYBOARD F5" THEN event = "SORT5"
                 WHEN class = "KEYBOARD F6" THEN event = "SORT6"
                 WHEN class = "KEYBOARD F7" THEN event = "SORT7"
                 OTHERWISE
              END
              if pos("SORT",event)~=0 THEN LEAVE
              if event = "DELETE" THEN LEAVE
              if event = "PLUS" THEN LEAVE
              if (event = "KEYBOARD") & (parameter = "DELETE") THEN LEAVE
          END
      END

      if event = "CLOSEWINDOW" THEN LEAVE
      if (event = "DELETE")|((event = "KEYBOARD")&(parameter = "DELETE")) THEN DO
          ret = rtezrequest("Delete file ?","_Delete|_Cancel","WARNING:",,result)
          if ret = 1 THEN ADDRESS COMMAND "delete "||bild.i
      END

      string = ""

      if event = "SORT1" THEN string = bild.i||" "||path1
      if event = "SORT2" THEN string = bild.i||" "||path2
      if event = "SORT3" THEN string = bild.i||" "||path3
      if event = "SORT4" THEN string = bild.i||" "||path4
      if event = "SORT5" THEN string = bild.i||" "||path5
      if event = "SORT6" THEN string = bild.i||" "||path6
      if event = "SORT7" THEN string = bild.i||" "||path7

      if string ~= "" THEN DO
          result = 0
          ADDRESS COMMAND "copy "||string
          result = RC
          IF RESULT = 0 THEN ADDRESS COMMAND "delete "||bild.i
      END

    END

  END

  'hide'
  ADDRESS VALUE vhost
  BUSY
RETURN 0

/*
** Subprocedures
*/

getdir:
  Parse Arg olddir

  drop dir result

  call rtfreefilebuffer()

  tag = "rtfi_flags = freqf_nofiles"
  dir = rtfilerequest(olddir,,"Select Directory","Okay",tag,result)

RETURN dir



/* Error messages */

failure:
    SAY "Error code" rc "-- Line" SIGL
    SAY EXTERNERROR
    'hide unload'
    CALL CLOSEPORT ("HOLLY")

EXIT

syntax:
    SAY "Error" rc  "-- Line" SIGL
    SAY ERRORTEXT( rc )
    'hide unload'
    CALL CLOSEPORT ("HOLLY")
EXIT
