#!/usr/bin/env bash
# File picker dialog -> clipboard.
#   path    (default): copy the selected file's absolute path
#   content          : copy the selected file's contents
# Bound to Super+F (path) and Super+Shift+F (content) in usr/binds.lua.

set -euo pipefail

mode="${1:-path}"

# zenity exits non-zero on cancel/close -> treat as a no-op.
# Float/size/cursor-centering are handled by the +filepicker window rule.
file="$(zenity --file-selection --title="Copy file ${mode} to clipboard")" || exit 0
[ -n "$file" ] || exit 0

case "$mode" in
  content)
    if [ ! -r "$file" ]; then
      notify-send -u critical "File picker" "Cannot read: $file"
      exit 1
    fi
    wl-copy < "$file"
    notify-send "Copied file contents" "$(basename "$file")"
    ;;
  path | *)
    # Collapse the home dir to ~
    case "$file" in
      "$HOME"/*) file="~${file#"$HOME"}" ;;
      "$HOME")   file="~" ;;
    esac
    printf '%s' "$file" | wl-copy
    notify-send "Copied file path" "$file"
    ;;
esac
