post_install() {
  THEME_NAME="melawy-lera-sugar"
  local install_dir="/usr/share/refind/themes/$THEME_NAME"

  echo "Searching rEFInd installation in EFI partition..."
  if ! __has_esp__; then
    echo "EFI partition not found" >&2
    exit 1
  fi

  REFIND_DIR=$(find "$ESP" -type d -iname refind)
  if ! [[ -d "${REFIND_DIR}" ]]; then
    echo "rEFInd not installed in $ESP" >&2
    exit 1
  fi
  echo "Found rEFInd in ${REFIND_DIR}"

  EFI_BOOT_DIR=$(find "$ESP" -type d -iname boot)
  if ! [[ -d "${EFI_BOOT_DIR}" ]]; then
    EFI_BOOT_DIR=$(find "$ESP" -type d -iname Boot)
    if ! [[ -d "${EFI_BOOT_DIR}" ]]; then
      EFI_BOOT_DIR=$(find "$ESP" -type d -iname BOOT)
      if ! [[ -d "${EFI_BOOT_DIR}" ]]; then
        mkdir -p "$(dirname ${REFIND_DIR})/boot"
        EFI_BOOT_DIR="$(dirname ${REFIND_DIR})/boot"
      fi
    fi
  fi

  if [ -f "${REFIND_DIR}/refind_x64.efi" ]; then
    echo "Installing theme in ${REFIND_DIR}/themes"
    cd ${install_dir}

    mkdir -p "${REFIND_DIR}/themes/$THEME_NAME"
    mkdir -p "${EFI_BOOT_DIR}/themes/$THEME_NAME"

    cp -vrf "$install_dir/." "${REFIND_DIR}/themes/$THEME_NAME/."
    cp -vrf "$install_dir/." "${EFI_BOOT_DIR}/themes/$THEME_NAME/."

    # Remove all from "${EFI_BOOT_DIR}" except "themes"
    f_list=$(ls -1 "${EFI_BOOT_DIR}" | grep -v "themes")

    for i in ${f_list[@]}
    do
      rm -vr "${EFI_BOOT_DIR}/${i}"
    done

    unset f_list

    # Copy all from "${REFIND_DIR}" to "${EFI_BOOT_DIR}" except "themes"
    f_list=$(ls -1 "${REFIND_DIR}" | grep -v "themes")

    for i in ${f_list[@]}
    do
      if [ -f ${REFIND_DIR}/${i} ]; then
        cp -vf "${REFIND_DIR}/${i}" "${EFI_BOOT_DIR}/${i}"
      fi

      if [ -d ${REFIND_DIR}/${i} ]; then
        cp -vrf "${REFIND_DIR}/${i}" "${EFI_BOOT_DIR}/${i}"
      fi
    done

    unset f_list

    mv -vf "${EFI_BOOT_DIR}/refind_x64.efi" "${EFI_BOOT_DIR}/bootx64.efi"
  fi
}

post_upgrade() {
  post_install
}

post_remove() {
  THEME_NAME="melawy-lera-sugar"

  echo "Removal of theme from EFI partition"

  echo "Searching rEFInd installation in EFI partition..."
  if ! __has_esp__; then
    echo "EFI partition not found" >&2
    exit 1
  fi

  REFIND_DIR=$(find "$ESP" -type d -iname refind)
  if ! [[ -d "${REFIND_DIR}" ]]; then
    echo "rEFInd not installed in $ESP" >&2
    exit 1
  fi
  echo "Found rEFInd in ${REFIND_DIR}"

  EFI_BOOT_DIR=$(find "$ESP" -type d -iname boot)
  if ! [[ -d "${EFI_BOOT_DIR}" ]]; then
    EFI_BOOT_DIR=$(find "$ESP" -type d -iname Boot)
    if ! [[ -d "${EFI_BOOT_DIR}" ]]; then
      EFI_BOOT_DIR=$(find "$ESP" -type d -iname BOOT)
      if ! [[ -d "${EFI_BOOT_DIR}" ]]; then
        mkdir -p "$(dirname ${REFIND_DIR})/boot"
        EFI_BOOT_DIR="$(dirname ${REFIND_DIR})/boot"
      fi
    fi
  fi

  if [[ -d "${REFIND_DIR}/themes/$THEME_NAME" ]]; then
    rm -vr "${REFIND_DIR}/themes/$THEME_NAME"
    echo "Theme uninstalled from ${REFIND_DIR}/themes/$THEME_NAME"
  fi

  if [[ -d "${EFI_BOOT_DIR}/themes/$THEME_NAME" ]]; then
    rm -vr "${EFI_BOOT_DIR}/themes/$THEME_NAME"
    echo "Theme uninstalled from ${EFI_BOOT_DIR}/themes/$THEME_NAME"
  fi

  REFIND_CONF_PATH="${REFIND_DIR}/refind.conf"
  sed -i "/$THEME_NAME/d" $REFIND_CONF_PATH

  cp -vf "${REFIND_DIR}/refind.conf" "${EFI_BOOT_DIR}/refind.conf"
}

# Copied from https://raw.githubusercontent.com/jaltuna/refind-theme-nord/main/setup.sh
# Verify EFI System Partition
__has_esp__() {
  __find_esp__

  mount "$ESP" &>/dev/null
  [[ -d "$ESP/EFI" ]] && return 0 || return 1
}

__find_esp__() {
  local parttype
  local fstype
  local device

  while read -r device; do
    read -r parttype fstype ESP <<<"$(lsblk -o "PARTTYPE,FSTYPE,MOUNTPOINT" "$device" 2>/dev/null | awk 'NR==2')"

    [[ "${parttype,,}" != "c12a7328-f81f-11d2-ba4b-00a0c93ec93b" ]] && continue
    [[ "${fstype,,}" != "vfat" ]] && continue
    [[ -z $(findmnt -sn "$ESP") ]] && continue

  done <<<"$(fdisk -l 2>/dev/null | grep -i efi | cut -d " " -f 1)"

  if [ -z "${ESP}" ]; then
    ESP=$(grep -i "/efi" /etc/fstab | awk '{print $2}')
    if [ -z "${ESP}" ]; then
      ESP=$(grep -i "/boot" /etc/fstab | awk '{print $2}')
      if [ -z "${ESP}" ]; then
        ESP=$(bootctl --print-esp-path)
        if [ -z "${ESP}" ]; then
          exit 1
        fi
      fi
    fi
  fi

  echo "Found ${ESP}"
  readonly ESP
}
