From ac4ef81f70b70fe100ed429f9a7387824aba7cec Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Karel=20Ko=C4=8D=C3=AD?= <karel.koci@nic.cz>
Date: Wed, 20 Mar 2019 17:41:17 +0100
Subject: [PATCH] lists: update lists to use relative URI

This implements support for new updater with relative URI and no model
variable.
---
 lists/base-min.lua.m4      | 40 ++++++++++++------------------
 lists/base-netboot.lua.m4  |  4 +--
 lists/base.lua.m4          | 10 ++++----
 lists/luci-controls.lua.m4 |  2 +-
 lists/nas.lua.m4           |  4 +--
 lists/repository.lua.m4    | 50 ++++++++++++++++++--------------------
 lists/repository.m4        | 12 ---------
 lists/utils.lua            | 19 +++++++++++++++
 lists/utils.m4             | 10 ++++++++
 9 files changed, 78 insertions(+), 73 deletions(-)
 delete mode 100644 lists/repository.m4
 create mode 100644 lists/utils.lua

diff --git a/lists/base-min.lua.m4 b/lists/base-min.lua.m4
index f44fbdde2..801001d67 100644
--- a/lists/base-min.lua.m4
+++ b/lists/base-min.lua.m4
@@ -1,6 +1,5 @@
 include(utils.m4)dnl Include utility macros
-include(repository.m4)dnl Include Repository command
-----------------------------------------------------------------------------------
+list_script('repository.lua')
 
 -- Updater itself
 Install('updater-ng', 'updater-supervisor', { critical = true })
@@ -13,37 +12,29 @@ Install("base-files", "busybox", "dns-resolver", { critical = true })
 Package("kernel", { reboot = "delayed" })
 Package("kmod-mac80211", { reboot = "delayed" })
 forInstallCritical(kmod,file2args(kmod.list))
-if model:match("[Mm][Oo][Xx]") then
+if board == "mox" then
 	forInstallCritical(kmod,file2args(kmod-mox.list))
-elseif model:match("[Oo]mnia") then
+	Install("mox-support", { critical = true })
+	Install("zram-swap", { priority = 40 })
+elseif board == "omnia" then
 	forInstallCritical(kmod,file2args(kmod-omnia.list))
-elseif model:match("^[Tt]urris$") then
+	Install("omnia-support", { critical = true })
+elseif board == "turris1x" then
 	forInstallCritical(kmod,file2args(kmod-turris.list))
-end
-Install("fstools", { critical = true })
-if model:match("^[Tt]urris$") then
 	Install("turris1x-support", { critical = true })
 end
-if model:match("[Oo]mnia") then
-	Install("omnia-support", { critical = true })
-elseif model:match("[Mm][Oo][Xx]") then
-	Install("mox-support", { critical = true })
-	Install("zram-swap", { priority = 40 })
-end
+Install("fstools", { critical = true })
 
 -- OpenWRT minimum
 Install("procd", "ubus", "uci", "netifd", "firewall", { critical = true})
 Install("ebtables", "odhcpd", "odhcp6c", "rpcd", { priority = 40 })
 Install("opkg", "libustream-openssl", { priority = 40 })
-if model:match("^[Tt]urris$") then
-	Install("swconfig", { critical = true })
-end
 
 -- Turris minimum
 Install("vixie-cron", "syslog-ng", { priority = 40 })
 Install("logrotate", { priority = 40 })
 Install("dnsmasq-full", { priority = 40 })
-if model:match("^[Tt]urris$") then
+if board == "turris1x" then
 	Install("unbound", "unbound-anchor", { priority = 40 })
 else
 	Install("knot-resolver", { priority = 40 })
@@ -84,16 +75,17 @@ Install("turris-utils", "user-notify", "watchdog_adjust", { priority = 40 })
 if for_l10n then
 	for_l10n("user-notify-l10n-")
 end
-if model:match("[Mm][Oo][Xx]") then
+local use_atsha204 = false
+if board == "mox" then
 	Install("mox-otp", { priority = 40 })
-elseif model:match("[Oo]mnia") then
+elseif board == "omnia" then
 	Install("rainbow-omnia", { priority = 40 })
-	Install("libatsha204", { priority = 40 })
-elseif model:match("^[Tt]urris$") then
+	use_atsha204 = true
+elseif board == "turris1x" then
 	Install("rainbow", { priority = 40 })
 	Install("libatsha204", "update_mac", { priority = 40 })
 end
