.key ConnectionType/K,Action/K
.bra {
.ket }

;echo {TCPRecv}
;echo {UDPRecv}
;echo {UDPSend}
;echo {TCPSend}

;endcli

if {Action} EQ "Disconnect"
    echo "Disconnecting Network"
    Set wirelessManagerPID `status COM=C:wirelessmanager`
    if VAL $wirelessManagerPID GT 0
        break $WirelessManagerPID
    endif
    
    failat 21
    killdev DOSDEV=SMB0 >NIL:
    c:Netshutdown
    failat 10
    ;SETENV ConnectionStatus Disconnected
    Skip End
endif



If {Action} EQ "Connect"
    rx sys:Pistorm/ReadRoadshowParameters.rexx {ConnectionType} 
    if {ConnectionType} EQ "wifipi"
        echo "Connecting to Wireless (wifipi.device)"
    endif
    if {ConnectionType} EQ "genet"
    echo "Connecting to Ethernet (Genet.device)"
        if not $RpiType EQ "RPi4"
            echo "Genet.device only works on Pistorm with Raspberry Pi4 or CM4! Aborting!"
			Skip End
        endif
    endif
    c:areweonline   
;    shownetstatus >t:NetStatus.txt
;    search t:NetStatus.txt "Not configured" >NIL:
    if Warn
        Skip ConnectToNetwork
    else
;    search t:NetStatus.txt "on interface 'wifipi'" >NIL:
;        if not warn
;            if {ConnectionType} EQ "wifipi"
                Echo "You are already connected!"
                Skip End
 ;           else
 ;               c:netshutdown QUIET
 ;               Set wirelessManagerPID `status COM=C:wirelessmanager`
 ;               if VAL $wirelessManagerPID GT 0
 ;                   break $WirelessManagerPID
 ;               endif
 ;               Skip ConnectToNetwork
 ;           endif
 ;       endif
 ;   search t:NetStatus.txt "on interface 'genet'" >NIL:
 ;       if not warn
 ;           if {ConnectionType} EQ "genet"
 ;               Skip End
 ;           else 
 ;               c:netshutdown QUIET
 ;               Skip ConnectToNetwork    
 ;           endif
 ;       endif        
    endif
    
Else
    Echo "Invalid Action!"
    skip End
endif

Lab ConnectToNetwork

setenv NetworkErrorCount=0

if {ConnectionType} EQ "wifipi"
    Set wirelessManagerPID `status COM=C:wirelessmanager`
    if VAL $wirelessManagerPID GT 0
        break $WirelessManagerPID
    endif
    echo "Running WirelessManager"
    setenv NetworkInProgress 1
    run >T:Progressbar.txt S:ProgressBar
    run >NIL: c:wirelessmanager DEVICE="wifipi.device" UNIT=0 CONFIG="ENVARC:Sys/Wireless.prefs" VERBOSE >t:wirelessmanager.txt
    c:waituntilconnected device="DEVS:Networks/wifipi.device" UNIT=0 DELAY=100
    If not warn
        unsetenv NetworkInProgress
        wait 1
        echo ""
        echo ""
        Echo "Connected to WirelessManager. Now running Roadshow."
        roadshowcontrol tcp.recvspace = $TCPReceive >NIL:
        roadshowcontrol udp.recvspace = $UDPReceive >NIL:
        roadshowcontrol tcp.sendspace = $TCPSend >NIL:
        roadshowcontrol udp.sendspace = $UDPSend >NIL:
        AddNetInterface wifipi TIMEOUT=50 >T:AddInterface.txt
        search t:addinterface.txt "Could not add" >NIL:
        if not warn
            set NetInterfaceErrorWifi=1
            eval $NetworkErrorCount+1 to ENV:NetworkErrorCount
            skip End
        endif
    
        echo "Successfully connected to Network (Wifi)!" 
        echo ""
        shownetstatus
    else
        unsetenv NetworkInProgress
        Set WirelessManagerError=1
        eval $NetworkErrorCount+1 to ENV:NetworkErrorCount   
    endif
        Skip SetNetworkTime
endif

