#!/usr/bin/env bash
# Bug Bounty Screenshot Helper with Automatic Borders and Annotations
# Usage: bb-screenshot.sh [program-name]

set -euo pipefail

PROGRAM="${1:-current}"
SCREENSHOT_DIR="${HOME}/bug-bounty/${PROGRAM}/screenshots"
TIMESTAMP=$(date +%Y%m%d_%H%M%S)
TEMP_FILE="/tmp/flameshot_${TIMESTAMP}.png"
FINAL_FILE="${SCREENSHOT_DIR}/${TIMESTAMP}_screenshot.png"

# Colors
GREEN='\033[0;32m'
BLUE='\033[0;34m'
YELLOW='\033[1;33m'
NC='\033[0m'

# Ensure screenshot directory exists
mkdir -p "$SCREENSHOT_DIR"

echo -e "${BLUE}[+] Bug Bounty Screenshot Tool${NC}"
echo -e "${YELLOW}[!] Take your screenshot, annotate as needed, then click Save${NC}"
echo ""

# Take screenshot with Flameshot (with annotation tools)
flameshot gui -p "$TEMP_FILE"

# Check if screenshot was actually taken (user might have cancelled)
if [[ ! -f "$TEMP_FILE" ]]; then
    echo -e "${YELLOW}[!] Screenshot cancelled${NC}"
    exit 0
fi

echo -e "${GREEN}[✓] Screenshot captured${NC}"
echo -e "${BLUE}[+] Adding professional border...${NC}"

# Add border and shadow using ImageMagick
convert "$TEMP_FILE" \
    -bordercolor '#333333' -border 2 \
    -bordercolor white -border 10 \
    -bordercolor '#333333' -border 2 \
    \( +clone -background black -shadow 80x5+5+5 \) \
    +swap -background white -layers merge +repage \
    "$FINAL_FILE"

# Remove temp file
rm "$TEMP_FILE"

echo -e "${GREEN}[✓] Screenshot saved with border: $FINAL_FILE${NC}"
echo -e "${BLUE}[+] Copying path to clipboard...${NC}"

# Copy filename to clipboard (for easy paste into JSON)
echo "screenshots/$(basename "$FINAL_FILE")" | xclip -selection clipboard 2>/dev/null || \
echo "screenshots/$(basename "$FINAL_FILE")" | wl-copy 2>/dev/null || \
echo -e "${YELLOW}[!] Could not copy to clipboard (install xclip or wl-clipboard)${NC}"

echo ""
echo -e "${GREEN}Path copied: screenshots/$(basename "$FINAL_FILE")${NC}"
echo -e "${YELLOW}[!] Paste this into your vulnerability JSON file${NC}"

# Optional: Open the screenshot to verify
if command -v feh &> /dev/null; then
    feh "$FINAL_FILE" &
elif command -v eog &> /dev/null; then
    eog "$FINAL_FILE" &
fi
