#!/bin/bash
# PWA Apple Icon Generator using GraphicsMagick or ImageMagick

OUTPUT_DIR="public/icons/apple"
mkdir -p "$OUTPUT_DIR"

# Check which tool is available
if command -v gm &> /dev/null; then
    CONVERT_CMD="gm convert"
elif command -v magick &> /dev/null; then
    CONVERT_CMD="magick"
else
    echo "Warning: ImageMagick/GraphicsMagick not found. Installing Python fallback..."
fi

# Input file
INPUT_FILE="${1:-apple_original.jpg}"

if [ ! -f "$INPUT_FILE" ]; then
    echo "Error: Input file '$INPUT_FILE' not found"
    exit 1
fi

echo "Generating PWA Apple icon sizes..."
echo ""

# Define icon sizes for PWA
declare -A SIZES=(
    ["apple-192.png"]=192
    ["apple-256.png"]=256
    ["apple-384.png"]=384
    ["apple-512.png"]=512
    ["apple-touch-icon-180.png"]=180
    ["apple-touch-icon-167.png"]=167
    ["apple-touch-icon-152.png"]=152
    ["apple-touch-icon-120.png"]=120
    ["apple-96.png"]=96
    ["apple-64.png"]=64
    ["apple-32.png"]=32
)

for filename in "${!SIZES[@]}"; do
    size=${SIZES[$filename]}
    output_path="$OUTPUT_DIR/$filename"
    
    if [ ! -z "$CONVERT_CMD" ]; then
        $CONVERT_CMD "$INPUT_FILE" -resize "${size}x${size}!" -quality 95 "$output_path" 2>/dev/null
        if [ $? -eq 0 ]; then
            echo "  ✓ $filename (${size}x${size})"
        fi
    fi
done

# Create manifest.json
cat > "$OUTPUT_DIR/manifest.json" << 'EOF'
{
  "name": "My PWA App",
  "short_name": "App",
  "description": "Progressive Web App",
  "start_url": "/",
  "display": "standalone",
  "background_color": "#ffffff",
  "theme_color": "#000000",
  "icons": [
    {
      "src": "/icons/apple/apple-192.png",
      "sizes": "192x192",
      "type": "image/png",
      "purpose": "any"
    },
    {
      "src": "/icons/apple/apple-256.png",
      "sizes": "256x256",
      "type": "image/png",
      "purpose": "any"
    },
    {
      "src": "/icons/apple/apple-384.png",
      "sizes": "384x384",
      "type": "image/png",
      "purpose": "any"
    },
    {
      "src": "/icons/apple/apple-512.png",
      "sizes": "512x512",
      "type": "image/png",
      "purpose": "any"
    }
  ]
}
EOF

# Create HTML snippet
cat > "$OUTPUT_DIR/html-head-snippet.html" << 'EOF'
<!-- PWA & iOS Home Screen Meta Tags -->
<meta name="theme-color" content="#000000">
<meta name="mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
<meta name="apple-mobile-web-app-title" content="My App">

<!-- iOS Home Screen Icons -->
<link rel="apple-touch-icon" href="/icons/apple/apple-touch-icon-180.png" sizes="180x180">
<link rel="apple-touch-icon" href="/icons/apple/apple-touch-icon-167.png" sizes="167x167">
<link rel="apple-touch-icon" href="/icons/apple/apple-touch-icon-152.png" sizes="152x152">
<link rel="apple-touch-icon" href="/icons/apple/apple-touch-icon-120.png" sizes="120x120">

<!-- PWA Manifest -->
<link rel="manifest" href="/icons/apple/manifest.json">

<!-- Favicon Fallbacks -->
<link rel="icon" type="image/png" href="/icons/apple/apple-192.png" sizes="192x192">
<link rel="icon" type="image/png" href="/icons/apple/apple-512.png" sizes="512x512">
<link rel="shortcut icon" href="/icons/apple/apple-32.png">
EOF

echo ""
echo "✓ All icons generated in: $OUTPUT_DIR/"
echo "✓ manifest.json created"
echo "✓ html-head-snippet.html created"
echo ""
echo "Total sizes created: ${#SIZES[@]} PNG files"
