#!/usr/bin/env python3

# SPDX-FileCopyrightText: 2025 Enrique M.G. <quique@necos.es>
#
# SPDX-License-Identifier: GPL-3.0-or-later

import os
import sys
import glob
import subprocess
import pathlib

TAGS_DIR = "/etc/lliurex-auto-upgrade/tags/"
RUN_TAGS_DIR = "/run/lliurex-auto-upgrade/tags/"

tags = []

def clear_tags():
	for path in (glob.glob(TAGS_DIR+"lliurex.version.*")):
		os.remove(path)

	for path in (glob.glob(RUN_TAGS_DIR+"lliurex.version.*")):
		os.remove(path)


if __name__=="__main__":

	if (not os.path.exists(RUN_TAGS_DIR)):
		p = pathlib.Path(RUN_TAGS_DIR)
		p.mkdir(parents = True)

	try:
		clear_tags()
		tmp = subprocess.check_output(["lliurex-version"])
		tmp = tmp.decode("utf-8").strip()
		for q in tmp.split(","):
			tags.append("lliurex.version." + q.strip())

	except Exception as e:
		pass


	for tag in tags:
		p = pathlib.Path(RUN_TAGS_DIR + tag)
		p.touch()