-if not model:match("^[Tt]urris$") then
+if board ~= "turris1x" then
 	Install("schnapps", { priority = 40 })
 end
 
@@ -101,7 +93,7 @@ end
 -- Wifi
 Install("hostapd-common", "wireless-tools", "wpad", "iw", "iwinfo", { priority = 40 })
 Install("ath10k-firmware-qca988x-ct", { priority = 40 })
-if model:match("[Mm][Oo][Xx]") then
+if board == "mox" then
 	Install("mwifiex-sdio-firmware", { priority = 40 })
 end
 
diff --git a/lists/base-netboot.lua.m4 b/lists/base-netboot.lua.m4
index eacf2b3d3..2e36b444b 100644
--- a/lists/base-netboot.lua.m4
+++ b/lists/base-netboot.lua.m4
@@ -1,6 +1,6 @@
 include(utils.m4)dnl Include utility macros
-include(repository.m4)dnl Include Repository command
-Script(repo_base_uri .. "/lists/base-min.lua")
+list_script('repository.lua')
+list_script('base-min.lua')
 
 _FEATURE_GUARD_
 
diff --git a/lists/base.lua.m4 b/lists/base.lua.m4
index 8a50d538d..409b861bc 100644
--- a/lists/base.lua.m4
+++ b/lists/base.lua.m4
@@ -1,12 +1,12 @@
 include(utils.m4)dnl Include utility macros
-include(repository.m4)dnl Include Repository command
-Script(repo_base_uri .. "/lists/base-min.lua")
+list_script('repository.lua')
+list_script('base-min.lua')
 
 _FEATURE_GUARD_
 
-Script(repo_base_uri .. "/lists/luci.lua")
-Script(repo_base_uri .. "/lists/foris.lua")
-Script(repo_base_uri .. "/lists/terminal-apps.lua")
+list_script('luci.lua')
+list_script('foris.lua')
+list_script('terminal-apps.lua')
 
 -- IPv6
 Install("ds-lite", "6in4", "6rd", "6to4", { priority = 40 })
diff --git a/lists/luci-controls.lua.m4 b/lists/luci-controls.lua.m4
index be9828b00..d0ee3a177 100644
--- a/lists/luci-controls.lua.m4
+++ b/lists/luci-controls.lua.m4
@@ -4,7 +4,7 @@ _FEATURE_GUARD_
 forInstall(luci-app,ahcp,firewall,minidlna,mjpg-streamer,statistics,tinyproxy,transmission,upnp)
 forInstall(luci-proto,openconnect,relay,vpnc)
 Install("luci-theme-bootstrap", { priority = 40 })
-if model:match('[Oo]mnia') or model:match('^[Tt]urris$') then
+if board == "omnia" or board == "turris1x" then
 	Install('luci-app-rainbow', { priority = 40 })
 end
 
