#!/opt/cloudlinux/venv/bin/python3 -bb
#coding:utf-8

# Copyright © Cloud Linux GmbH & Cloud Linux Software, Inc 2010-2023 All Rights Reserved
#
# Licensed under CLOUD LINUX LICENSE AGREEMENT
# http://cloudlinux.com/docs/LICENSE.TXT

from __future__ import print_function
from __future__ import division
from __future__ import absolute_import
import os
import re
import sys
import json
import shlex
import shutil
import argparse
import subprocess
import configparser
import cldetectlib as detect
from clcommon.ui_config import UIConfig

SOURCE_PATH_BASE = "/usr/share/cloudlinux-awp-plugin/"
SOURCE_PATH_ADMIN = "/usr/share/cloudlinux-awp-plugin/awp-admin/"
SOURCE_PATH_USER = "/usr/share/cloudlinux-awp-plugin/awp-user/"
DYNAMICUI_SYNC_CONFIG_COMMAND = "/usr/share/l.v.e-manager/utils/dynamicui.py --sync-conf=all --silent"
PANEL_INTEGRATION_CONFIG = '/opt/cpvendor/etc/integration.ini'
CLOUDLINUX_TRANSLATIONS_DIR = '/usr/share/cloudlinux-translations/'

class Base:
    def __init__(self):
        pass

    def remove_file_or_dir(self, path):
        """
        Remove file or directory
        """
        try:
            if os.path.isfile(path):
                os.remove(path)
            elif os.path.isdir(path):
                shutil.rmtree(path)
        except Exception as e:
            print("An error occurred while copying: {}".format(e))

    def copy_file_or_dir(self, src, dst):
        """
        Copy file or directory to specified location
        """
        try:
            if os.path.isfile(src):
                self.remove_file_or_dir(dst)
                shutil.copy(src, dst)
            elif os.path.isdir(src):
                self.remove_file_or_dir(dst)
                shutil.copytree(src, dst)
        except Exception as e:
            print("An error occurred while copying: {}".format(e))

    def parse_command(self, command):
        """
        Parses a command string into a list of arguments.
        """
        if isinstance(command, str):
            if command.strip() == "":
                return []
            return shlex.split(command)
        elif isinstance(command, list):
            return command
        else:
            return []

    def exec_command(self, command, env=None):
        """
        This function will run passed command
        in command line and returns result
        :param command:
        :param env:
        :return:
        """
        result = []
        try:
            args = self.parse_command(command)
            if not args:
                raise ValueError(f"The provided command is not valid: {command}")
            p = subprocess.Popen(args, stdout=subprocess.PIPE, env=env, text=True)
            while 1:
                output = p.stdout.readline()
                if not output:
                    break
                if output.strip() != "":
                    result.append(output.strip())
        except Exception as e:
            print ("Call process error: " + str(e))
        return result

    def sync_ui_config(self):
        self.exec_command(DYNAMICUI_SYNC_CONFIG_COMMAND)

    def generate_translate_templates(self):
        """
        Prepare translate templates using english as a base.
        """
        source_path = f"{SOURCE_PATH_USER}i18n/en-en.json"
        template_path = f"{CLOUDLINUX_TRANSLATIONS_DIR}awp-user-ui.json"
        self.copy_file_or_dir(source_path, template_path)

