#!/usr/bin/env bash

# Exit on errors
set -e

# Variables
PDS_DATADIR="/var/lib/pds"
PDS_DID_PLC_URL="https://plc.directory"
PDS_BSKY_APP_VIEW_URL="https://api.bsky.app"
PDS_BSKY_APP_VIEW_DID="did:web:api.bsky.app"
PDS_REPORT_SERVICE_URL="https://mod.bsky.app"
PDS_REPORT_SERVICE_DID="did:plc:ar7c4by46qjdydhdevvrndac"
PDS_CRAWLERS="https://bsky.network"

# Generate secrets
PDS_JWT_SECRET=$(openssl rand --hex 32)
PDS_ADMIN_PASSWORD=$(openssl rand --hex 16)
PDS_PLC_ROTATION_KEY_K256_PRIVATE_KEY_HEX=$(openssl ecparam -name secp256k1 -genkey -noout -outform DER | tail -c 32 | xxd -p -c 32)

# Function to validate FQDN
validate_fqdn() {
    local fqdn=$1
    if [[ ! $fqdn =~ ^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9-]*[a-zA-Z0-9])\.)+[a-zA-Z]{2,}$ ]]; then
        echo "Invalid FQDN. Please enter a valid fully qualified domain name."
        return 1
    fi
    return 0
}

# Function to select protocol
select_protocol() {
    echo "Choose the protocol for SMTP:"
    echo "1) Implicit (smtps://, secure connection starts immediately)"
    echo "2) Explicit (smtp://, upgrades to secure connection with StartTLS)"
    while true; do
        read -p "Enter the number of your choice [1]: " choice
        case $choice in
            ""|1)
                SMTP_URI="smtp://"
                DEFAULT_PORT=587
                echo "Using Explicit (StartTLS)"
                break
                ;;
            2)
                SMTP_URI="smtps://"
                DEFAULT_PORT=465
                echo "Using Implicit SSL"
                break
                ;;
            *)
                echo "Invalid choice. Please try again."
                ;;
        esac
    done
}

# PDS Details
read -p "Enter your server's public DNS address (e.g., example.com): " PDS_HOSTNAME
read -p "Enter an admin email address (e.g., example@geeemail.com): " PDS_ADMIN_EMAIL

# SMTP Configuration
SMTP_SERVER="smtp.postmarkapp.com"

while true; do
    read -p "Enter your SMTP server address [$SMTP_SERVER]: " input_server
    SMTP_SERVER=${input_server:-$SMTP_SERVER}
    validate_fqdn "$SMTP_SERVER" && break
done

while true; do
    read -p "Enter your SMTP user (mandatory): " SMTP_USER
    if [[ -n "$SMTP_USER" ]]; then
        break
    else
        echo "SMTP user is required. Please try again."
    fi
done

read -p "Enter your SMTP password (optional): " SMTP_PASSWORD

# Protocol Selection
select_protocol

# Port
read -p "Enter SMTP port [default: $DEFAULT_PORT]: " input_port
SMTP_PORT=${input_port:-$DEFAULT_PORT}

read -p "Enter an allowed sender address: " SMTP_FROM

# Create the SMTP URI
SMTP_URI="${SMTP_URI}${SMTP_USER}:${SMTP_PASSWORD}@${SMTP_SERVER}:${SMTP_PORT}"

# Create .env file
cat <<EOF > pds.env
PDS_HOSTNAME=${PDS_HOSTNAME}
PDS_JWT_SECRET=${PDS_JWT_SECRET}
PDS_ADMIN_PASSWORD=${PDS_ADMIN_PASSWORD}
PDS_PLC_ROTATION_KEY_K256_PRIVATE_KEY_HEX=${PDS_PLC_ROTATION_KEY_K256_PRIVATE_KEY_HEX}
PDS_DATA_DIRECTORY=${PDS_DATADIR}
PDS_BLOBSTORE_DISK_LOCATION=${PDS_DATADIR}/blocks
PDS_BLOB_UPLOAD_LIMIT=52428800
PDS_DID_PLC_URL=${PDS_DID_PLC_URL}
PDS_BSKY_APP_VIEW_URL=${PDS_BSKY_APP_VIEW_URL}
PDS_BSKY_APP_VIEW_DID=${PDS_BSKY_APP_VIEW_DID}
PDS_REPORT_SERVICE_URL=${PDS_REPORT_SERVICE_URL}
PDS_REPORT_SERVICE_DID=${PDS_REPORT_SERVICE_DID}
PDS_CRAWLERS=${PDS_CRAWLERS}
PDS_EMAIL_SMTP_URL=${SMTP_URI}
PDS_EMAIL_FROM_ADDRESS=${SMTP_FROM}
LOG_ENABLED=true
EOF

echo "pds.env file created successfully."
