/* Archie.rexx -- a backup utility by <CB> aka Christian Balzer
   based on du.rexx by Larry Phillips.
    _  _
 / /  | \ \  <CB> aka Christian Balzer  - The Software Brewery -
< <   |-<  > UUCP: decwrl!frambo.dec.com!cb OR cb@frambo.dec.com
 \ \_ |_/ /  CIS : 71001,210 (be brief!) | Phone: +49 6150 4151 (CET!)
------------ Mail: Im Wingertsberg 45, D-6108 Weiterstadt, F.R.G.

Usage: [rx] Archie SOURCE DESTINATION [-options]

SOURCE and DESTINATION are valid AmigaDOS path's WITHOUT trailing
slashes '/'.
Valid options are: Currently none...

Archie will copy all files that don't have the archive bit set
from SOURCE and it's subdirectories to DESTINATION. If these
directories don't exist at DESTINATION, Archie will try to create
them on the fly and thus preserving the original structure at SOURCE.
After the file has been copied, it's archive bit will be set.

Note: The files will be copied with 'CLONE' flag of the COPY command
set. This of course only works with the 1.3 version of COPY.
In this version of Archie, filenames or directories containing blanks
(like "My Dir:My File"), are NOT supported!
In version 1.7 the form "a add" for Protect was changed to "add a" to
support the ARP 1.3 protect command which isn't fully compatible.  */

/* This is Public Domain, read the source and learn */

say 'Archie 1.8 (05-May-89) by <CB>'

/* open the Rexx support library */

if ~show('L',"rexxsupport.library") then do
   if addlib('rexxsupport.library',0,-30,0) then
      say 'added rexxsupport.library'
   else do;
      say 'support library not available'
      exit 10
      end
   end

address command
call pragma 'priority',-1

arg rein

/* The parser */
copt = 'clone'

if words(rein) < 2 then do
        say 'Come on, gimme a SOURCE *AND* a DESTINATION path'
        say 'Ya better try again... Bye!'
        exit 10
end


root = subword(rein,1,1)
dest = subword(rein,2,1)

if ~ exists(root) then do
        say 'Source not found'
        say 'Exiting... Check your parameters'
        exit 10
end

if ~ exists(dest) then do
        say 'Destination not found'
        say 'I''ll try to create it for you...'
        'makedir 'dest
        if ~ exists(dest) then do
                say 'Exiting... Check your parameters'
                exit 15
        end
        say 'Created 'dest
end

if right(root,1) ~= ':' then
  root = root || '/'
if root = '/' then root = ''

if right(dest,1) ~= ':' then
  dest = dest || '/'
if dest = '/' then dest = ''

rlen = length(root)

bytes = 0
blocks = 0
files = 0
dircount = 1

say 'Archie is doing it''s job on 'root

call dolist(root)

say 'Archived:' bytes 'bytes,' blocks 'blocks, ',
files 'files in a total 'dircount' directories.'
say
exit 0
 .

/* The real thing */

dolist: procedure expose bytes blocks files dest rlen dircount copt
parse arg x
contents = showdir(x);
do i = 1 to words(contents)
  temp = x || word(contents,i)
  flen = length(word(contents,i))
  type = statef(temp)
  if word(type,1) = 'FILE' then
    do
      if substr(word(type,4),4,1) = '-' then
        do
                target = dest || right(temp,(length(temp) - rlen))
                target = left(target,(length(target) - flen))
                'copy 'temp target copt
/*              if RC > 0 then
                  say 'Error archiving 'temp
                else */
                do
                  files = files + 1
                  bytes = bytes + word(type,2)
                  blocks = blocks + word(type,3) + 1
                  'protect 'temp 'add a'
/*                if RC > 0 then
                    say 'Error protecting 'temp
                  else */
                  say 'Archived: 'temp
                end
        end
    end
  if word(type,1) ='DIR' then do
        subdir = dest || right(temp,(length(temp) - rlen))
        if ~ exists(subdir) then
          do
            'makedir' subdir
            if ~ exists(subdir) then do
              say 'Sorry... Exiting'
              exit 20
            end
            say ' *** Created directory' subdir
          end
        call dolist(temp || '/')
        dircount = dircount + 1
  end
end
return
