#!/bin/bash

# Generate SSL certificates for Dovecot testing

set -e

CERT_DIR="dovecot/ssl"
mkdir -p "$CERT_DIR"

# Generate DH parameters
echo "Generating DH parameters..."
openssl dhparam -out "$CERT_DIR/dh.pem" 2048

# Generate private key
echo "Generating private key..."
openssl genrsa -out "$CERT_DIR/server.key" 2048

# Generate certificate signing request
echo "Generating certificate..."
openssl req -new -key "$CERT_DIR/server.key" -out "$CERT_DIR/server.csr" -subj "/C=US/ST=Test/L=Test/O=Mail2Couch/CN=localhost"

# Generate self-signed certificate
openssl x509 -req -days 365 -in "$CERT_DIR/server.csr" -signkey "$CERT_DIR/server.key" -out "$CERT_DIR/server.crt"

# Clean up CSR
rm "$CERT_DIR/server.csr"

echo "SSL certificates generated successfully in $CERT_DIR/"