# -*- coding: utf-8 -*-
from setuphelpers import *


def update_package():
    doc_built_version = WAPT.wapt_status()["wapt-version-full"].split("-")[0]
    control.set_software_version(doc_built_version)
    control.save_control_to_wapt()

    conf_py_file = makepath("source", "conf.py")

    wget('http://srvtemplates.ad.tranquil.it/binary_cache/get-pip_26.1.2.py', target=rf'{basedir}/get-pip.py', sha256='a341e1a43e38001c551a1508a73ff23636a11970b61d901d9a1cad2a18f57055')

    data = ''
    with open(conf_py_file, "r", encoding="utf-8") as pyfile:
        for line in pyfile.readlines():

            if line.startswith("version"):
                data = data + "version = '%s'\n" % doc_built_version
            elif line.startswith("release"):
                data = data + "release = '%s'\n" % doc_built_version
            else:
                data = data + line

    with open(conf_py_file, "w", encoding="utf-8") as pyfile:
        pyfile.write(data)
        pyfile.close()

    run(f""" /opt/wapt/waptpython.sh {basedir}/get-pip.py""")

    mkdirs(f"{basedir}/lib")

    run(f""" /opt/wapt/waptpython.sh -m pip install -r requirements.txt --only-binary :all: --index-url=https://pip.ad.tranquil.it --trusted-host=pip.ad.tranquil.it  -t "{basedir}/lib" """)

    run(f""" /opt/wapt/waptpython.sh waptsphinx.py   -M html source  build """)

