#!/usr/bin/lua

local sysconfig = require 'gluon.sysconfig'


if sysconfig.primary_mac then
  os.exit(0)
end


local util = require 'gluon.util'
local platform = require 'gluon.platform'


local try_files = {
  '/sys/class/net/eth0/address'
}

if not (
  util.contains({'x86', 'brcm2708'}, platform.get_target()) or
  platform.match('ar71xx', 'mikrotik')
) then
  table.insert(try_files, 1, '/sys/class/ieee80211/phy0/macaddress')
end

if platform.match('ar71xx', 'generic', {'tl-wdr3600', 'tl-wdr4300',
                                        'tl-wr902ac-v1'}) then
  table.insert(try_files, 1, '/sys/class/ieee80211/phy1/macaddress')
elseif platform.match('ar71xx', 'generic', {'a40', 'a60',
                                            'archer-c25-v1',
                                            'archer-c7-v4', 'archer-c7-v5',
                                            'carambola2',
                                            'koala',
                                            'mr600', 'mr600v2',
                                            'mr900', 'mr900v2',
                                            'mr1750', 'mr1750v2',
                                            'om2p', 'om2pv2', 'om2pv4',
                                            'om2p-hs', 'om2p-hsv2', 'om2p-hsv3',
                                            'om2p-hsv4',
                                            'om2p-lc',
                                            'om5p', 'om5p-an',
                                            'om5p-ac', 'om5p-acv2',
                                            'unifi-outdoor-plus',
                                            'unifiac-lite', 'unifiac-pro'}) then
  table.insert(try_files, 1, '/sys/class/net/eth0/address')
elseif platform.match('ar71xx', 'generic', {'archer-c5', 'archer-c58-v1',
                                            'archer-c59-v1', 'archer-c60-v1',
                                            'archer-c7'}) then
  table.insert(try_files, 1, '/sys/class/net/eth1/address')
elseif platform.match('ar71xx', 'nand', {'hiveap-121'}) then
  table.insert(try_files, 1, '/sys/class/net/eth0/address')
elseif platform.match('ipq40xx', nil, {'avm,fritzbox-4040',
                                       'openmesh,a42', 'openmesh,a62'}) then
  table.insert(try_files, 1, '/sys/class/net/eth0/address')
elseif platform.match('ipq806x', nil, {'netgear,r7800'}) then
  table.insert(try_files, 1, '/sys/class/net/eth1/address')
elseif platform.match('mpc85xx', 'p1020', {'aerohive,hiveap-330'}) then
  table.insert(try_files, 1, '/sys/class/net/eth0/address')
elseif platform.match('ramips', 'mt7620', {'miwifi-mini'}) then
  table.insert(try_files, 1, '/sys/class/net/eth0/address')
elseif platform.match('ramips', 'mt7621', {'dir-860l-b1'}) then
  table.insert(try_files, 1, '/sys/class/ieee80211/phy1/macaddress')
end


for _, file in ipairs(try_files) do
  local addr = util.readfile(file)

  if addr then
    sysconfig.primary_mac = util.trim(addr)
    break
  end
end
