#!/usr/bin/python3

## early init
import os
import sys
OPENGNSYS = os.environ.get ('OPENGNSYS', '/opt/opengnsys')
libs = [f'{OPENGNSYS}/lib/python3', '/usr/share/opengnsys-modules/python3/dist-packages']
os.environ['PYTHONPATH'] = ':'.join (libs)
for l in libs: sys.path.insert (0, l)
## end

import subprocess

import ogGlobals
from InitLib import loadenviron, write_profile, clean_esp, fileslinks, loadmodules, metadevs, mountrepo, poweroff, filebeat, stunnel, dbus, otherservices, runhttplog

IPV4ADDR = os.environ.get ('IPV4ADDR', '')
OG_IP    = os.environ.get ('OG_IP', '')

loadenviron()
write_profile()

# Funciones de inicio.
clean_esp()
fileslinks()
loadmodules()
metadevs()
mountrepo()
poweroff()
filebeat()
stunnel()
dbus()
otherservices()
runhttplog()

if IPV4ADDR and os.path.exists (f'{ogGlobals.OGETC}/init/{IPV4ADDR}.sh'):
    if OG_IP: subprocess.run ([f'{ogGlobals.OGETC}/init/{OG_IP}.sh'])

elif os.path.exists (f'{ogGlobals.OGETC}/init.py'):
    subprocess.run ([f'{ogGlobals.OGETC}/init.py'])

else:
    print ('No se ha encontrado script de inicio')
    subprocess.run (['halt'])
