#!/usr/bin/env bash

# ─< Helper functions >─────────────────────────────────────────────────────────────────
function echo_error() { echo -e "\033[0;1;31mError: \033[0;31m\t${*}\033[0m"; }
function echo_binfo() { echo -e "\033[0;1;34mINFO: \033[0;34m\t${*}\033[0m"; }
function echo_info() { echo -e "\033[0;1;35mInfo: \033[0;35m${*}\033[0m"; }

# ─< Check if the given command exists silently >─────────────────────────────────────────
command_exists() {
  command -v "$@" >/dev/null 2>&1
}

# ─< to get the alias to install everything either with your shell or inside your script! >─
if command_exists curl; then
  install_pkg() {
    bash --norc -c "$(curl -sSL https://git.k4li.de/pika/scripts/raw/branch/main/bash/snippets/install_pkg.sh)" "$@"
  }
else
  echo_error "curl is not installed, universal install disabled!"
fi

# ─< Silent execution >─────────────────────────────────────────────────────────────────
silentexec() {
  "$@" >/dev/null 2>&1
}

c_fonts() {
  local dirFonts="$HOME/.local/share/fonts/"
  if [[ ! -d "$dirFonts" ]]; then
    echo_info "Seems like you may miss some fonts.. Do you want to clone them now to <$HOME/.local/share/fonts/> ? [y|n]" && read -r ask_fonts

    case "$ask_fonts" in
    [Yy])
      if ! command_exists git; then
        install_pkg git &&
          git clone --depth=1 https://git.k4li.de/pika/fonts.git "$dirFonts"
      else
        git clone --depth=1 https://git.k4li.de/pika/fonts.git "$dirFonts"
      fi
      ;;
    [Nn])
      echo_binfo "You might have some font issues, but that's your business now!"
      return 0
      ;;
    *)
      c_fonts
      ;;
    esac
  fi
}

c_wallpapers() {
  local dirWallpaper="$HOME/.wallpapers"
  if [[ ! -d "$dirWallpaper" ]]; then
    echo_info "Seems like you may miss some wallpapers.. Do you want to clone them now to <$HOME/.wallpapers/> ? [y|n]" && read -r ask_wall

    case "$ask_wall" in
    [Yy])
      if ! command_exists git; then
        install_pkg git &&
          git clone --depth=1 https://git.k4li.de/pika/wallpaper.git "$dirWallpaper"
      else
        git clone --depth=1 https://git.k4li.de/pika/wallpaper.git "$dirWallpaper"
      fi
      ;;
    [Nn])
      echo_binfo "All right, be sure to checkout the $HOME/.config/hypr/.scripts/random_swww.sh to change the wallpaper dir"
      return 0
      ;;
    *)
      c_wallpapers
      ;;
    esac
  fi
}

mkdirs() {
  # ─< .config dir >────────────────────────────────────────────────────────────────────────
  if [[ ! -f $HOME/.config/ ]]; then
    echo_binfo "mkdir $HOME/.config"
    silentexec mkdir $HOME/.config
  fi
  if [[ ! -f $HOME/.local/share/icons/ ]]; then
    echo_binfo "mkdir -p $HOME/.local/share/icons/"
    silentexec mkdir -p $HOME/.local/share/icons/
  fi
  if [[ ! -f $HOME/.local/share/fonts/ ]]; then
    c_fonts || echo_binfo "mkdir -p $HOME/.local/share/fonts/" && silentexec mkdir -p $HOME/.local/share/fonts/
  fi

  c_wallpapers
}

mkdirs
