import subprocess
import config
from pathlib import Path


def sym_link_dir():
    confirm_link = input("Do you want to link home directories to external storage? (Y/n): ")

    if confirm_link.lower() not in ("y", "yes", ""):
        return

    target_dir = Path(input("Specify the full path to the external storage: "))

    if not target_dir.is_dir():
        return

    for subdir in config.DIRECTORIES:
        dir_item = Path.home() / subdir

        if not dir_item.is_symlink():
            if dir_item.is_dir():
                dir_item.rmdir()
            dir_item.symlink_to(target_dir / subdir)

    for subdir in config.DIRECTORIES_TO_REMOVE:
        dir_item = Path.home() / subdir

        if dir_item.is_dir() and not dir_item.is_symlink():
            dir_item.rmdir()


def install_apps():
    subprocess.run(
        ['sudo', 'pacman', '-S', '--noconfirm', *config.APPS],
        check=True
    )

    subprocess.run(
        ["fish", "-c", "fisher install jorgebucaran/nvm.fish"],
        check=True
    )

    subprocess.run(
        ["fish", "-c", "nvm install lts"],
        check=True
    )


def stow_dot_files():
    dotfiles_root = Path.home() / '.dotfiles'

    subprocess.run(
        ['stow', '-d', str(dotfiles_root), *config.DOTFILES, '--adopt'],
        check=True
    )

    subprocess.run(
        ['git', 'checkout', '--', *config.DOTFILES],
        cwd=dotfiles_root,
        check=True,
    )


if __name__ == "__main__":
    sym_link_dir()
    install_apps()
    stow_dot_files()
