#!/bin/bash SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" VENV_DIR="$SCRIPT_DIR/.venv" ENV_FILE="$SCRIPT_DIR/.env" if [[ ! -d "$VENV_DIR" ]]; then echo "Creating Python virtual environment..." python3 -m venv "$VENV_DIR" "$VENV_DIR/bin/pip" install --upgrade pip "$VENV_DIR/bin/pip" install pillow screeninfo fi PYTHON_EXEC="$VENV_DIR/bin/python3" if [[ -f "$ENV_FILE" ]]; then set -o allexport source "$ENV_FILE" set +o allexport else echo "Error: .env file not found in script directory ($SCRIPT_DIR)." exit 1 fi if [[ -n "$WAYLAND_DISPLAY" ]]; then SCREENSHOT_CMD=("grim" "-g") SELECT_REGION="slurp" REQUIRED_COMMANDS=("curl" "jq" "wl-copy" "grim" "slurp") elif [[ -n "$DISPLAY" ]]; then SCREENSHOT_CMD=("flameshot" "gui" "-p") REQUIRED_COMMANDS=("curl" "jq" "xclip" "flameshot") else echo "Error: Unable to detect display server (Wayland/X11)." exit 1 fi MISSING_COMMANDS=() for cmd in "${REQUIRED_COMMANDS[@]}"; do command -v "$cmd" &>/dev/null || MISSING_COMMANDS+=("$cmd") done # Check for xdg-utils for xdg-open if ! command -v xdg-open &>/dev/null; then MISSING_COMMANDS+=("xdg-utils") fi if [[ ${#MISSING_COMMANDS[@]} -gt 0 ]]; then echo "Error: The following commands are not installed: ${MISSING_COMMANDS[*]}" exit 1 fi if [[ -z "$UPLOAD_AUTH" || -z "$UPLOAD_URL" ]]; then echo "Error: Required environment variables UPLOAD_AUTH or UPLOAD_URL are not set in .env." exit 1 fi HEADERS=( -H "authorization: $UPLOAD_AUTH" -H "content-type: multipart/form-data" ) if [[ -n "$UPLOAD_HEADERS" ]]; then IFS=';' read -ra ADDITIONAL_HEADERS <<< "$UPLOAD_HEADERS" for HEADER in "${ADDITIONAL_HEADERS[@]}"; do HEADERS+=(-H "$HEADER") done fi if [[ "$1" == "-h" || "$1" == "-help" ]]; then echo "Usage: $0 [-c | -clipboard] [FILE]" echo "Upload an image to atums.world and copy the URL to the clipboard" echo echo "Options:" echo " -c, -clipboard Upload the image from the clipboard" echo exit 0 fi TEMP_FILE="" if [[ "$1" == "-c" || "$1" == "-clipboard" ]]; then FILE="$2" if [[ -z "$FILE" ]]; then TEMP_FILE="/tmp/screenshot.png" if [[ -n "$WAYLAND_DISPLAY" ]]; then "${SCREENSHOT_CMD[@]}" "$("$SELECT_REGION")" "$TEMP_FILE" else "${SCREENSHOT_CMD[@]}" "$TEMP_FILE" fi if [[ $? -ne 0 ]]; then echo "Error: Screenshot capture failed." >&2 exit 1 fi FILE="$TEMP_FILE" fi if [[ ! -f "$FILE" ]]; then echo "Error: File not found: $FILE" >&2 exit 1 fi if [[ -n "$WAYLAND_DISPLAY" ]]; then echo "Using wl-copy for clipboard." cat "$FILE" | wl-copy elif [[ -n "$DISPLAY" ]]; then echo "Using xclip for clipboard." xclip -selection clipboard -t image/png -i "$FILE" else echo "Error: Unable to detect display server (Wayland/X11)." exit 1 fi "$PYTHON_EXEC" "$SCRIPT_DIR/show_image.py" "$FILE" "Copied to your clipboard" [[ -n "$TEMP_FILE" ]] && rm -f "$TEMP_FILE" exit 0 fi if [[ -z "$1" ]]; then TEMP_FILE="/tmp/screenshot.png" if [[ -n "$WAYLAND_DISPLAY" ]]; then "${SCREENSHOT_CMD[@]}" "$("$SELECT_REGION")" "$TEMP_FILE" else "${SCREENSHOT_CMD[@]}" "$TEMP_FILE" fi if [[ $? -ne 0 ]]; then echo "Error: Screenshot capture failed." >&2 exit 1 fi FILE="$TEMP_FILE" else FILE="$1" fi if [[ ! -f "$FILE" ]]; then echo "Error: File not found: $FILE" >&2 exit 1 fi FILE_URL=$(curl -s "${HEADERS[@]}" -F file=@"$FILE" "$UPLOAD_URL" | jq -r .files[0].url | tr -d '\n') if [[ -n "$WAYLAND_DISPLAY" ]]; then echo "Using wl-copy for clipboard." echo -n "$FILE_URL" | wl-copy elif [[ -n "$DISPLAY" ]]; then echo "Using xclip for clipboard." echo -n "$FILE_URL" | xclip -selection clipboard else echo "Error: Unable to detect display server (Wayland/X11)." exit 1 fi "$PYTHON_EXEC" "$SCRIPT_DIR/show_image.py" "$FILE" "$FILE_URL" "$FILE_URL" [[ -n "$TEMP_FILE" ]] && rm -f "$TEMP_FILE"