/* $VER: Connect 1.4  (07.06.96)        */
/* Connects your Amiga to the Internet */
/*
    Name
        Connect

    Usage
        Connect [MAIL|NEWS|AUTOMAIL|AUTONEWS|OFF|AUTOOFF]

    Options
        None:       Connects to Internet
        MAIL:       Connects to Internet and fetches mail
        NEWS:       Connects to Internet and fetches news and mail
        AUTOMAIL:   Connects to Internet, fetches mail and disconnects
        AUTONEWS:   Connects to Internet, fetches news and mail and disconnects
        OFF:        Disconnects from Internet
        AUTOOFF:    Disconnects from Internet after waiting for news and mail to finish

*/

/* These are specific to your setup */

DevName    = 'DEVS:Networks/ppp.device'                             /* Your network device */
DevUnit    = 0                                                      /* its unit number */
Interface  = 'ppp'||DevUnit                                         /* Interface name */
BeforeCon  = ''                                                     /* Commands to be executed */
AfterCon   = ''                                                     /* before and after */
BeforeDis  = ''                                                     /* connection and */
AfterDis   = ''                                                     /* disconnection */

/* Don't change anything below here */

if ~show('L','rexxdossupport.library') then
    call addlib('rexxdossupport.library',0,-30)

BinPath    = 'AmiTCP:bin/'                                          /* Paths to AmiTCP commands */
ThorPath   = GetVar('Thor/ThorPath')
StartNet   = BinPath||'StartNet'
ifconfig   = BinPath||'ifconfig'
route      = BinPath||'route'
online     = BinPath||'online'
offline    = BinPath||'offline'
resolve    = BinPath||'resolve'
SendEvents = BinPath||'SendEvents'
GetMail    = BinPath||'Fetch Mail'
GetNews    = BinPath||'Fetch All'
LogPath    = 'UUSpool:Connect.log'
SendProc   = ThorPath||'bin/SendTCP'                                /* CLI process names for news and mail */
GetProc    = ThorPath||'bin/GetTCP'
Unbatch    = BinPath||'ParseThor'

options results
address command
arg opt

/* Main routine */

select
    when opt = '' then call Connect
    when opt = 'MAIL' then do
        call Connect
        call DoMail
        end
    when opt = 'NEWS' then do
        call Connect
        call DoNews
        end
    when opt = 'AUTOMAIL' then do
        call Connect
        call DoMail
        call Auto
        call Disconnect
        end
    when opt = 'AUTONEWS' then do
        call Connect
        call DoNews
        call Auto
        call Disconnect
        end
    when opt = 'OFF' then call Disconnect
    when opt = 'AUTOOFF' then do
        call Auto
        call Disconnect
        end
    otherwise call BadArgs
    end

exit

/* Procedures */

Connect:                                                        /* Establish connection */
    BeforeCon
    StartNet
    unsetenv ppp0IPLocal
    unsetenv ppp0IPRemote
    online DevName DevUnit
    if RC > 0 then call ConnectionFailed
    ifconfig 'lo0 localhost'
    ifconfig Interface '$ppp0IPLocal $ppp0IPRemote'
    route 'add default $ppp0IPRemote'
    call WriteLog('Connection made')
    'SetEnv NetState Online'
    'run >NIL:' SendEvents
    AfterCon
    Wait 3
    return

Disconnect:                                                     /* Break connection */
    BeforeDis
    ifconfig interface 'down'
    offline DevName DevUnit
    route 'delete >NIL: $ppp0IPLocal'
    route 'delete >NIL: default'
    call WriteLog('Connection closed')
    'SetEnv NetState Offline'
    Unbatch
    AfterDis
    return

DoMail:
    'run >NIL:' GetMail                                             /* Start mail download */
    call WriteLog('Mail download started')
    return

DoNews:
    'Run >NIL:' GetNews                                             /* Start mail and news download */
    call WriteLog('News download started')
    return

Auto:
    say 'Waiting for news/mail to finish'                           /* Wait for mail and news processes to stop */
    call WaitToEnd(SendProc GetProc)
    Wait 2
    call WaitToEnd(SendProc GetProc 'ncftp')
    return

BadArgs:                                                            /* Give usage information */
    say
    say 'Usage: Connect [MAIL|NEWS|AUTOMAIL|AUTONEWS|OFF|AUTOOFF]'
    say
    say '       None:       Connects to Internet'
    say '       MAIL:       Connects to Internet and fetches mail'
    say '       NEWS:       Connects to Internet and fetches news and mail'
    say '       AUTOMAIL    Connects to Internet, fetches mail and disconnects'
    say '       AUTONEWS:   Connects to Internet, fetches news and mail and disconnects'
    say '       OFF:        Disconnects from Internet'
    say '       AUTOOFF:    Disconnects from Internet after waiting for news and mail to finish'
    say
    exit

WriteLog: procedure expose LogPath                                  /* Write line in logfile */
    parse arg msg
    LogEntry = left(date('W'),3) date() time() msg
    say LogEntry
    if ~open(log,LogPath,'A') then do
        if ~open(log,LogPath,'W') then return
        end
    call writeln(log,LogEntry)
    call close(log)
    return

WaitToEnd: procedure                                                /* Wait for the specified process to finish */
    parse arg Processes

    procs = words(Processes)
    do i = 1 to procs
        parse var Processes Process.i Processes
        end

    do until Busy = FALSE
        Busy = FALSE
        do i = 1 to procs
            'Status >NIL: com='Process.i
            if RC = 0 then do
                Busy = TRUE
                Wait 5
                end
            end
        end
    return

ConnectionFailed:
    call WriteLog('Connection attempt failed')                      /* Report connection failure */
    Ouch('Your internet connection has failed')
    return

Ouch:
    parse arg ErrMsg
    say(ErrMsg)
    exit
    return

