#
# This file is part of pretix (Community Edition).
#
# Copyright (C) 2014-2020 Raphael Michel and contributors
# Copyright (C) 2020-2021 rami.io GmbH and contributors
#
# This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General
# Public License as published by the Free Software Foundation in version 3 of the License.
#
# ADDITIONAL TERMS APPLY: Pursuant to Section 7 of the GNU Affero General Public License, additional terms are
# applicable granting you additional permissions and placing additional restrictions on your usage of this software.
# Please refer to the pretix LICENSE file to obtain the full terms applicable to this work. If you did not receive
# this file, see <https://pretix.eu/about/en/license>.
#
# This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License along with this program.  If not, see
# <https://www.gnu.org/licenses/>.
#

import os
import shutil
import subprocess

from setuptools.command.build import build
from setuptools.command.build_ext import build_ext

here = os.path.abspath(os.path.dirname(__file__))
npm_installed = False


def npm_install():
    global npm_installed

    if not npm_installed:
        # keep this in sync with Makefile!
        node_prefix = os.path.join(here, 'static.dist', 'node_prefix')
        os.makedirs(node_prefix, exist_ok=True)
        shutil.copytree(os.path.join(here, 'static', 'npm_dir'), node_prefix, dirs_exist_ok=True)
        subprocess.check_call('npm install', shell=True, cwd=node_prefix)
        npm_installed = True


class CustomBuild(build):
    def run(self):
        if "src" not in os.listdir(".") or "pretix" not in os.listdir("src"):
            # Only run this command on the pretix module, not on other modules even if it's registered globally
            # in some cases
            return build.run(self)
        if "PRETIX_DOCKER_BUILD" in os.environ:
            return  # this is a hack to allow calling this file early in our docker build to make use of caching
        os.environ.setdefault("DJANGO_SETTINGS_MODULE", "pretix._build_settings")
        os.environ.setdefault("PRETIX_IGNORE_CONFLICTS", "True")
        import django
        django.setup()
        from django.conf import settings
        from django.core import management

        settings.COMPRESS_ENABLED = True
        settings.COMPRESS_OFFLINE = True

        npm_install()
        management.call_command('compilemessages', verbosity=1)
        management.call_command('compilejsi18n', verbosity=1)
        management.call_command('collectstatic', verbosity=1, interactive=False)
        management.call_command('compress', verbosity=1)

        build.run(self)


class CustomBuildExt(build_ext):
    def run(self):
        if "src" not in os.listdir(".") or "pretix" not in os.listdir("src"):
            # Only run this command on the pretix module, not on other modules even if it's registered globally
            # in some cases
            return build_ext.run(self)
        if "PRETIX_DOCKER_BUILD" in os.environ:
            return  # this is a hack to allow calling this file early in our docker build to make use of caching
        npm_install()
        build_ext.run(self)
