#!/bin/bash

# AAF Systems Homepage - Create Transfer Package
# This script creates a complete transfer package with the Docker image and scripts

echo "📦 Creating Complete Transfer Package"
echo "===================================="

PACKAGE_DIR="aaf-systems-transfer-package"
IMAGE_TAR="aaf-systems-homepage-docker.tar"

# Check if Docker image tar exists
if [ ! -f "$IMAGE_TAR" ]; then
    echo "🔨 Docker image package not found. Building and packaging..."
    ./package-docker.sh
    
    if [ ! -f "$IMAGE_TAR" ]; then
        echo "❌ Failed to create Docker image package"
        exit 1
    fi
fi

# Create transfer package directory
echo "📁 Creating transfer package directory..."
rm -rf $PACKAGE_DIR
mkdir -p $PACKAGE_DIR

# Copy necessary files
echo "📋 Copying files to package..."
cp $IMAGE_TAR $PACKAGE_DIR/
cp load-and-run-docker.sh $PACKAGE_DIR/
cp stop-docker.sh $PACKAGE_DIR/

# Create a README for the transfer package
cat > $PACKAGE_DIR/README.txt << 'EOF'
AAF Systems Homepage - Transfer Package
======================================

This package contains everything needed to run the AAF Systems Homepage on a target computer.

Contents:
- aaf-systems-homepage-docker.tar: The Docker image
- load-and-run-docker.sh: Script to load and run the Docker image
- stop-docker.sh: Script to stop the Docker container

Instructions:
1. Make sure Docker is installed and running on the target computer
2. Copy this entire folder to the target computer
3. Open terminal in this folder
4. Run: chmod +x *.sh
5. Run: ./load-and-run-docker.sh

The application will be available at http://localhost:8080

To stop the application: ./stop-docker.sh
EOF

# Create archive
echo "🗜️  Creating compressed archive..."
tar -czf aaf-systems-transfer-package.tar.gz $PACKAGE_DIR

echo "✅ Transfer package created successfully!"
echo ""
echo "📦 Package Contents:"
echo "==================="
ls -la $PACKAGE_DIR/
echo ""
echo "📊 Archive Info:"
echo "==============="
ls -lh aaf-systems-transfer-package.tar.gz
echo ""
echo "🚚 Ready for Transfer:"
echo "====================="
echo "Transfer 'aaf-systems-transfer-package.tar.gz' to the target computer"
echo "On target computer: tar -xzf aaf-systems-transfer-package.tar.gz"
echo "Then follow instructions in the README.txt file"