diff --git a/lists/nas.lua.m4 b/lists/nas.lua.m4
index ee4318abc..161b0ee62 100644
--- a/lists/nas.lua.m4
+++ b/lists/nas.lua.m4
@@ -11,9 +11,9 @@ forInstall(kmod-nls,cp1250,cp1251,cp437,cp775,cp850,cp852,cp862,cp864,cp866,cp93
 forInstall(kmod-md,linear,multipath,raid0,raid1,raid10,raid456)
 -- Additional kernel drivers
 Install("kmod-usb-storage-extras", "kmod-usb2", "kmod-usb-storage-uas", { priority = 40 })
-if model:match("[Oo]mnia") then
+if board == "omnia" then
 	Install("blkdiscard", "fstrim", "asm1062-fix", { priority = 40 })
-elseif model:match("^[Tt]urris$") then
+elseif board == "turris1x" then
 	Install("kmod-mmc-fsl-p2020", "kmod-fs-reiserfs", "reiserfsprogs", { priority = 40 })
 end
 
diff --git a/lists/repository.lua.m4 b/lists/repository.lua.m4
index e34d0bd97..39006703e 100644
--- a/lists/repository.lua.m4
+++ b/lists/repository.lua.m4
@@ -1,36 +1,32 @@
 include(utils.m4)dnl Include utility macros
-divert(-1)
 
-# This is definition of subrepositories
-pushdef(`SUBDIRS',`subdirs = {"base", "core" esyscmd(`awk "/^src-git/{printf \", \\\"%s\\\"\", \$'`2}" '_FEEDS_)}')
+if not turris_repo_included then -- single include guard
 
-divert(0)dnl
-local board
-if model:match("[Mm][Oo][Xx]") then
-	board = "mox"
-elseif model:match("[Oo]mnia") then
-	board = "omnia"
-elseif model:match("^[Tt]urris$") then
-	board = "turris"
+local subdirs = { "base", "core" esyscmd(`awk "/^src-git/{printf \", \\\"%s\\\"\", \$'`2}" '_FEEDS_)}
+
+if features["relative_uri"] then
+	local rroot = ".."
 else
-	DIE("Unsupported Turris model: " .. tostring(model))
+	local rroot = repo_base_uri or "https://repo.turris.cz/hbs"
 end
 
--- Standard Turris OS package repository
-Repository("turris", repo_base_uri .. "/packages/" .. board, {
-	SUBDIRS
-})
--- Minimal faster rolling package repository
-if minimal_builds then
-	Repository("turris-minimal", repo_base_uri .. "/packages-minimal/" .. board, {
-		SUBDIRS,
-		ignore = { "missing" }
-	})
+local pkg_board = board
+if pkg_board == "turris1x" then
+	pkg_board = "turris"
 end
-dnl
-divert(-1)
 
-# Now just clean up after our self
-popdef(`SUBDIRS')
+for _, subdir in ipairs(subdirs) do
+	-- Standard Turris OS package repository
+	Repository("turris-" .. subdir, rroot .. "/packages/" .. pkg_board .. "/" .. subdir)
+
+	-- Minimal faster rolling package repository
+	if minimal_builds then
+		Repository("turris-minimal-" .. subdir,
+			rroot .. "/packages-minimal/" .. pkg_board .. "/" .. subdir,
+			{ optional = true })
+	end
+end
 
-divert(0)dnl
+turris_repo_included = true
+Export('turris_repo_included')
+end
diff --git a/lists/repository.m4 b/lists/repository.m4
deleted file mode 100644
index 973ef65d3..000000000
--- a/lists/repository.m4
+++ /dev/null
@@ -1,12 +0,0 @@
-dnl This is common repository include
-dnl It guards it against multiple inclusion in subscripts.
-if not repo_base_uri then
-	-- For backward compatibility we set base uri to HBS to update to updater version with repo_base_uri
-	repo_base_uri = "https://repo.turris.cz/hbs"
-	Export('repo_base_uri')
-end
-if not turris_repo_included then
-	Script(repo_base_uri .. "/lists/repository.lua")
-	turris_repo_included = true
-	Export('turris_repo_included')
-end
diff --git a/lists/utils.lua b/lists/utils.lua
new file mode 100644
index 000000000..5cf6d0cd7
--- /dev/null
+++ b/lists/utils.lua
@@ -0,0 +1,19 @@
+if not turris_utils_included then -- Multiple inclusions guard
+
+if not board then
+	model = model or os_release["LEDE_DEVICE_PRODUCT"]
+	if model:match("[Mm]ox") then
+		board = "mox"
+	elseif model:match("[Oo]mnia") then
+		board = "omnia"
+	elseif model:match("^[Tt]urris$") then
+		board = "turris1x"
+	else
+		DIE("Unsupported Turris model: " .. tostring(model))
+	end
+	Export('board')
+end
+
+turris_utils_included = true
+Export('turris_utils_included')
+end
diff --git a/lists/utils.m4 b/lists/utils.m4
index f7d557c44..bf7e6bbdf 100644
--- a/lists/utils.m4
+++ b/lists/utils.m4
@@ -45,3 +45,13 @@ if not version_match or not self_version or version_match(self_version, "<60.0.1
 	DIE("Minimal required version of Updater-ng for Turris repository is 60.0.1!")
 end
 
+-- Script simplifying lists inclusion when older version of updater is used
+function list_script(list)
+	if features["relative_uri"] then
+		Script(list)
+	else
+		Script((repo_base_uri or "https://repo.turris.cz/hbs") .. "/lists/" .. list)
+	end
+end
+list_script("utils.lua")
+----------------------------------------------------------------------------------
-- 
GitLab