###############################################################################
# eth0
###############################################################################

        location /get_eth0_status {
            add_header 'Access-Control-Allow-Origin' '*';
            add_header 'Access-Control-Allow-Credentials' 'true';
            add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
            add_header 'Access-Control-Allow-Headers' 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
            
            default_type plain/text;

            content_by_lua '
                local f = io.popen([[ip -4 addr list eth0]])
                local content = f:read("*all") 
                f:close() 

                local gf = io.popen([[ip route | grep -Po "(?<=via )(\d{1,3}.){4}"]])
                local gateway = gf:read("*all")
                gf:close()

                ngx.say(content.."\\ngateway:"..gateway) 
            ';
        }

        location /set_eth0 {
            add_header 'Access-Control-Allow-Origin' '*';
            add_header 'Access-Control-Allow-Credentials' 'true';
            add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
            add_header 'Access-Control-Allow-Headers' 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
            
            default_type plain/text;

            content_by_lua '
                local args = ngx.req.get_uri_args()

                local network =          "[Match]\\n"
                      network = network.."Name=eth0\\n"
                      network = network.."[Network]\\n"
                      network = network.."LinkLocalAddressing=yes\\n"
                if args.dhcp then
                      network = network.."DHCP="..args.dhcp.."\\n"
                end
                if args.address then
                      network = network.."Address="..args.address.."\\n"
                end
                if args.gateway then
                      network = network.."Gateway="..args.gateway.."\\n"
                end
                if args.dns then
                      network = network.."DNS="..args.dns.."\\n"
                end
                      network = network.."IPForward=yes\\n"
                      network = network.."IPMasquerade=yes\\n"
                      network = network.."[Address]\\n"
                if args.broadcast then
                      network = network.."Broadcast="..args.broadcast.."\\n"
                end

                os.execute("rw")
                local f = io.open("/etc/systemd/network/wired.network", "w")
                f:write(network)
                f:close()

                os.execute("systemctl restart systemd-networkd.service")
                os.execute("ro")
                os.execute("kill -HUP $(pgrep nginx | head -1)")
            ';
        }
###############################################################################
# wlan0 client
###############################################################################

        location /get_wlan0_status {
            add_header 'Access-Control-Allow-Origin' '*';
            add_header 'Access-Control-Allow-Credentials' 'true';
            add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
            add_header 'Access-Control-Allow-Headers' 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
            
            default_type plain/text;

            content_by_lua '
                local f = io.popen("ip addr list wlan0") 
                local content = f:read("*all") 
                f:close() 
                ngx.say(content) 
            ';
        }

        location /check_dongle {
            add_header 'Access-Control-Allow-Origin' '*';
            add_header 'Access-Control-Allow-Credentials' 'true';
            add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
            add_header 'Access-Control-Allow-Headers' 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';

            default_type plain/text;

            content_by_lua '
                local fh = io.popen("ip addr show wlan0")
                local line = fh:read("*all")
                if line == nil then
                    ngx.say("Fail")
                else
                    if line:find("link") ~= nil then
                        ngx.say("OK")
                    else
                        ngx.say("Fail")
                    end
                end
                fh:close()
            ';
        }

        location /get_wnet_list {
            add_header 'Access-Control-Allow-Origin' '*';
            add_header 'Access-Control-Allow-Credentials' 'true';
            add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
            add_header 'Access-Control-Allow-Headers' 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';

            default_type application/json;

            content_by_lua '
                local f = io.popen("iw dev wlan0 scan | gawk -f /opt/redpitaya/www/apps/network_manager/iw_scan.awk")
                local content = f:read("*all")
                f:close()
                ngx.say(content)
            ';
        }

        location /connect_wifi {
            add_header 'Access-Control-Allow-Origin' '*';
            add_header 'Access-Control-Allow-Credentials' 'true';
            add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
            add_header 'Access-Control-Allow-Headers' 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';

            default_type plain/text;

            content_by_lua '
                local args     = ngx.req.get_uri_args()
                local ssid    = args.ssid    or 0
                local password = args.password or 0
                local need_reboot = false

                local f=io.open("/opt/redpitaya/hostapd.conf","r")
                if f~=nil then
                    io.close(f)
                    need_reboot = true
                else
                    need_reboot = false
                end

                os.execute("rw")
                os.execute("systemctl stop wpa_supplicant@wlan0.service")
                os.execute("wpa_passphrase "..ssid.." "..password.." > /opt/redpitaya/wpa_supplicant.conf")
                os.execute("systemctl restart wpa_supplicant@wlan0.service")
                os.execute("ro")

                if need_reboot then
                	os.execute("kill -HUP $(pgrep nginx | head -1)")
                end
            ';
        }

        location /get_connected_wlan {
            add_header 'Access-Control-Allow-Origin' '*';
            add_header 'Access-Control-Allow-Credentials' 'true';
            add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
            add_header 'Access-Control-Allow-Headers' 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';

            default_type plain/text;

            content_by_lua '
                local f = io.popen("iw dev wlan0 link | grep SSID")
                local content = f:read("*all")
                f:close()
                ngx.say(content)
            ';
        }

        location /disconnect_wifi {
            add_header 'Access-Control-Allow-Origin' '*';
            add_header 'Access-Control-Allow-Credentials' 'true';
            add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
            add_header 'Access-Control-Allow-Headers' 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';

            default_type plain/text;

            content_by_lua '
                os.execute("rw")
                os.execute("systemctl stop wpa_supplicant@wlan0.service")
                os.execute("rm -f /opt/redpitaya/wpa_supplicant.conf")
		os.execute("iw dev wlan0 disconnect")
		os.execute("ip link set dev wlan0 down")
		os.execute("sleep 3")
		os.execute("ip link set dev wlan0 up")
                os.execute("ro")
                os.execute("kill -HUP $(pgrep nginx | head -1)")
            ';
        }

