/*rx
 *
 * ExtractFilesLhA.rexx - Extract selected files from an LhA archive previously
 *                        listed in a DOpus window by ListLha.rexx
 *
 * $VER: ExtractFilesLhA 40.8 (03/01/94) by Geoff Seeley
 *
 * Usage: ARexx command ExtractFilesLhA.rexx (from DOpus)
 *
 */

/* configuration variables (change these to suit your setup) */

LhaCommand    = 'XFH_Work:C/Archivers/File/LhA '
OutputWindow  = '>CON:30/145/640/100/LhA_Output/CLOSE/SCREENDOPUS.1 '
ExtractList   = 'T:lha_file_list'
ExtractAll    = 1 /* set to 1 if you want to extract all files at one time */
                  /* else set to 0 for file-by-file extraction (slow) */

/* misc. variables */

DOpusPort = 'DOPUS.1'

LhaExtractCmd  = '-x -m x '

/* make sure we've got somebody to talk to */

if showlist('Ports', DOpusPort) = 0 then do
   say 'Directory Opus ARexx port not found. Aborting.'
   call CleanUp
end

address 'DOPUS.1'
options results

TopText "Extracting File(s) From an LhA Archive"

Busy on

/* get window information from DOpus */

Status 3
CurrentWindow = result

if CurrentWindow = 0 then
   DestinationWindow = 1
else
   DestinationWindow = 0

/* get the path/name of the LhA archive file */

'Status 14 -1'
LhaArchive = result

/* make sure it's an LhA archive listing buffer */

if (IsLhAFile(LhaArchive) = 0) then do

   /* try other window */

   OtherWindow
   'Status 14 -1'
   LhaArchive = Result
   
   if (IsLhAFile(LhaArchive) = 0) then do

      Notify "Sorry, no LhA archive buffer found. You must use the ListLha button first."
      call CleanUp

   end

   /* adjust destination as we switched windows */

   if DestinationWindow = 0 then
      DestinationWindow = 1
   else
      DestinationWindow = 0

end

call FindLhAPath
LhaArchive = LhaPath || LhaArchive

/* check for existance of archive */

if ~exists(LhaArchive) then do

   Notify "Can't seem to find '" || LhaArchive || "'. Aborting."
   call CleanUp

end

/* get the destination path from the other window */

OtherWindow

'Status 13 -1'
DestinationPath = result

OtherWindow

/* check for valid destination path */

if DestinationPath = '' then do

   Notify "The destination path is invalid\Pick another destination"
   call CleanUp

end

/* get list of selected entries */

GetSelectedAll
SelectedEntries = result

if SelectedEntries = 'RESULT' then do

   Notify "Please select some files to extract first..."
   call CleanUp

end

NumberOfEntries = words(SelectedEntries)

/* extract the files */

if ExtractAll then

   call ExtractFileList

else

   call ExtractEachFile

/* update destination window, tell user we are finished */

Rescan DestinationWindow

TopText "Finished extracting selected file(s) from LhA archive."

call CleanUp

exit 0

/*---------------------------------------------------------------------------*/

ExtractEachFile: /* extract each selected file */

   do EntryNumber = 1 to NumberOfEntries

      /* get entry number, retrieve entry */

      Index = word(SelectedEntries, EntryNumber)

      GetEntry Index + 1
      Entry = result

      /* grab file name/path */

      File = substr(Entry, 10)

      TopText "Extracting " || File || "..."

      /* make sure user see's the entry */

      ScrollToShow Index

      /* form CLI command and extract the file(s) */

      CliCommand = LhaCommand || OutputWindow || LhaExtractCmd || LhaArchive
      CliCommand = CliCommand || ' "' || File || '" ' || DestinationPath

      address command CliCommand

      /* deselect this entry */

      selection = Index ||' '|| 0 ||' '|| 1
      SelectEntry selection

   end

return

/*----------------------------------------------------------------------------*/

ExtractFileList: /* build a list of selected files, extract list */

   /* toast old list */

   if exists(ExtractList) then

      delete(ExtractList)

   if ~open(FileList, ExtractList, 'W') then do

      Notify "Can't open file " || ExtractList
      call CleanUp

   end

   TopText "Creating file(s) list..."

   do EntryNumber = 1 to NumberOfEntries

      /* get entry number, retrieve entry */

      Index = word(SelectedEntries, EntryNumber)

      GetEntry Index + 1
      Entry = result

      /* grab file name/path, protect in quotes */

      File = substr(Entry, 10)
      File = '"' || File || '"'

      /* make sure user see's the entry */

      ScrollToShow Index

      /* put it in the file list */

      call ReplaceMetaChars
      writeln(FileList, File)

      /* deselect this entry */

      selection = Index ||' '|| 0 ||' '|| 1
      SelectEntry selection

   end

   close(FileList)

   /* form CLI command and extract the file(s) */

   TopText "Extracting file(s) from LhA archive..."

   CliCommand = LhaCommand || OutputWindow || LhaExtractCmd || LhaArchive
   CliCommand = CliCommand || ' @' || ExtractList || ' ' || DestinationPath

   address command CliCommand

return

/*--------------------------------------------------------------------------*/

IsLhAFile: procedure   /* look at extension, return 1 if right, else 0 */

   parse arg AFileName

   lps = lastpos(".", AFileName)               
   if lps = 0 then                           
      return 0

   FileExt = upper(right(AFileName,length(AFileName)-lps))

   if FileExt ~= "LHA" & FileExt ~= "LZH" then
      return 0
   else
      return 1

return 0

/*--------------------------------------------------------------------------*/

FindLhAPath: /* grab invisible file path to archive */

   /* find number of entries, path is the last one */

   'Status 6 -1'

   GetEntry Result
   LhaPath = Result

return
 
/*--------------------------------------------------------------------------*/

ReplaceMetaChars: /* replace special wildcards with ? */

   File = translate(File, '???', '()`', '?')

return

/*--------------------------------------------------------------------------*/

CleanUp: /* clean up files and exit */

   if exists(ExtractList) then
      delete(ExtractList)

   Busy off

   exit 0

return