if {ConnectionType} EQ "genet"
    roadshowcontrol tcp.recvspace = $TCPReceive >NIL:
    roadshowcontrol udp.recvspace = $UDPReceive >NIL:
    roadshowcontrol tcp.sendspace = $TCPSend >NIL:
    roadshowcontrol udp.sendspace = $UDPSend >NIL:
    setenv NetworkInProgress 1
    run >T:Progressbar.txt S:ProgressBar
    AddNetInterface genet >T:AddInterface.txt
    search t:addinterface.txt "Could not add" >NIL:
    if not warn
        set NetInterfaceErrorWired=1
        eval $NetworkErrorCount+1 to ENV:NetworkErrorCount
        skip End
    endif
    unsetenv NetworkInProgress
    echo ""
    echo "Successfully connected to Network (Ethernet)!" 
    echo ""
    shownetstatus
;    type T:AddInterface.txt
    Skip SetNetworkTime
    
endif

Lab SetNetworkTime

echo ""
echo "Setting/Updating system clock"
sntp pool.ntp.org >T:sntp.txt
search T:sntp.txt "Unknown host" >NIL:
if not warn
    set SNTPError=1
    eval $NetworkErrorCount+1 to ENV:NetworkErrorCount
    Skip End
endif
search T:sntp.txt "Resource temporarily unavailable" >NIL:
if not warn
    set SNTPError=1
    eval $NetworkErrorCount+1 to ENV:NetworkErrorCount
    Skip End
endif
if exists SYS:Prefs/ENV-ARCHIVE/TZONEOVERRIDE
    c:setDST ZONE=$TZONEOVERRIDEs
else
    c:SetDST NOASK NOREQ QUIET >NIL:
endif
Skip End

Lab End

if $NetInterfaceErrorWifi EQ 1
    echo "Could not set up wifipi.device!"
    Set wirelessManagerPID `status COM=C:wirelessmanager`
    if VAL $wirelessManagerPID GT 0
        break $WirelessManagerPID
    endif
    ;break name="wirelessmanager"
    netshutdown QUIET
endif

if $NetInterfaceErrorWired EQ 1
    echo "Could not set up genet.device!"
    netshutdown QUIET
Endif

if $WirelessManagerError EQ 1
    Set wirelessManagerPID `status COM=C:wirelessmanager`
    if VAL $wirelessManagerPID GT 0
         break $WirelessManagerPID
    endif
    ;break name wirelessmanager >NIL:
    echo "Unsuccessful at connecting to Wireless network!"
    netshutdown QUIET
endif

if $SNTPError EQ 1
    echo "Unable to synchronise time with time server!"
endif

if VAL $NetworkErrorCount GT 0
    delete RAM:NetworkConnectionLogs.txt QUIET FORCE >NIL:
    echo "One or more errors encountered. Log will be written to RAM:NetworkConnectionLogs.txt"
    echo "Log of Network Connection" >RAM:NetworkConnectionLogs.txt
    echo "" >>RAM:NetworkConnectionLogs.txt
    echo "Output of Wireless Manager:" >>RAM:NetworkConnectionLogs.txt
    echo "" >>RAM:NetworkConnectionLogs.txt
    type t:wirelessmanager.txt >>RAM:NetworkConnectionLogs.txt
    echo "Output of AddInterface Command:" >>RAM:NetworkConnectionLogs.txt
    echo "" >>RAM:NetworkConnectionLogs.txt
    type t:AddInterface.txt >>RAM:NetworkConnectionLogs.txt
    echo "" >>RAM:NetworkConnectionLogs.txt
    echo "Output of SNTP Command:" >>RAM:NetworkConnectionLogs.txt
    echo "" >>RAM:NetworkConnectionLogs.txt
    type t:sntp.txt >>RAM:NetworkConnectionLogs.txt
    
endif

;Clean up

unsetenv NetworkInProgress
unsetenv NetworkErrorCount
unsetenv TCPReceive
unsetenv UDPReceive
unsetenv TCPSend 
unsetenv UDPSend
delete t:ProgressBar.txt >NIL:
delete t:AddInterface.txt QUIET FORCE >NIL:
delete t:RunningProcesses.txt QUIET FORCE >NIL:
delete t:Netstatus.txt QUIET FORCE >NIL:
delete t:sntp.txt QUIET FORCE >NIL:

echo "Window will close in 2 seconds"
wait 2