###############################################################################
# wlan0 access point
###############################################################################

        location /wifi_create_point {
            add_header 'Access-Control-Allow-Origin' '*';
            add_header 'Access-Control-Allow-Credentials' 'true';
            add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
            add_header 'Access-Control-Allow-Headers' 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';

            default_type text/plain;

            content_by_lua '
                local args = ngx.req.get_uri_args()
                local need_reboot = false

                os.execute("rw")
                local f=io.open("/opt/redpitaya/wpa_supplicant.conf","r")
                if f~=nil then
                    io.close(f)
                    need_reboot = true
                else
                    need_reboot = false
                end
                os.execute("rm -f /opt/redpitaya/wpa_supplicant.conf")
                os.execute("rm -f /opt/redpitaya/hostapd.conf")

                local hostapd =          "interface=wlan0\\n"
                      hostapd = hostapd.."ssid="..args.ssid.."\\n"
                      hostapd = hostapd.."driver=nl80211\\n"
                      hostapd = hostapd.."hw_mode=g\\n"
                      hostapd = hostapd.."channel=6\\n"
                      hostapd = hostapd.."macaddr_acl=0\\n"
                      hostapd = hostapd.."auth_algs=1\\n"
                      hostapd = hostapd.."ignore_broadcast_ssid=0\\n"
                      hostapd = hostapd.."wpa=2\\n"
                      hostapd = hostapd.."wpa_passphrase="..args.password.."\\n"
                      hostapd = hostapd.."wpa_key_mgmt=WPA-PSK\\n"
                      hostapd = hostapd.."wpa_pairwise=TKIP\\n"
                      hostapd = hostapd.."rsn_pairwise=CCMP\\n"

                local f = io.open("/opt/redpitaya/hostapd.conf", "w")
                f:write(hostapd)
                f:close()

		ngx.say("OK")
                os.execute("systemctl restart hostapd@wlan0.service")
                os.execute("ro")

                if need_reboot then
                	os.execute("kill -HUP $(pgrep nginx|head -1)")
                end
            ';
        }

        location /remove_ap {
            add_header 'Access-Control-Allow-Origin' '*';
            add_header 'Access-Control-Allow-Credentials' 'true';
            add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
            add_header 'Access-Control-Allow-Headers' 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';

            default_type plain/text;

            content_by_lua '
                os.execute("rw")
                os.execute("systemctl stop hostapd@wlan0.service")
                os.execute("rm -f /opt/redpitaya/hostapd.conf")
                os.execute("ro")
                os.execute("kill -HUP $(pgrep nginx|head -1)")
            ';
        }

        location /get_ap_status {
            add_header 'Access-Control-Allow-Origin' '*';
            add_header 'Access-Control-Allow-Credentials' 'true';
            add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
            add_header 'Access-Control-Allow-Headers' 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';

            default_type plain/text;

            content_by_lua '
                local f = io.popen("iw dev wlan0 info | grep type")
                local content = f:read("*all")
                f:close()
                ngx.say(content)
            ';
        }
