#!/usr/bin/env bash
# API key path substituted at build time (@API_KEY_FILE@).
set -euo pipefail

COPY_URL=0
if [ "$#" -ge 1 ] && [ "$1" = "--copy-url" ]; then
  COPY_URL=1
  shift
fi

APP_NAME=""
if [ "$#" -ge 2 ] && [ "$1" = "--app-name" ]; then
  APP_NAME="$2"
  shift 2
fi

API_KEY_FILE=@API_KEY_FILE@
if [ -r "$API_KEY_FILE" ]; then
  SWIFTSHARE_API_KEY="$(tr -d '\n' < "$API_KEY_FILE")"
fi

if [ -z "${SWIFTSHARE_API_KEY:-}" ]; then
  notify-send "SwiftShare upload" "SwiftShare API key missing (expected readable: $API_KEY_FILE)"
  echo "Error: SwiftShare API key missing (expected readable: $API_KEY_FILE)" >&2
  exit 1
fi

IMAGE_FILE=""
RESPONSE_FILE=""
cleanup() {
  if [ -n "$RESPONSE_FILE" ] && [ -f "$RESPONSE_FILE" ]; then
    rm -f "$RESPONSE_FILE"
  fi
}
trap cleanup EXIT

if [ "$#" -ge 1 ] && [ "$1" != "-" ]; then
  IMAGE_FILE="$1"
  if [ "${IMAGE_FILE#/}" = "${IMAGE_FILE}" ]; then
    IMAGE_FILE="$(readlink -f "${IMAGE_FILE}")"
  fi
  if [ ! -f "$IMAGE_FILE" ]; then
    echo "Error: file not found: $IMAGE_FILE" >&2
    exit 1
  fi
else
  APP_NAME="${APP_NAME:-screenshot}"
  APP_NAME="${APP_NAME%% *}"
  APP_NAME="${APP_NAME//[^A-Za-z0-9]/}"
  APP_NAME="${APP_NAME,,}"
  if [ -z "$APP_NAME" ]; then
    APP_NAME="screenshot"
  fi

  IMAGE_FILE="$(mktemp --suffix=.png "${TMPDIR:-/tmp}/${APP_NAME}_XXXXXX")"
  cat > "$IMAGE_FILE"
fi

if [ ! -s "$IMAGE_FILE" ]; then
  notify-send "SwiftShare" "Empty capture (maybe canceled) – not uploading"
  echo "Empty image file, not uploading." >&2
  exit 0
fi

RESPONSE_FILE="$(mktemp)"
set +e
HTTP_STATUS="$(curl -sS -o "${RESPONSE_FILE}" -w '%{http_code}' \
  -X POST "https://swiftshare.cloud/api/upload/sharex" \
  -F "upload=${IMAGE_FILE}" \
  -F "apiKey=${SWIFTSHARE_API_KEY}")"
CURL_EXIT=$?
set -e

RESPONSE="$(cat "${RESPONSE_FILE}")"

if [ "${CURL_EXIT}" -ne 0 ]; then
  notify-send "SwiftShare upload failed" "Network or HTTP error (curl exit ${CURL_EXIT})"
  echo "SwiftShare upload failed (curl exit ${CURL_EXIT})." >&2
  echo "Response body:" >&2
  echo "${RESPONSE}" >&2
  exit 1
fi

if ! echo "${HTTP_STATUS}" | grep -qE '^2[0-9][0-9]$'; then
  ERROR_MSG="$(jq -r '.error // empty' <<< "${RESPONSE}")"
  if [ -z "${ERROR_MSG}" ] || [ "${ERROR_MSG}" = "null" ]; then
    ERROR_MSG="Failed to upload file"
  fi
  notify-send "SwiftShare upload failed (${HTTP_STATUS})" "${ERROR_MSG}"
  echo "SwiftShare upload failed (HTTP ${HTTP_STATUS}): ${ERROR_MSG}" >&2
  exit 1
fi

URL="$(jq -r '.url // empty' <<< "${RESPONSE}")"
THUMBNAIL="$(jq -r '.thumbnail // empty' <<< "${RESPONSE}")"

if [ -z "$URL" ] || [ "$URL" = "null" ]; then
  notify-send "SwiftShare upload failed" "Could not parse URL from response"
  echo "Upload failed. Raw response:" >&2
  echo "${RESPONSE}" >&2
  exit 1
fi

echo "$URL"
if [ -n "$THUMBNAIL" ] && [ "$THUMBNAIL" != "null" ]; then
  echo "$THUMBNAIL"
fi

if [ "$COPY_URL" = "1" ]; then
  wl-copy <<< "$URL"
fi

if [ -n "$IMAGE_FILE" ] && [ -f "$IMAGE_FILE" ]; then
  notify-send \
    -a "SwiftShare" \
    -i "$IMAGE_FILE" \
    -h string:image-path:"$IMAGE_FILE" \
    "SwiftShare upload" "Uploaded image: $URL"
else
  notify-send "SwiftShare upload" "Uploaded image: $URL"
fi
