/**/
v="$VER: Mfwd WPLRX Modem Call Forwarded Packet Switching Williamson 55.00"
/*
    This in entered in Cache,CFG as follows:
    number      number of service
    presess     Mfwd.wplrx $<line> fwdnumber
        where fwdnumber phone number of actual site
    
*/
options RESULTS
options failat 99
signal on syntax
signal on halt
signal on ioerr
signal on break_c
signal on break_d

if ~show('L', "rexxsupport.library") then
    if ~addlib("rexxsupport.library", 0, -30, 0) then do
         say "Couldn't access support.library !"
        exit 20
    end

pragma("W","NULL")
log=show('P','ROOFLOG')
mailer=GetCLip('SHELTER')
l_mailer=lower(mailer)
sv='v'||right(v,5);script="MFWD"
cr    = '\r\n'  /* WPL  */
nl    = '0a'X   /* REXX */
bs    = '08'x
quote = '"'
parse arg line fwdphone 
    trys=0
    do forever
        if trys=3 then do
            Address "LOGPROC" 'PutLog 'l_mailer'wpl' time() line 'Server Connect Timeout after 'trys' trys'
            return 5
        end
        trys=trys+1
        resp=upper(wpl_prompt(3600,cr))
        if resp="" then iterate
        if index(resp,"AUCUNE ENTREE")>0  then do
            Address "LOGPROC" 'PutLog 'l_mailer'wpl' time() line 'Server Busy after 'trys' trys'
            return 5
        end
        if index(resp,"DESTINATION:")>0  then leave
    end

    Address "LOGPROC" 'PutLog 'l_mailer'wpl' time() line 'Getting Remote Modem'
    do forever
        if trys=3 then do
            Address "LOGPROC" 'PutLog 'l_mailer'wpl' time() line 'Timeout 'trys' on Remote Modem OK'
            return 5
        end
        trys=trys+1
        resp=upper(wpl_prompt(180,"AT"||cr))
        if resp="" then iterate    
        if index(resp,"DESTINATION:")>0  then do
            Address "LOGPROC" 'PutLog 'l_mailer'wpl' time() line 'Remote modem failed on 'trys
            return 5
        end
        if index(resp,"OK")>0  then leave
    end
    Address "LOGPROC" 'PutLog 'l_mailer'wpl' time() line 'Dialing 'fwdnumber
    resp=wpl_prompt(3600,"ATDT9-,"fwdnumber||cr)
    if index(resp,"CONNECT")=0 then return 0
    Address "LOGPROC" 'PutLog 'l_mailer'wpl' time() line 'Remote connect failed on 'trys
return 5

send:
    'Print' quote||arg(1)||quote
    'Send' quote||arg(1)||quote
return

wpl_prompt:
    'Print' quote||arg(2)||quote
nprompt:
    'Send' quote||arg(2)||quote
getstring:
    'GetInbound E0 'arg(1)
    'String $(event)'
    if upper(RESULT) = 'CARRIER' then do
        ADdress "LOGPROC" 'PutLog 'l_mailer'wpl' time() line fname 'Dropped Carrier'
        call cleanup
        exit
    end;else if upper(RESULT) = 'LOGIN' then do
        'String $(namebuf)'
        x=(RESULT) 
    end;else x=""
return x

lower:
return(bitor(arg(1),'20'x))

PutLog:  procedure expose log script
if arg(2) > GetClip('LOGLEVEL') & ~log then return 0
address 'ROOFLOG' 'logline' left(time(),5) script': 'arg(1)
address
return 0

addslash:
curr = arg(1)
select
    when right(curr, 1) = ":" then nop
    when right(curr, 1) = "/" then nop
        otherwise curr = curr"/"
end
return curr

/* a useful procedure by Walt Sullivan	*/
dequote:
    parse arg thing
    parse var thing '"' unq_thing '"'
    if unq_thing ~= "" then return unq_thing
return thing
cleanup:
return 0
break_c:
break_d:
    PutLog('User abort',10,10)
    call cleanup
    exit 10
novalue:
        call template_oops "Novalue" sigl
syntax:
        call template_oops "Syntax(RC=" || RC || ")" sigl
failure:
        call template_oops "Failure(RC=" || RC || ")" sigl
ioerr:
        call template_oops "IOErr" sigl 
halt:
        call template_oops "Halt" sigl 

template_oops:
        parse arg what badline
        PutLog('Error Line:'badline what,10,10)
        call cleanup
        exit(40)
/**/
    