main
  1#!/bin/sh
  2
  3YES_MODE=0
  4PLATFORM_ARG=""
  5
  6show_help() {
  7  echo "Usage: $(basename "$0") <platform> [OPTIONS]"
  8  echo ""
  9  echo "Description:"
 10  echo "  Read lines from the system clipboard and add them to packwiz."
 11  echo "  Can detect macOS, Wayland, and X11 environments automatically."
 12  echo ""
 13  echo "Arguments:"
 14  echo "  platform      The platform to add mods from."
 15  echo "                Must be either 'mr' (Modrinth) or 'cf' (CurseForge)."
 16  echo ""
 17  echo "Options:"
 18  echo "  -y, --yes     Automatically answer yes to all prompts."
 19  echo "  -h, --help    Show this help message and exit."
 20}
 21
 22get_clipboard() {
 23  if [ "$(uname)" = "Darwin" ]; then
 24    # macOS
 25    pbpaste
 26  elif [ -n "$WAYLAND_DISPLAY" ] && command -v wl-paste >/dev/null 2>&1; then
 27    # Linux Wayland
 28    wl-paste
 29  elif [ -n "$DISPLAY" ]; then
 30    # Linux X11
 31    if command -v xclip >/dev/null 2>&1; then
 32      xclip -selection clipboard -o
 33    elif command -v xsel >/dev/null 2>&1; then
 34      xsel --clipboard --output
 35    else
 36      echo "Error: X11 environment detected but 'xclip' or 'xsel' not found." >&2
 37      exit 1
 38    fi
 39  else
 40    echo "Error: Could not detect a supported clipboard provider." >&2
 41    echo "  (Checked for: pbpaste, wl-paste, xclip, xsel)" >&2
 42    exit 1
 43  fi
 44}
 45
 46while [ $# -gt 0 ]; do
 47  case "$1" in
 48  -h | --help)
 49    show_help
 50    exit 0
 51    ;;
 52  -y | --yes)
 53    YES_MODE=1
 54    shift
 55    ;;
 56  mr | cf)
 57    if [ -n "$PLATFORM_ARG" ]; then
 58      echo "Error: Platform specified twice ('$PLATFORM_ARG' and '$1')." >&2
 59      exit 1
 60    fi
 61    PLATFORM_ARG="$1"
 62    shift
 63    ;;
 64  -*)
 65    echo "Error: Unknown option '$1'." >&2
 66    show_help
 67    exit 1
 68    ;;
 69  *)
 70    echo "Error: Invalid argument '$1'. Platform must be 'mr' or 'cf'." >&2
 71    exit 1
 72    ;;
 73  esac
 74done
 75
 76if [ -z "$PLATFORM_ARG" ]; then
 77  echo "Error: Missing platform argument (mr or cf)." >&2
 78  show_help
 79  exit 1
 80fi
 81
 82PLATFORM_CMD="$PLATFORM_ARG"
 83if [ "$PLATFORM_CMD" = "mr" ]; then
 84  PLATFORM_NAME="Modrinth"
 85else
 86  PLATFORM_NAME="CurseForge"
 87fi
 88
 89if ! command -v packwiz >/dev/null 2>&1; then
 90  echo "Error: 'packwiz' command not found in PATH." >&2
 91  exit 1
 92fi
 93
 94PACKWIZ_ARGS=""
 95if [ "$YES_MODE" -eq 1 ]; then
 96  PACKWIZ_ARGS="-y"
 97fi
 98
 99echo "Reading clipboard for: $PLATFORM_NAME ($PLATFORM_CMD)..."
100echo "----------------------------------------"
101
102get_clipboard | sed '/^[[:space:]]*$/d' | while IFS= read -r line; do
103  case "$line" in \#*) continue ;; esac
104
105  printf "Processing: %s ... " "$line"
106
107  echo ""
108  if packwiz "$PLATFORM_CMD" add $PACKWIZ_ARGS "$line" </dev/tty; then
109    echo "-> OK"
110  else
111    echo "-> FAILED"
112  fi
113  echo "----------------------------------------"
114done
115
116echo "Done."