/*
  $VER: DO5-Unpack_kat.rexx 1.0 (19.12.95) Georges 'Melkor' Goncalves
  Written by Georges 'Melkor' Goncalves and Eric Sauvageau.

  ARexx Script for DOpus 5 to decrunch selected files via XPKatana.

  Usage:

  <ARexx>   DO5-Unpack_kat.rexx

  Note: XPKatana is copyright © Eric Sauvageau and it does NOT belong to the
        DOpus5 distribution. It's ShareWare.

*/

OPTIONS RESULTS
OPTIONS FAILAT 25

/* Adjust this as needed */
XPKATANA_PATH = "SYS:Utilities/XPKatana"

/* Should check if DOpus is really there */

  If ~SHOW('p',"DOPUS.1") Then Do
     Say "This script should be used with DirectoryOpus running!"

     Exit 20
  End

/* We have to actually verify is XPKatana is running.          */
  If ~SHOW('p',"KATANA") Then Do

/* IF XPKatana is not already running, start it !                     */
      started = 1
      Address Command "run <>NIL: "||XPKATANA_PATH||" ICONIFY"
      Address Command "WaitForPort KATANA"
  End

address 'DOPUS.1'

lister query source /* Is there any source lister ? */
SRCHandle=RESULT

if SRCHandle="RESULT" Then Do
  dopus request '"You need a source lister to use this script :-)" Damn!'

  Address 'KATANA'
  If started = 1 Then Do
     Quit
  End
  Exit 0
end


lister query SRCHandle selfiles  /* Returns the name of selected files separated by space */
Files=RESULT

lister query SRCHandle numselfiles ; selected = result

if selected=0 Then Do
  dopus request '"No source file(s) selected!" Damn!'

  Address 'KATANA'
  If started = 1 Then Do
     Quit
  End

  Exit 0
end

/* Busy the lister */
lister set SRCHandle busy 1


lister query SRChandle path  /* Returns the source path */
Path = RESULT

Address 'KATANA'

GetFlag NOPROGRESS ; oldflag = result
SetFlags NOPROGRESS 1

Do Forever
   Parse Var Files afile Files   /* Pull the first file and keep the remain */
   If afile == '' Then Leave
   completefile = path||Strip(afile,'B','"')   /* Add path, strip "" */

   SetSource completefile
   UnPack  ; error = result 

   /* Un-select the processed file */
   Address 'DOPUS.1' 

   Address 'DOPUS.1' 
   If error = 'ABORT' Then Do
      dopus request '"Error while unpacking "'||afile||'"!" Damn!'
   End

   lister select SRCHandle afile off

   Address 'KATANA'  /* Switch back to the Katana port */
End


/* If we started XPKatana, close it down */
If started = 1 Then Do
   Quit
End
Else Do
   SetFlags NOPROGRESS oldflag
End


/* Update lister */
Address 'DOPUS.1'

lister set SRCHandle busy 0              /* Un-busy the lister */
lister refresh SRChandle
