#!/bin/bash
# Script to create htpasswd file for Docker Registry
# Usage: ./CREATE_AUTH.sh <username> <password>

if [ -z "$1" ] || [ -z "$2" ]; then
    echo "Usage: $0 <username> <password>"
    echo "Example: $0 admin mypassword"
    exit 1
fi

USERNAME=$1
PASSWORD=$2
AUTH_DIR="$(dirname "$0")/auth"

# Create auth directory if it doesn't exist
mkdir -p "$AUTH_DIR"

# Create or update htpasswd file
docker run --rm --entrypoint htpasswd httpd:2 -Bbn "$USERNAME" "$PASSWORD" > "$AUTH_DIR/htpasswd"

# Set proper permissions
chmod 644 "$AUTH_DIR/htpasswd"

echo "✅ htpasswd file created successfully!"
echo "Username: $USERNAME"
echo "Location: $AUTH_DIR/htpasswd"
