"""
* Author: "PeppermintOS Team(peppermintosteam@proton.me)
*
* License: SPDX-License-Identifier: GPL-3.0-or-later
*
* Move and copy files as needed
"""
import os
import shutil
from pathlib import Path
import conf
import paths
import inflate_bubble
import finish_cleanup
import copy_folders
import copy_files_specified

# Set the home path used regardless the user logged in
BSTRING = paths.bstring_iso_configs
home_folder = Path(BSTRING).expanduser()

# Move to iso_configs
os.chdir(home_folder)

# Set the working path
WP_CHROOT = paths.WPCHROOT

# Set the current working folder
cur_dir = os.getcwd().replace('/', '/')

def lbinit():
    """ runs the lb config command for the iso build"""
    current_dir = os.getcwd().replace('/', '/')
    os.chdir(current_dir + '/fusato')
    lbsetup = (conf.lbset_devarm)
    os.system(lbsetup)



def copy_devarm_specific():
    """" Devuan arm64 copy jobs"""
    shutil.copytree(cur_dir + paths.src_devarm_osrelease,
                    WP_CHROOT + paths.des_osrelease,
                    dirs_exist_ok=True
                    )
    shutil.copytree(cur_dir + paths.src_devarm_osrelease,
                    WP_CHROOT + paths.des_osrelease_opt,
                    dirs_exist_ok=True
                    )
    shutil.copytree(cur_dir + paths.src_devarm_slpash,
                    cur_dir + paths.des_splash,
                    dirs_exist_ok=True
                    )
    shutil.copytree(cur_dir + paths.src_devarm_isolinux,
                    cur_dir + paths.des_isolinux,
                    dirs_exist_ok=True
                    )
    shutil.copytree(cur_dir + paths.src_devarm_multi,
                    cur_dir + paths.des_archives,
                    dirs_exist_ok=True
                    )
    shutil.copytree(cur_dir + paths.src_dev_grub_theme,
                    WP_CHROOT + paths.des_grub_theme,
                    dirs_exist_ok=True
                    )
    shutil.copytree(cur_dir + paths.src_devarm_modules,
                    WP_CHROOT + paths.des_modules,
                    dirs_exist_ok=True
                    )
    shutil.copytree(cur_dir + paths.src_dev_splash_image,
                    cur_dir + paths.des_splash_image_isolinux,
                    dirs_exist_ok=True
                    )
    shutil.copytree(cur_dir + paths.src_dev_splash_image,
                    cur_dir + paths.des_splash_image_grub,
                    dirs_exist_ok=True
                    )
    shutil.copytree(cur_dir + paths.src_dev_live_theme,
                    cur_dir + paths.des_live_theme,
                    dirs_exist_ok=True
                    )

    shutil.copy(cur_dir + paths.src_devarm_grub_etc,
                WP_CHROOT + paths.des_grub_etc
                )
    shutil.copy(cur_dir + paths.src_devarm_settings,
                WP_CHROOT + paths.des_setttings
                )
    shutil.copy(cur_dir + paths.src_devarm_installer,
                WP_CHROOT + paths.des_installer
                )
    shutil.copy(cur_dir + paths.src_devarm_sourcesfinal,
                WP_CHROOT + paths.des_sourcesfinal
                )
    shutil.copy(cur_dir + paths.src_devarm_bootloader,
                WP_CHROOT + paths.des_bootloader
                )
    shutil.copy(cur_dir + paths.src_devarm_netinstall,
                WP_CHROOT + paths.des_netinstall
                )
    shutil.copy(cur_dir + paths.src_devarm_sourcelist,
                WP_CHROOT + paths.des_sourcelist
                )


def run_build():
    run_cmd = 'sudo lb build'
    os.chdir(home_folder)
    os.chdir('fusato')
    os.system(run_cmd)


def readybuild():
    """" Ready the bld structure"""
    if os.path.exists('fusato'):
        rm_cmd = "sudo rm -r -f fusato"
        os.system(rm_cmd)
        os.makedirs('fusato')
        # Run lb config to begin the setup
        lbinit()
        # Prep the folders
        inflate_bubble.set_fusato_structure()
        # Packages
        inflate_bubble.set_desktop_environment()
        inflate_bubble.set_extra_packages()
        inflate_bubble.set_general_packages()
        inflate_bubble.set_system_packages()
        inflate_bubble.set_artwork_packages()
        inflate_bubble.set_python_packages()
        inflate_bubble.set_calamares_packages()
        inflate_bubble.set_firmware_packages_arm()
        inflate_bubble.set_binary_packages_arm64()
        inflate_bubble.set_system_packages_arm()
        copy_folders.copy_folders_files()
        copy_files_specified.copy_specific_files()
        copy_files_specified.set_symlinks()
        copy_devarm_specific()
        run_build()
        finish_cleanup.make_check_sum()
        finish_cleanup.copy_iso_file_nightly_devarm()
        finish_cleanup.kill_old_iso()
    else:
        os.makedirs('fusato')
        # Run lb config to begin the setup
        lbinit()
        # Prep the folders
        inflate_bubble.set_fusato_structure()
        # Packages
        inflate_bubble.set_desktop_environment()
        inflate_bubble.set_extra_packages()
        inflate_bubble.set_general_packages()
        inflate_bubble.set_system_packages()
        inflate_bubble.set_artwork_packages()
        inflate_bubble.set_python_packages()
        inflate_bubble.set_calamares_packages()
        inflate_bubble.set_firmware_packages_arm()
        inflate_bubble.set_binary_packages_arm64()
        inflate_bubble.set_system_packages_arm()
        copy_folders.copy_folders_files()
        copy_files_specified.copy_specific_files()
        copy_files_specified.set_symlinks()
        copy_devarm_specific()
        run_build()
        finish_cleanup.make_check_sum()
        finish_cleanup.copy_iso_file_nightly_devarm()
        finish_cleanup.kill_old_iso()


readybuild()