class CpanelPluginInstaller(Base):
    def __init__(self):
        self.CPANEL_THEMES_BASE_DIR = "/usr/local/cpanel/base/frontend/"
        self.source_admin = SOURCE_PATH_ADMIN
        self.source_user = SOURCE_PATH_USER
        self.destination_admin = "/usr/local/cpanel/whostmgr/docroot/3rdparty/cloudlinux/assets/awp-admin"
        self.destination_user = "/usr/local/cpanel/whostmgr/docroot/3rdparty/cloudlinux/assets/awp-user"
        self.template_src = SOURCE_PATH_BASE + "user-plugins/cpanel/wpos.live.pl"
        self.template_dst = self.CPANEL_THEMES_BASE_DIR + "{}/lveversion/wpos.live.pl"
        self.plugin_installer = "/usr/local/cpanel/scripts/install_plugin"
        self.plugin_uninstaller = "/usr/local/cpanel/scripts/uninstall_plugin"
        self.awp_feature_file = "/usr/local/cpanel/whostmgr/addonfeatures/lvewpos"
        self.install_config = SOURCE_PATH_BASE + "user-plugins/cpanel/plugin/install.json"
        self.plugin_tar = SOURCE_PATH_BASE + "user-plugins/cpanel/cpanel-awp-plugin.tar.bz2"

    def install_plugin(self):
        self.copy_file_or_dir(self.source_admin, self.destination_admin)
        self.copy_file_or_dir(self.source_user, self.destination_user)
        for theme in self.get_theme_list():
            self.copy_file_or_dir(self.template_src, self.template_dst.format(theme))
            self.exec_command("{} {}  --theme {}".format(self.plugin_installer, self.plugin_tar, theme))
        self.cpanel_fix_feature_manager()

    def cpanel_fix_feature_manager(self):
        if os.path.exists(self.awp_feature_file) and os.path.exists(self.install_config):
            with open(self.install_config) as install_config:
                feature_config = json.load(install_config)
                feature = feature_config[0]  # We have only one item
                try:
                    feature_name_fixed = re.search("\$LANG{'(.*)'}", feature['name']).group(1)
                except AttributeError:
                    feature_name_fixed = ''
                feature_file = open(self.awp_feature_file, 'w')
                feature_file.write(feature['feature'] + ':' + feature_name_fixed)
                feature_file.close()

    def uninstall_plugin(self):
        self.remove_file_or_dir(self.destination_admin)
        self.remove_file_or_dir(self.destination_user)
        for theme in self.get_theme_list():
            self.remove_file_or_dir(self.template_dst.format(theme))
            self.exec_command("{} {}  --theme {}".format(self.plugin_uninstaller, self.plugin_tar, theme))

    def get_theme_list(self):
        if os.path.isdir(self.CPANEL_THEMES_BASE_DIR):
            return next(os.walk(self.CPANEL_THEMES_BASE_DIR), (None, None, []))[1]

class PleskPluginInstaller(Base):
    def __init__(self):
        self.ROOT_PLESK_DIR = "/usr/local/psa/admin/"
        self.source_admin = SOURCE_PATH_ADMIN
        self.source_user = SOURCE_PATH_USER
        self.destination_admin = "/usr/local/psa/admin/htdocs/modules/plesk-lvemanager/awp-admin"
        self.destination_user = "/usr/local/psa/admin/htdocs/modules/plesk-lvemanager/awp-user"
        self.controller_src = SOURCE_PATH_BASE + "user-plugins/plesk/AwpController.php"
        self.controller_dst = self.ROOT_PLESK_DIR + "plib/modules/plesk-lvemanager/controllers/AwpController.php"
        self.send_request_controller_src = SOURCE_PATH_BASE + "user-plugins/plesk/AwpSendRequestController.php"
        self.send_request_controller_dst = self.ROOT_PLESK_DIR + "plib/modules/plesk-lvemanager/controllers/AwpSendRequestController.php"
        self.icon_src = SOURCE_PATH_BASE + "user-plugins/plesk/awp.svg"
        self.icon_dst = self.ROOT_PLESK_DIR + "htdocs/modules/plesk-lvemanager/images/awp.svg"
        self.views_dir = self.ROOT_PLESK_DIR + "plib/modules/plesk-lvemanager/views/scripts/awp/"
        self.template_src = SOURCE_PATH_BASE + "user-plugins/plesk/index.phtml"
        self.template_dst = self.ROOT_PLESK_DIR + "plib/modules/plesk-lvemanager/views/scripts/awp/index.phtml"

    def install_plugin(self):
        self.copy_file_or_dir(self.source_admin, self.destination_admin)
        self.copy_file_or_dir(self.source_user, self.destination_user)
        self.copy_file_or_dir(self.controller_src, self.controller_dst)
        self.copy_file_or_dir(self.send_request_controller_src, self.send_request_controller_dst)
        self.copy_file_or_dir(self.icon_src, self.icon_dst)
        if not os.path.isdir(self.views_dir):
            os.mkdir(self.views_dir)
        self.copy_file_or_dir(self.template_src, self.template_dst)

    def uninstall_plugin(self):
        self.remove_file_or_dir(self.destination_admin)
        self.remove_file_or_dir(self.destination_user)
        self.remove_file_or_dir(self.controller_dst)
        self.remove_file_or_dir(self.send_request_controller_dst)
        self.remove_file_or_dir(self.icon_dst)
        self.remove_file_or_dir(self.views_dir)

