#!/usr/bin/env python3
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

import os
import os.path
import sys
import shutil
from pathlib import Path
from zipfile import ZipFile, ZIP_DEFLATED

scriptdir = os.path.dirname(os.path.abspath(sys.argv[0]))

module_dirs = [
    ("python_path", "aihordeclient"),
    ("python_path", "sseclient"),
]

for module_dir in module_dirs:
    os.makedirs(Path(scriptdir, *module_dir), exist_ok=True)

files = [
    (["..", "LICENSE"], ["description", "license.txt"]),
    (["..", "src", "StableHordeForLibreOffice.py"], [""]),
    (["..", "docs", "README.md"], ["README"]),
    (
        ["..", "modules", "sseclient-py", "LICENSE"],
        ["python_path", "sseclient", "LICENSE"],
    ),
    (
        ["..", "modules", "sseclient-py", "README.rst"],
        ["python_path", "sseclient", "README.rst"],
    ),
]

for pair in files:
    shutil.copy(Path(scriptdir, *pair[0]), Path(scriptdir, *pair[1]))

my_dirs = [
    (
        ["..", "modules", "aihordeclient", "src", "aihordeclient"],
        ["python_path", "aihordeclient"],
    ),
    (["..", "modules", "sseclient-py", "sseclient"], ["python_path", "sseclient"]),
]

for pair in my_dirs:
    shutil.copytree(
        Path(scriptdir, *pair[0]), Path(scriptdir, *pair[1]), dirs_exist_ok=True
    )


extensionname = (
    open(os.path.join(scriptdir, "extensionname.txt")).readlines()[0].rstrip("\n")
)
with ZipFile(extensionname, "w", compression=ZIP_DEFLATED) as zipfile:
    os.chdir(scriptdir)
    for root, dirs, files in os.walk("."):
        for name in files:
            if not name == extensionname:
                zipfile.write(os.path.join(root, name))
