#!/usr/bin/python3

import argparse
import shutil
import os
import glob
import sys
import re
import subprocess

from boottools import utils, apt

def _oghook_deactivate():
    #Desactivamos el hook del oginitrd.img para evitar problemas, al final de este escripts se activará
    os.rename ('/etc/initramfs-tools/hooks/oghooks', '/etc/initramfs-tools/oghooks')

def _oghook_activate():
    #Activamos el hook del oginitrd.img
    os.rename ('/etc/initramfs-tools/oghooks', '/etc/initramfs-tools/hooks/oghooks')

def boottoolsSoftwareInstall (osarch, osrelease):
    os.environ['LANGUAGE'] = 'C'
    os.environ['LC_ALL'] = 'C'
    os.environ['LANG'] = 'C'
    os.environ['DEBIAN_FRONTEND'] = 'noninteractive'

    stdout, _ = utils.run (['dpkg-divert', '--list'])
    if not re.findall (r'local diversion of /sbin/initctl to /sbin/initctl.distrib', stdout):
        utils.run (['dpkg-divert', '--local', '--rename', '--add', '/sbin/initctl'])
        os.symlink ('/bin/true', '/sbin/initctl')

    pkgs32 = []
    if 'i386' != osarch:
        utils.run (['dpkg', '--add-architecture', 'i386'])
        pkgs32 = 'lib32gcc-s1 lib32stdc++6 lib32z1 libc6-i386'.split (' ')

    _oghook_deactivate()

    print ('boottoolsSoftwareInstall: debconf-set-selections', file=sys.stderr)
    subprocess.run (['debconf-set-selections'], input=debconf_settings, text=True)
    utils.run (['dpkg-reconfigure', '--frontend', 'noninteractive', 'console-setup', 'locales'])   ## XXX: despues de esto, debconf-get-selections devuelve los valores antiguos, no se por que...

    pkgs = glob.glob ('/tmp/opengnsys/oglive_builder/ogagent_*.deb') + glob.glob ('/tmp/opengnsys/oglive_builder/OGBrowser*.deb')
    for section in config.options('Packages'):
        pkgs += re.split (r'[ \n]', config['Packages'].get(section).strip())

    apt.update()
    apt.upgrade()

    pkgs = [f'linux-image-{osrelease}', f'linux-headers-{osrelease}', f'linux-modules-{osrelease}', f'linux-modules-extra-{osrelease}', 'dkms', 'shim-signed', 'openssl', 'sshfs', 'kexec-tools'] + pkgs32 + pkgs
    print (f'boottoolsSoftwareInstall: installing packages: {str(pkgs)}', file=sys.stderr)
    apt.install (pkgs, opts={'DPkg::Options::': '--force-confdef'})         ## --force-confdef is required to avoid an interactive question regarding /etc/ssh/ssh_config

    # Instalar módulos que algunos paquetes puedan tener pendientes de compilar.
    print ('boottoolsSoftwareInstall: dkms', file=sys.stderr)
    stdout, _ = utils.run (['dkms', 'status'])
    for l in stdout.strip().split ('\n'):
        if not l: continue
        #print (f'l "{l}"')
        mod, vers, status = l.split (',')
        if 'added' in status:
            print (f'dkms installing {mod} {vers}')
            utils.run (['dkms', 'install', '-m', mod.strip(), '-v', vers.strip()])

    _oghook_activate()
    apt.clean()
    apt.autoremove()

