#!/bin/bash
# - bof - #



<<'comment'

# -  - #
# - File name - #
Yazi_CLI-FileManager.sh

bash <(curl -s https://www.education.isdevelopment.us/Yazi-FileManager/Yazi_CLI-FileManager.sh)



cd ~/; rm -rf ~/Yazi_.sh; vim ~/Yazi_.sh;
copy & paste this content
chmod +x ~/Yazi_.sh; ~/./Yazi_.sh;

ssh-keygen -R 192.168.1.1

# - On client - #
cd ~/.ssh;
ssh-keygen -t rsa;

cat ~/.ssh/id_rsa.pub;

# - On server - #
vim ~/.ssh/authorized_keys;

sudo apt install curl git inxi net-tools ntp neovim -y;
sudo apt install curl git ifupdown2 inxi net-tools ntp neovim -y;

# -  - #
# - Documentation - #
https://www.youtube.com/watch?v=RgV47l59NYs
https://yazi-rs.github.io/docs/installation
https://github.com/sxyazi/yazi/tree/shipped/yazi-config/preset

comment



# -  - #
# - InstallFirefox_ - #
function InstallFirefox_(){
  local ffProfile_=""
  local ffBackupDir_="$HOME/.firefox-backup-$(date +%Y-%m-%d_%H-%M-%S)"

  echo -e "\n\n\nMigrating Firefox to Flatpak...\n";

  # 1. Save existing Firefox profile if it exists
  if [ -d "$HOME/.mozilla/firefox" ]; then
    echo -e "\nSaving existing Firefox profile...\n";
    mkdir -p "$ffBackupDir_"
    cp -a "$HOME/.mozilla" "$ffBackupDir_/"
    echo -e "Profile saved to: $ffBackupDir_\n";
  else
    echo -e "No existing Firefox profile found.\n";
  fi

  # 2. Remove any snap or apt Firefox
  echo -e "\nRemoving old Firefox installations...\n";
  sudo snap remove firefox 2>/dev/null && echo " - Snap Firefox removed"
  case $Opt in
    1 | 3 | 4 | 5 )
      sudo apt purge -y firefox 2>/dev/null && echo " - APT Firefox removed"
      ;;
    2 )
      sudo pacman -Rns --noconfirm firefox 2>/dev/null && echo " - Pacman Firefox removed"
      ;;
  esac

  # 3. Install Flatpak Firefox
  echo -e "\nInstalling Firefox via Flatpak...\n";
  flatpak remote-add --if-not-exists flathub https://flathub.org/repo/flathub.flatpakrepo;
  flatpak install -y flathub org.mozilla.firefox;

  # 4. Restore profile to Flatpak location if we saved one
  if [ -d "$ffBackupDir_/.mozilla/firefox" ]; then
    echo -e "\nRestoring profile to Flatpak Firefox...\n";
    mkdir -p "$HOME/.var/app/org.mozilla.firefox"
    cp -a "$ffBackupDir_/.mozilla" "$HOME/.var/app/org.mozilla.firefox/"
    echo -e "Profile restored.\n";
    echo -e "Backup also kept at: $ffBackupDir_\n";
  fi

  echo -e "\nFirefox migration complete! Run: flatpak run org.mozilla.firefox\n";
  sleep 2
}



# -  - #
# - InstallYazi_ - #
function InstallYazi_(){
  local lPrograms1_="ffmpeg fzf jq 7zip poppler ripgrep fd zoxide file-roller"
  echo -e "\n\n\nInstalling yazi...\n";
  Log_ 7;
  update_;

  # Install flatpak per distro
  case $Opt in
    1 | 3 | 4 | 5 )
      echo -e "\nInstalling flatpak...\n";
      sudo apt install -y flatpak;
      ;;
    2 )
      echo -e "\nInstalling flatpak...\n";
      sudo pacman -S --noconfirm --needed flatpak;
      ;;
  esac

  # Install yazi via flatpak (same for all distros)
  flatpak remote-add --if-not-exists flathub https://flathub.org/repo/flathub.flatpakrepo;
  flatpak install -y flathub io.github.sxyazi.yazi;

  # Install optional helpers
  sPackages_ "$lPrograms1_"

  # Install Firefox
  InstallFirefox_;

  # Grant flatpak filesystem access
  FlatpakOverride_;

  Configure_Yazi_;
}



