#!/bin/bash

function isMeshable {
    interface=`iw dev | grep phy#$1 -A 1 | grep Interface | awk '{print $2}'`

    # Check to see if driver reports missing mesh point support
    if  [ -z "$(iw phy phy$1 info | grep 'mesh point')" ]; then
        result='' # Indicate interface is not meshable
    # XRADIO driver reports Mesh Point support incorrectly (there is no support for it)
    elif [ "$(basename $(readlink /sys/class/net/$interface/device/driver))" == 'xradio_wlan' ]; then	    
        result='' # Indicate interface is actually not meshable
    else
        result='1' # Indicate interface is  meshable
    fi

    echo $result
}


set -e

# Set wireless regulatory domain
sudo iw reg set CA

# Select physical device that supports 802.11s Mesh Point
if ! [ -z $(isMeshable 0) ]; then
    mesh_phy=phy0
    mesh_dev=$(iw dev | grep phy#0 -A 1 | grep Interface | awk '{print $2}')
elif ! [ -z $(isMeshable 1) ]; then
    mesh_phy=phy1
    mesh_dev=$(iw dev | grep phy#1 -A 1 | grep Interface | awk '{print $2}')
else
    exit 1
fi

# Shut down the mesh_dev interface
sudo ifconfig $mesh_dev down

# Convert mesh_dev to 802.11s Mesh Point interface
sudo iw $mesh_dev set type mp

# Bring up the mesh_dev interface
sudo ifconfig $mesh_dev up

# Optionally assign IPv4 address to the mesh_dev interface
# sudo ifconfig $mesh_dev 192.168.X.Y

# Join the mesh network with radio in HT40+ htmode to enable 802.11n rates
sudo iw dev $mesh_dev mesh join MESH_NAME freq 2412 HT40+

# Disable forwarding since we rely on cjdns to do routing and only uses Mesh Point as a point-to-point link
sudo iw dev $mesh_dev set mesh_param mesh_fwding=0

# Restart cjdns
sudo killall cjdroute
