/*
 *
 * Rename file(s) with TwinExpress from DOpus.
 *
 * (c) 1994 by K.P. van Beem (2:280/464.2, patrick.van.beem@aobh.xs4all.nl)
 *
 * Based on the DOpusLhaARexx package by Geoff Seeley (but you can better
 * use GuiArc in stead of DOpus and a script, to deal with archives)
 *
 *    Mods  By Ray Abram
 *     - If called on a DOpus Dir, then will tell Dopus to do a Rename
 */

DOpusPort   = 'DOPUS.1'

if ~show(l,"rexxsupport.library") then        
   call addlib("rexxsupport.library",0,-30,0)
if showlist('Ports', DOpusPort) = 0 then do
   say 'Directory Opus Arexx port not found. Aborting.'
   call CleanUp
end

address 'DOPUS.1'
options results

/* setup DOpus window and tell user what's happening */
Busy on
TopText "Renaming selected files..."

/* Get the current path and do file rename */
'Status 6 -1'
GetEntry Result
FilePath = Result
if left(FilePath,1) ~= '*' then do
   TopText "You are not in a 'Twin' directory. !!"
   Rename
   Busy off
   exit
end

FilePath = SubStr(FilePath,2)
GetSelectedAll
SelectedEntries = result
if SelectedEntries = 'RESULT' then do
   TopText "No files selected. ??"
   call CleanUp
end
NumberOfEntries = words(SelectedEntries)
do EntryNumber = 1 to NumberOfEntries
   Index = word(SelectedEntries, EntryNumber)
   GetEntry Index+1
   Entry = result
   File = strip(left(Entry,25))
   getstring '"Rename' File 'as ?"' File
   NewFile = Result
   if NewFile="" | rc~=0 then do
      TopText "You have to specify a new name !!"
      call CleanUp
   end
   if words(Filepath) > 1 then do
      Request "Spaces in a Filename are not Allowed !!"
       exit
   end

   SD = EnterDir(FilePath)
   address command 'echo >PPipe: rename' SD || File  SD || NewFile
   selection = Index||' 0 0'
   SelectEntry selection 0 1
end

TopText "Ready"
'DisplayDir -1'
address AREXX "Rexx:DOpus/Reread.rexx"
call CleanUp

exit

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

CleanUp: /* Remove any files and exit */
   Busy off
   exit
return

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

Quote: procedure /* add quotes to string */
   parse arg string
return '"'||string||'"'

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

GetWord: procedure /* get word from '|' separated string */

  parse arg number,words

  if(left(words,1) ~= '|') then
     words = '|'||words
  do i=1 to number
     idx = index(words, '|');
     words = substr(words, idx+1)
  end
  end = index(words, '|') - 1
  if words = "" then
     return ""
  ret_str = substr(words, 1, end)
return ret_str

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

CountWords: procedure /* count words from '|' separated string */

   parse arg words

   count = 0
   idx = index(words, '|')
   do while idx ~= 0
     count = count + 1
     words = substr(words, idx+1)
     idx = index(words, '|')
   end
return count

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

/* Cd into the sent Directory path
   - this is needed, as TWIN has a command parameter limit of 30 characters,
      and as we all know AmigaDos file & paths can easily go over this limit !!*/

EnterDir: procedure

   parse arg Dev ':' Path

   /* get the 1st character to address the local or remote machine correctly */
   if left(Dev,1) = '~' then
      sbit = '~'
     else
      sbit = ''

   /* does the passed name have a DEVICE in it ? */
   if Dev ~= '' then
      address command 'echo >PPipe: cd' Dev || ':'

   do until (t2 = '')
      parse var Path t1 '/' t2
      path = t2
      if t1 ~= '' then
         address command 'echo >PPipe: cd' sbit || t1
   end

return sbit
