#!/bin/bash

# Solana ICO DApp Deployment Script
# This script builds the app and creates deployment packages

echo "🚀 Starting Solana ICO DApp deployment process..."

# Check if Node.js is installed
if ! command -v node &> /dev/null; then
    echo "❌ Node.js is not installed. Please install Node.js first."
    exit 1
fi

# Check if npm is installed
if ! command -v npm &> /dev/null; then
    echo "❌ npm is not installed. Please install npm first."
    exit 1
fi

echo "📦 Installing dependencies..."
npm install

if [ $? -ne 0 ]; then
    echo "❌ Failed to install dependencies"
    exit 1
fi

echo "🔨 Building the application..."
npm run build

if [ $? -ne 0 ]; then
    echo "❌ Build failed"
    exit 1
fi

echo "✅ Build completed successfully!"

# Create deployment packages directory
echo "📁 Creating deployment packages..."
mkdir -p deployment-packages/cpanel
mkdir -p deployment-packages/netlify

# Copy build files to cPanel package
echo "📋 Copying files to cPanel package..."
cp -r out/* deployment-packages/cpanel/

# Copy build files to Netlify package
echo "📋 Copying files to Netlify package..."
cp -r out/* deployment-packages/netlify/

# Create .htaccess for cPanel
echo "⚙️ Creating .htaccess for cPanel..."
cat > deployment-packages/cpanel/.htaccess << 'EOF'
RewriteEngine On

# Handle client-side routing
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.html [L]

# Enable compression
<IfModule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/plain
    AddOutputFilterByType DEFLATE text/html
    AddOutputFilterByType DEFLATE text/xml
    AddOutputFilterByType DEFLATE text/css
    AddOutputFilterByType DEFLATE application/xml
    AddOutputFilterByType DEFLATE application/xhtml+xml
    AddOutputFilterByType DEFLATE application/rss+xml
    AddOutputFilterByType DEFLATE application/javascript
    AddOutputFilterByType DEFLATE application/x-javascript
</IfModule>

# Set cache headers
<IfModule mod_expires.c>
    ExpiresActive on
    ExpiresByType text/css "access plus 1 year"
    ExpiresByType application/javascript "access plus 1 year"
    ExpiresByType image/png "access plus 1 year"
    ExpiresByType image/jpg "access plus 1 year"
    ExpiresByType image/jpeg "access plus 1 year"
    ExpiresByType image/gif "access plus 1 year"
    ExpiresByType image/svg+xml "access plus 1 year"
</IfModule>

# Security headers
<IfModule mod_headers.c>
    Header always set X-Content-Type-Options nosniff
    Header always set X-Frame-Options DENY
    Header always set X-XSS-Protection "1; mode=block"
    Header always set Referrer-Policy "strict-origin-when-cross-origin"
</IfModule>
EOF

# Create _redirects for Netlify
echo "⚙️ Creating _redirects for Netlify..."
echo "/*    /index.html   200" > deployment-packages/netlify/_redirects

# Create netlify.toml for Netlify
echo "⚙️ Creating netlify.toml for Netlify..."
cat > deployment-packages/netlify/netlify.toml << 'EOF'
[build]
  publish = "."
  command = "npm run build"

[build.environment]
  NODE_VERSION = "18"

[[headers]]
  for = "/*"
  [headers.values]
    X-Frame-Options = "DENY"
    X-XSS-Protection = "1; mode=block"
    X-Content-Type-Options = "nosniff"
    Referrer-Policy = "strict-origin-when-cross-origin"

[[headers]]
  for = "/static/*"
  [headers.values]
    Cache-Control = "public, max-age=31536000, immutable"

[[headers]]
  for = "*.css"
  [headers.values]
    Cache-Control = "public, max-age=31536000, immutable"

[[headers]]
  for = "*.js"
  [headers.values]
    Cache-Control = "public, max-age=31536000, immutable"

[[headers]]
  for = "*.png"
  [headers.values]
    Cache-Control = "public, max-age=31536000, immutable"

[[headers]]
  for = "*.jpg"
  [headers.values]
    Cache-Control = "public, max-age=31536000, immutable"

[[headers]]
  for = "*.jpeg"
  [headers.values]
    Cache-Control = "public, max-age=31536000, immutable"

[[headers]]
  for = "*.gif"
  [headers.values]
    Cache-Control = "public, max-age=31536000, immutable"

[[headers]]
  for = "*.svg"
  [headers.values]
    Cache-Control = "public, max-age=31536000, immutable"

[[redirects]]
  from = "/*"
  to = "/index.html"
  status = 200
EOF

# Create ZIP files
echo "📦 Creating ZIP files..."
cd deployment-packages

if command -v zip &> /dev/null; then
    zip -r solana-ico-cpanel-deployment.zip cpanel/
    zip -r solana-ico-netlify-deployment.zip netlify/
elif command -v powershell &> /dev/null; then
    powershell Compress-Archive -Path "cpanel\*" -DestinationPath "solana-ico-cpanel-deployment.zip" -Force
    powershell Compress-Archive -Path "netlify\*" -DestinationPath "solana-ico-netlify-deployment.zip" -Force
else
    echo "⚠️  ZIP command not found. Please install zip or use PowerShell to create ZIP files manually."
fi

cd ..

echo "✅ Deployment packages created successfully!"
echo ""
echo "📁 Deployment packages are ready in the 'deployment-packages' directory:"
echo "   - cpanel/ (for cPanel hosting)"
echo "   - netlify/ (for Netlify hosting)"
echo "   - solana-ico-cpanel-deployment.zip"
echo "   - solana-ico-netlify-deployment.zip"
echo ""
echo "🚀 Ready for deployment!"