class DirectAdminPluginInstaller(Base):
    def __init__(self):
        # Plugin files
        self.source_user_plugin = SOURCE_PATH_BASE + "user-plugins/directadmin/awp/"
        self.destination_user_plugin = "/usr/local/directadmin/plugins/awp/"
        self.source_index_file = SOURCE_PATH_BASE + "user-plugins/directadmin/awpIndex.php"
        self.destination_index_file = "/usr/local/directadmin/plugins/lvemanager_spa/app/View/Spa/index/awpIndex.php"
        self.plugin_conf_file = self.destination_user_plugin + "/plugin.conf"

        # Spa files
        self.source_admin_spa = SOURCE_PATH_ADMIN
        self.destination_admin_spa = "/usr/local/directadmin/plugins/lvemanager_spa/images/assets/awp-admin"
        self.source_user_spa = SOURCE_PATH_USER
        self.destination_user_spa = "/usr/local/directadmin/plugins/awp/images/awp-user"


    def install_plugin(self):
        # admin part
        self.copy_file_or_dir(self.source_admin_spa, self.destination_admin_spa)
        # user part
        self.copy_file_or_dir(self.source_user_plugin, self.destination_user_plugin)
        self.copy_file_or_dir(self.source_index_file, self.destination_index_file)
        self.copy_file_or_dir(self.source_user_spa, self.destination_user_spa)
        # set permission and owner
        self.exec_command("chown -R diradmin:diradmin " + self.destination_user_plugin)
        self.exec_command("chown -R diradmin:diradmin " + self.destination_admin_spa)
        self.exec_command("chmod -R 755 " + self.destination_user_plugin)
        self.exec_command("chmod -R 644 " + self.plugin_conf_file)

    def uninstall_plugin(self):
        self.remove_file_or_dir(self.destination_user_plugin)
        self.remove_file_or_dir(self.destination_admin_spa)


class PanelIntegrationPluginInstaller(Base):
    def __init__(self):
        self.source_admin = SOURCE_PATH_ADMIN
        self.source_user = SOURCE_PATH_USER
        self.destination_admin = "/usr/share/l.v.e-manager/commons/spa-resources/static/awp-admin"
        self.destination_user = "/usr/share/l.v.e-manager/commons/spa-resources/static/awp-user"

    def get_panel_base_path(self):
        try:
            parser = configparser.ConfigParser(interpolation=None, strict=False)
            parser.read(PANEL_INTEGRATION_CONFIG)
            base_path = parser.get("lvemanager_config", "base_path")
            return base_path
        except Exception as e:
            print("Cannot copy files for no panel version. {}".format(e))
            sys.exit(1)

    def install_plugin(self):
        base_path = self.get_panel_base_path().rstrip('/')
        self.copy_file_or_dir(self.source_admin, self.destination_admin)
        self.copy_file_or_dir(self.source_admin, base_path + '/assets/awp-admin')
        self.copy_file_or_dir(self.source_user, self.destination_user)
        self.copy_file_or_dir(self.source_user, base_path + '/assets/awp-user')

    def uninstall_plugin(self):
        base_path = self.get_panel_base_path().rstrip('/')
        self.remove_file_or_dir(self.destination_admin)
        self.remove_file_or_dir(base_path + '/assets/awp-admin')
        self.remove_file_or_dir(self.destination_user)
        self.remove_file_or_dir(base_path + '/assets/awp-user')


class Main:
    def __init__(self):
        pass

    def make_parser(self):
        parser = argparse.ArgumentParser(description="Script to install|uninstall AccelerateWP")
        parser.add_argument("--install", "-i", action="store_true",
                            help="Install AccelerateWP")
        parser.add_argument("--uninstall", "-u", action="store_true",
                            help="Uninstall AccelerateWP")
        return parser

    def run(self):
        parser = self.make_parser()
        args = parser.parse_args()
        installer_class = None

        if detect.is_cpanel():
            # Cpanel
            installer = CpanelPluginInstaller()
        elif detect.is_plesk():
            # Plesk
            installer = PleskPluginInstaller()
        elif detect.is_da():
            # DirectAdmin
            installer = DirectAdminPluginInstaller()
        elif os.path.isfile(PANEL_INTEGRATION_CONFIG):
            # Custom panel with integration.ini
            installer = PanelIntegrationPluginInstaller()
        else:
            print("AccelerateWP plugin cannot be installed on your environment")
            sys.exit(0)

        if args.install:
            installer.install_plugin()
            installer.generate_translate_templates()
            installer.sync_ui_config()
        elif args.uninstall:
            installer.uninstall_plugin()
            installer.sync_ui_config()
        else:
            parser.print_help()


if __name__ == "__main__":
    main = Main()
    main.run()
