# -*- coding: utf-8 -*-
##################################################
# This file is part of WAPT Enterprise
# All right reserved, (c) Tranquil IT Systems 2023
# For more information please refer to
# https://wapt.tranquil.it/store/licences.html
##################################################
from setuphelpers import *

'''
    In order to capture packets, you must install the “ChmodBPF” launch daemon.
    You can do so by opening the Install ChmodBPF.pkg file in the Wireshark .dmg or from Wireshark itself by opening Wireshark → About Wireshark selecting the “Folders” tab, and double-clicking “macOS Extras”.
    The installer package includes Wireshark along with ChmodBPF and system path packages. See the included Read me first.html file for more details.
'''

app_name = 'Wireshark.app'

def install():
    persistent_folder = get_persistent_package_file('Wireshark_' + control.architecture + '_' + control.version)

    dmg_mount_path = mount_dmg(glob.glob('Wireshark*.dmg')[0])
    try:
        install_app(os.path.join(dmg_mount_path, app_name))
        install_pkg(os.path.join(dmg_mount_path, 'Add Wireshark to the system path.pkg'))
        install_pkg(os.path.join(dmg_mount_path, 'Install ChmodBPF.pkg'))

        # Copy of files in persistent location of this package for uninstall
        if not isdir(persistent_folder):
            mkdirs(persistent_folder)
        filecopyto(os.path.join(dmg_mount_path, 'Uninstall ChmodBPF.pkg'), os.path.join(persistent_folder, 'Uninstall ChmodBPF.pkg'))
        filecopyto(os.path.join(dmg_mount_path, 'Remove Wireshark from the system path.pkg'), os.path.join(persistent_folder, 'Remove Wireshark from the system path.pkg'))

        unmount_dmg(dmg_mount_path)
    except Exception:
        unmount_dmg(dmg_mount_path)
        raise

def uninstall():
    persistent_folder = get_persistent_package_file('Wireshark_' + control.architecture + '_' + control.version)

    uninstall_app(app_name)
    if isdir(os.path.join('/Library', 'Application Support', 'Wireshark')):
        remove_tree(os.path.join('/Library', 'Application Support', 'Wireshark'))

    install_pkg(os.path.join(persistent_folder, 'Uninstall ChmodBPF.pkg'))
    install_pkg(os.path.join(persistent_folder, 'Remove Wireshark from the system path.pkg'))

    remove_tree(persistent_folder)

def get_persistent_package_file(fname):
    if isdir(makepath(os.getcwd(), "WAPT", "persistent")):
        return makepath(os.getcwd(), "WAPT", "persistent", fname)
    else:
        return makepath(WAPT.persistent_root_dir, control.package_uuid, fname)