#!/usr/bin/env bash
set -euo pipefail

# Script Name: trash
# Description: Move files to trash instead of permanent deletion
# Source: https://evanhahn.com/scripts-i-wrote-that-i-use-all-the-time/
# Credit: macOS implementation modified from https://github.com/morgant/tools-osx
# Usage: trash file1 file2 dir1

if [[ $# -eq 0 ]]; then
  echo "Usage: trash <file1> [file2] ..." >&2
  exit 1
fi

if [[ "$(uname)" == 'Darwin' ]]; then
  # macOS: Use AppleScript through Finder
  for arg in "$@"; do
    file="$(realpath "$arg")"
    /usr/bin/osascript -e "tell application \"Finder\" to delete POSIX file \"$file\"" > /dev/null
  done
else
  # Linux: Use gio trash
  if ! command -v gio &>/dev/null; then
    echo "Error: 'gio' command not found. Install glib2 package." >&2
    exit 1
  fi
  gio trash "$@"
fi
