/* 

PKTShovel V1.1 freely distributable from the Plot Hatching Factory. 

Written my Mat 'Fingers' Bettinson and 10 jugs of coffee. 

Set the script flag on this file and copy into rexx: make sure your path is
also set up to rexx: too... Then you just have to type PKTshovel rather than
the rx etc... 

The systemname specifies a system in the config file. The config file
specifies the paths and addresses to be shoveled etc. 

This program handles file attaches and everything else in the flowfile.
Recognises what has to be deleted and not deleted. Datestamps packets as
they are moved in 8 digit hex form. netmail with .PKT and Echo mail with the
day and session stamp ie TH2, WE0 etc... 

Flowfiles and packets are deleted after copying. No checking is performed as
there is no reason why a copy should fail. It's down to your config. The
script works as I've tested it. Don't be paranoid now! :-) 

The Quiet switch turns off the copying and deleting output to the stdio.

The after-session script is configured in the config file again... It is
simply executed so you MUST set the script flag or it WILL NOT WORK. I
didn't execute it as it may be a DOS or Arexx script so I left it up to you.

Usage: [Rx] PKTShovel <systemname> [QUIET]

*/

EXEdir = 'C:'
Config = 'Xconfig:PKTShove.config'

ARG System quiet
Call TIME('R')

If system = "" then DO
 call fuckwit
 EXIT
 END

If ~ EXISTS(Config) then DO
 say 'And WHERE, pray tell, is the CONFIG? Hmmmm?!'
 EXIT
 END

If strip(quiet) = 'QUIET' then do
 Quiet = 0
 nil = '>NIL: '
 end
Else Do
 Quiet = 1
 nil = ''
 end

If quiet = 1 then DO
 say ''
 say '*** Welcome to Packet Shovel 1.1 by Mat Bettinson. ***'
 say ''
 END

call Readconfig

n = 1

Do i = 1 to numshove
 NetMtemp = Flowsrce.i||Flowaddr.i
 If exists(NetMtemp'.DUT') then DO
  NetmailFile = netMtemp'.DUT'
  call CopyNetMail
  END
 If exists(NetMtemp'.HUT') then DO
  NetmailFile = netMtemp'.HUT'
  call CopyNetMail
  END
 If exists(NetMtemp'.CUT') then DO
  NetmailFile = netMtemp'.CUT'
  call CopyNetMail
  END
 If exists(NetMtemp'.OUT') then DO
  NetmailFile = netMtemp'.OUT'
  call CopyNetMail
  END

 FlowDest = Flowdest.i

 If exists(NetMtemp'.FLO') then DO
  Flowfile = NetMtemp'.FLO'
  Call FlowfileExtract
  END
 If exists(NetMtemp'.HLO') then DO
  Flowfile = NetMtemp'.HLO'
  Call FlowfileExtract
  END
 If exists(NetMtemp'.CLO') then DO
  Flowfile = NetMtemp'.CLO'
  Call FlowfileExtract
  END
 END

address COMMAND
DosCOM

say ''
say 'Packet Shovel 1.1 finished.'
say ''
EXIT

/* Input: Flowfile and FlowDest. */ 

FlowfileExtract:

Call Open(Flow,Flowfile,'R')

Do until EOF(flow)
 Flowline = READLN(Flow)
 If Length(flowline) < 3 then break
 Flowline = translate(flowline,'^','#')
 If left(flowline,1) = '^' then DO
  flowline = Delstr(strip(flowline),1,1)
  Delete = 'YES'
  END
 ELSE delete = 'NO'
 Address COMMAND
 testfile = flowline
 Call getfiletype
 If testfile = 'PKT' then DO
  mailtype = 2
  FileEXT = right(flowline,4)
  call datestamp
  Address COMMAND
  EXEdir'Copy 'flowline' to 'Flowdest||stamp
  If quiet = 1 then say 'Copying 'flowline' to 'flowdest' as 'stamp 
  END
 If testfile = 'BIN' then DO
  Address COMMAND
  EXEdir'Copy 'flowline' to 'Flowdest
  If quiet = 1 then say 'Copying 'flowline' to 'flowdest
  END
 If delete = 'YES' then DO
  Address COMMAND
  EXEdir'Delete 'niL||flowline
  END
 END
eh = Close(flow)
address COMMAND
EXEdir'delete 'NIL||flowfile
RETURN

/* Routine that scans the config file and parses the commands one by one
  and executes instructions relevant to that command. System copies are 
  logged in an array and the total number of network addresses to be 
  copied is returned in numshove which is used as the upper limit for the 
  do (for next loop) loop which runs through checking for Netmail 
  packets and flow files... 
*/

ReadConfig:

Call Open(CFG,Config,"R")

Do UNTIL EOF(CFG)
 Do UNTIL EOF(CFG)
  raw = ReadLN(CFG)
  Parse UPPER VAR raw command readsys
  If command = 'SYSTEM' then break
  END
  readsys = strip(readsys)
  IF readsys = system then break
 END

If readsys ~= system then DO
 Say 'System not FOUND!!!'
 EXIT
 END

Numshove = 0

Flowsrce = 0
Flowdest = 0
FlowAddr = 0

DO UNTIL EOF(CFG)
 CFGLine = READLN(CFG)
 Parse UPPER VAR CFGline Command Data1 Data2 Data3
 SELECT
  WHEN Command = 'MOVE' then DO
   Numshove = numshove + 1
   Flowsrce.Numshove = strip(Data1)
   Flowdest.Numshove = strip(Data2)
   FlowAddr.Numshove = translate(strip(Data3),'..',':/')
   END
  WHEN Command = 'DOSCOM' then DO
   DOSCom = Data1||data2||data3
   END
  WHEN Command = 'SYSTEM' then DO
   fini = 1
   END
  OTHERWISE nop
  END
 If fini = 1 then break
 END

Call Close('CFG')

RETURN

/* Input Testfile. Output testfile = 'PKT' or 'BIN' */

getfiletype:

temp = translate(testfile,' ',':/')
temp = word(temp,words(temp))
temp = delstr(temp,length(temp)-2,2)
temp = compress(temp,'.')
If datatype(temp) = 'NUM' then DO
  testfile = 'PKT'
  END
ELSE testfile = 'BIN'
RETURN

/* stamp returns 8 digit hex number with .PKT. N is used to make unique.
   mailtype = 1 = netmail .PKT extension. 0 = echomail .TH3 etc... stripped
   off filename to be really flash. :-) 
 */

datestamp:
n = n + 1 
S = d2x((TIME('E') * 100) + n)
D = d2x(time('S'))
Z = d2x(date('I'))
stamp = right(Z||D||S,8)
ExtTod = '.'UPPER(Left(Date('W'),2))'0'
If mailtype = 1 then stamp = stamp'.PKT'
ELSE stamp = stamp||ExtTod
RETURN

/* Copies Netmailfile to flowdest renaming to datestamp.PKT */

CopyNetmail:
mailtype = 1
call datestamp
Address COMMAND
EXEdir'Copy 'Netmailfile' TO 'Flowdest.i||stamp
If Quiet = 1 then DO
  say 'Copying 'Netmailfile' to 'Flowdest.i' as 'stamp
  END
EXEdir'Delete 'NIL||Netmailfile
RETURN

fuckwit:
say 
say 'PKTShovel 1.1 from the Plot Hatching Factory.'
say "Written my Mat 'Fingers' Bettinson 10-nov-94"
say 
say 'USAGE: [RX] UnpackPKT <SYSTEM> [quiet]'
say
say 'Note: SYSTEM details in 'Config
say
return