# -  - #
# - Configure_Yazi_ - #
function Configure_Yazi_(){
  # Flatpak yazi config path
  local yaziConfigDir="$HOME/.var/app/io.github.sxyazi.yazi/config/yazi"
  
  echo -e "\nSetting up your custom overrides for Yazi...\n"

  mkdir -p "$yaziConfigDir"

  # 1. Add your custom overrides to yazi.toml
  tee "$yaziConfigDir/yazi.toml" << 'EOF' > /dev/null

# -  - #
# - Open files for view or edition when press enter or right click - #

[mgr]
ratio = [ 3, 3, 6 ]

[opener]
edit = [
  { run = '${EDITOR:-nvim} "$@"', desc = "$EDITOR", block = true, for = "unix" },
  { run = 'code %*',    orphan = true, desc = "code",           for = "windows" },
  { run = 'code -w %*', block = true,  desc = "code (block)",   for = "windows" },
]
archive = [
  { run = 'file-roller "$@"', desc = "File Roller", for = "unix", orphan = true },
]

[open]
rules = [
  # Folder
  { url = "*/", use = [ "edit", "open", "reveal" ] },
  # Text
  { mime = "text/*", use = [ "edit", "reveal" ] },
  # Image
  { mime = "image/*", use = [ "open", "reveal" ] },
  # Media
  { mime = "{audio,video}/*", use = [ "play", "reveal" ] },
  # Code
  { mime = "application/{json,ndjson,javascript,wine-extension-ini}", use = [ "edit", "reveal" ] },
  # Archive
  { mime = "application/{zip,rar,7z*,tar,gzip,xz,zstd,bzip*,lzma,compress,archive,cpio,arj,xar,ms-cab*}", use = [ "archive", "reveal" ] },
  # Empty file
  { mime = "inode/empty", use = [ "edit", "reveal" ] },
  # Fallback
  { url = "*", use = [ "open", "reveal" ] },
]
EOF

  # 2. Add your custom keymap overrides to keymap.toml
  tee "$yaziConfigDir/keymap.toml" << 'EOF' > /dev/null

# -  - #
# - Shift + a  Select all files - #

[manager]
prepend_keymap = [
  { on = "<Space>", run = [ "toggle", "arrow 1" ], desc = "Toggle the current selection state" },
  { on = "<S-a>",   run = "toggle_all --state=on", desc = "Select all files" },
  { on = "<C-r>",   run = "toggle_all",            desc = "Invert selection of all files" }
]
EOF

  echo -e "Custom configurations merged successfully.\n"
  sleep 2
}



# -  - #
# - FlatpakOverride_ - #
function FlatpakOverride_(){
  echo -e "\nGranting Flatpak Yazi filesystem access...\n";
  flatpak override --user io.github.sxyazi.yazi --filesystem=host
  echo -e "Flatpak Yazi can now access all system directories.\n"
  sleep 2
}



# -  - #
# - Rc_Alias_ - #
function Rc_Alias_(){
  local rcAlias_=~/.rc-Alias

  # 1. Clean up existing references if they exist
  if [ -f "$rcAlias_" ]; then
    sed -i '/source <(fzf --zsh)/d' "$rcAlias_"
    sed -i '/export EDITOR=/d' "$rcAlias_"
    sed -i '/^function y()/,/^}/d' "$rcAlias_"
    sed -i '/^y()/,/^}/d' "$rcAlias_"
    sed -i '/# - eof - #/d' "$rcAlias_"
    sed -i -e :a -e '/^\n*$/{$d;N;ba}' "$rcAlias_"
  else
    touch "$rcAlias_"
  fi

  # 2. Append the structured layout
  cat << 'EOF' >> "$rcAlias_"



source <(fzf --zsh)
export EDITOR="nvim"

function y() {
   local tmp="$(mktemp -t "yazi-cwd.XXXXXX")" cwd
   flatpak run io.github.sxyazi.yazi "$@" --cwd-file="$tmp"
   if cwd="$(command cat -- "$tmp")" && [ -n "$cwd" ] && [ "$cwd" != "$PWD" ]; then
      builtin cd -- "$cwd"
   fi
   rm -f -- "$tmp"
}



# - eof - #
EOF
}



clear;
logF_=~/.noInstalled-Yazi_CLI-FileManager_$(date +%Y-%m-%d_%H-%M-%S).log;

source <(curl -s https://www.education.isdevelopment.us/Functions/Functions.sh)
distroVersion_;

InstallYazi_;
Rc_Alias_;



rm -rf $Yazi_.sh;

echo -e "\n\nYazi has been installed successfully,\n \
System will reboot so changes can be applied...\n\n";

sleep 5;
sudo reboot now;


<<'comment'
cd ~/; rm -rf ~/Yazi_.sh; vim ~/Yazi_.sh;
copy & paste this content
chmod +x ~/Yazi_.sh; ~/./Yazi_.sh;

ssh-keygen -R 192.168.1.1
~/./Yazi_.sh;
comment



# - eof - #
