# Preamble
import ctypes
import os
import sys
sys.setdlopenflags(sys.getdlopenflags() | ctypes.RTLD_GLOBAL)

# Plugins
#--------------------------------------------------------------
import mod
from mod import *

#--------------------------------------------------------------



mod.includedFiles = set()

def include(fName, checkDup=True, putDup=True, skipDup=True):
	t = fName.rpartition("/")
	if(fName != prefixFilename(fName)):
		if not _modWrapperScriptQuiet:
			print("Executing code from '", fName, "' ('", prefixFilename(fName), "')", sep="")
	else:
		if not _modWrapperScriptQuiet:
			print("Executing code from '", fName, "'", sep="")
	fPrefixed = prefixFilename(fName)
	skip = False
	ino = os.stat(fPrefixed).st_ino
	if checkDup:
		if ino in mod.includedFiles:
			if skipDup:
				skip = True
			else:
				print("Include error (checkDup=", checkDup, ", putDup=", putDup, ", skipDup=", skipDup, "): file '", fPrefixed, "' already included once.", sep="")
				sys.exit(1)
	if putDup:
		mod.includedFiles.add(ino)	
	if t[1] != "":
		pushFilePrefix(t[0] + t[1])
	if skip:
		if not _modWrapperScriptQuiet:
			print("Skipping inclusion (already included)")
	else:
		with open(fPrefixed, 'rb') as f:
			code = compile(f.read(), fName, 'exec')
		exec(code, globals())
	if t[1] != "":
		popFilePrefix()
	if not _modWrapperScriptQuiet:
		if(fName != prefixFilename(fName)):
			print("End of code from '", fName, "' ('", prefixFilename(fName), "')", sep="")
		else:
			print("End of code from '", fName, "'", sep="")
mod.include = include

if "LD_PRELOAD" in os.environ and "valgrind" in os.environ["LD_PRELOAD"]:
	import atexit
	def _valgrindLeakHaxAtExit():
		toRemove = set(globals()) - _initialGlobals
		toRemove.remove("_initialGlobals")
		print("atexit hax to remove global variables for Valgrind leak check:")
		for n in toRemove:
			print("  ", n)
			del globals()[n]
	atexit.register(_valgrindLeakHaxAtExit)
	_initialGlobals = set(globals())
# From command line arguments
_modWrapperScriptQuiet = False
# PYTHONPATH=::/Users/addie/everything/bielefeld/2025/bielefeld/mod-AC/.menv3/lib:/Users/addie/everything/bielefeld/2026/overlay-graphs/addie-uniprot-hits