def boottoolsSoftwareCompile():
    env_language = os.environ['LANGUAGE']
    env_lc_all   = os.environ['LC_ALL']
    env_lang     = os.environ['LANG']
    os.environ['LANGUAGE'] = os.environ['LC_ALL'] = os.environ['LANG'] = 'C'
    os.chdir ('/tmp')

    print ('boottoolsSoftwareCompile: ms-sys', file=sys.stderr)
    try: utils.run (['which', 'ms-sys'])
    except:
        utils.run (['wget', '--quiet', 'https://sourceforge.net/projects/ms-sys/files/latest/download', '-O', 'ms-sys.tar.gz'])
        utils.run (['tar', '-xpzf', 'ms-sys.tar.gz'])
        mssys_dir = subprocess.run (['tar tzf ms-sys.tar.gz |head -n 1'], shell=True, capture_output=True, text=True).stdout.strip()
        print (f'mssys_dir "{mssys_dir}"')
        os.chdir (mssys_dir)
        utils.run (['make', 'install'])
        os.chdir ('..')

    print ('boottoolsSoftwareCompile: spartlnx', file=sys.stderr)
    try: utils.run (['which', 'spartl64.run'])
    except:
        utils.run (['wget', '--quiet', 'http://damien.guibouret.free.fr/savepart.zip'])
        utils.run (['unzip', '-o', 'savepart.zip', '-d', '/sbin/', 'spartl64.run', 'spartlnx.run'])
        utils.run (['mkdir', '/usr/share/doc/spartlnx'])
        utils.run (['unzip', '-j', '-o', 'savepart.zip', '-d', '/usr/share/doc/spartlnx/', 'doc/en/*'])

    if not os.path.exists ('python-libfdisk'):
        print ('boottoolsSoftwareCompile: python-libfdisk', file=sys.stderr)
        apt.install (['python3-psutil', 'python3-dev', 'libfdisk-dev', 'python3-setuptools'])
        utils.run (['git', 'clone', 'https://ognproject.evlt.uma.es/gitea/48k.eu-mirror/python-libfdisk.git'])
        os.chdir ('python-libfdisk')
        utils.run (['python3', 'setup.py', 'install'])
        os.chdir ('..')

    os.environ['LANGUAGE'] = env_language
    os.environ['LC_ALL']   = env_lc_all
    os.environ['LANG']     = env_lang

def updateCaCertificates():
    print ('Updating CA trust Store', file=sys.stderr)
    utils.run (['update-ca-certificates'])

def boottoolsPythonModules():
    utils.run (['pip3', 'install', 'pyblkid', '--break-system-packages'])

def boottoolsRemovePackages():
    apt.remove (['python3-dev', 'python3-setuptools', 'python3-pip'])

def setup_resolvconf():
    if os.path.islink('/etc/resolc.conf'):
        os.unlink ('/etc/resolv.conf')
    f = open ('/etc/resolv.conf', 'w')
    f.write ('nameserver 8.8.8.8')
    f.close()

def boottoolsInitrdGenerate (osrelease):
    print ('boottoolsInitrdGenerate', file=sys.stderr)
    for f in glob.glob ('/usr/lib/initramfs-tools/bin/*'):
        os.unlink (f)
    shutil.copy ('/bin/busybox', '/usr/lib/initramfs-tools/bin')

    initrd_img = f'/tmp/initrd.img-{osrelease}'

    os.chdir ('/tmp')
    utils.run (['mkinitramfs', '-o', initrd_img, osrelease])
    shutil.copy (f'/boot/vmlinuz-{osrelease}', '/tmp/')

    ## turn cpio-with-prepended-stuff into a regular cpio, see #975
    utils.run (['unmkinitramfs', initrd_img, 'undone'])
    os.mkdir ('undone/merged')
    subprocess.run (['rsync -aH undone/early/* undone/main/* undone/merged/'], shell=True)
    shutil.rmtree ('undone/early')
    shutil.rmtree ('undone/main')
    os.chdir ('undone/merged/')
    subprocess.run ([f'find . |cpio -H newc -oa >{initrd_img}'], shell=True)
    os.chdir ('/tmp')
    shutil.rmtree ('undone')


if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument ('--osarch',    help='OS architecture',            action='store', required=True)
    parser.add_argument ('--osrelease', help='OS release',                 action='store', required=True)
    parser.add_argument ('--config',    help='Path to configuration file', action='store')
    args = parser.parse_args()

    config = utils.read_config (args.config or 'mkoglive.cfg')
    if config is None:
        sys.exit (1)
    debconf_settings = config['General'].get ('debconf_settings')

    updateCaCertificates()
    setup_resolvconf()
    boottoolsSoftwareInstall (args.osarch, args.osrelease)
    boottoolsSoftwareCompile()
    boottoolsPythonModules()
    boottoolsRemovePackages()
    setup_resolvconf()    ## do this again, since someone seems to be overwriting the file
    boottoolsInitrdGenerate (args.osrelease)
