#!/usr/bin/python

# fix the properties files from upstream.  Mail to Markus 2025-08-28:
# * Feld "hips_service_url" muss auf eure Adresse zeigen (sonst ist der
#   Mirror kein Mirror)
# * Feld "hips_status" muss auf "public mirror clonable") geändert werden

import re

from gavo import api

def main():
	rd = api.getRD("nsns/q")
	for p in (api.getConfig("inputsDir") / "nsns/data/hips").iterdir():
		prop_file =  p / "properties"
		if prop_file.exists():
			with prop_file.open() as f:
				props = f.read()

			mat = re.search(r"(?m)^hips_service_url\s*=\s*(.*)", props)
			hips_id = mat.group(1).split("/")[-1]
			if hips_id=="hips":
				# we have already processed this file
				continue
			svc = rd.getById(hips_id)
			new_url = svc.getURL("hips")
			props = props.replace(
				mat.group(0),
				f"hips_service_url = {new_url}")

			props = re.sub(
				r"(?m)hips_status\s*=.*",
				"hips_status = public mirror clonable",
				props)

			with prop_file.open("w") as f:
				f.write(props)


if __name__=="__main__":
	main()
