80 lines
2.9 KiB
Bash
80 lines
2.9 KiB
Bash
#!/bin/bash
|
|
|
|
# Wait for the container to fully initialize
|
|
sleep 1
|
|
|
|
# Default the TZ environment variable to UTC.
|
|
TZ=${TZ:-UTC}
|
|
export TZ
|
|
|
|
# Switch to the container's working directory
|
|
cd /home/container || exit 1
|
|
|
|
# Stalwart paths
|
|
STALWART_HOME="/opt/stalwart-mail"
|
|
STALWART_CONFIG="${STALWART_HOME}/etc/config.toml"
|
|
STALWART_BIN="${STALWART_HOME}/bin/stalwart-mail"
|
|
|
|
# Create config from template if not exists
|
|
if [ ! -f "${STALWART_CONFIG}" ]; then
|
|
echo "Generating Stalwart configuration..."
|
|
cp /opt/stalwart-mail/etc/config.toml "${STALWART_CONFIG}"
|
|
|
|
# Replace placeholders with environment variables
|
|
sed -i "s|{{MAIL_DOMAIN}}|${MAIL_DOMAIN:-mail.example.com}|g" "${STALWART_CONFIG}"
|
|
sed -i "s|{{MAIL_HOSTNAME}}|${MAIL_HOSTNAME:-$(hostname)}|g" "${STALWART_CONFIG}"
|
|
sed -i "s|{{ADMIN_EMAIL}}|${ADMIN_EMAIL:-admin@${MAIL_DOMAIN:-mail.example.com}}|g" "${STALWART_CONFIG}"
|
|
sed -i "s|{{HTTP_PORT}}|${HTTP_PORT:-8080}|g" "${STALWART_CONFIG}"
|
|
sed -i "s|{{SMTP_PORT}}|${SMTP_PORT:-465}|g" "${STALWART_CONFIG}"
|
|
sed -i "s|{{SUBMISSION_PORT}}|${SUBMISSION_PORT:-587}|g" "${STALWART_CONFIG}"
|
|
sed -i "s|{{IMAP_PORT}}|${IMAP_PORT:-993}|g" "${STALWART_CONFIG}"
|
|
sed -i "s|{{MANAGESIEVE_PORT}}|${MANAGESIEVE_PORT:-4190}|g" "${STALWART_CONFIG}"
|
|
|
|
echo "Configuration generated."
|
|
fi
|
|
|
|
# Generate admin password if not set
|
|
if [ ! -f "${STALWART_HOME}/.admin_created" ]; then
|
|
ADMIN_PASS=${ADMIN_PASSWORD:-$(openssl rand -base64 16)}
|
|
echo ""
|
|
echo "=========================================="
|
|
echo " Stalwart Mail Server"
|
|
echo "=========================================="
|
|
echo " Admin URL: http://${SERVER_IP:-0.0.0.0}:${HTTP_PORT:-8080}"
|
|
echo " Admin Email: ${ADMIN_EMAIL:-admin@${MAIL_DOMAIN:-mail.example.com}}"
|
|
echo " Admin Pass: ${ADMIN_PASS}"
|
|
echo "=========================================="
|
|
echo " IMPORTANT: Save these credentials!"
|
|
echo "=========================================="
|
|
echo ""
|
|
|
|
touch "${STALWART_HOME}/.admin_created"
|
|
fi
|
|
|
|
# Clone Git repos if GIT_REPOS is set
|
|
if [ ! -z "${GIT_REPOS}" ]; then
|
|
IFS=',' read -ra REPOS <<< "$GIT_REPOS"
|
|
for repo in "${REPOS[@]}"; do
|
|
repo=$(echo "$repo" | xargs)
|
|
if [ ! -z "$repo" ]; then
|
|
REPO_NAME=$(basename "$repo" .git)
|
|
CLONE_DIR="${STALWART_HOME}/plugins/${REPO_NAME}"
|
|
if [ -d "${CLONE_DIR}/.git" ]; then
|
|
echo "Updating plugin: ${REPO_NAME}..."
|
|
cd "${CLONE_DIR}" && git pull && cd /home/container
|
|
else
|
|
echo "Cloning plugin: ${REPO_NAME}..."
|
|
mkdir -p "${STALWART_HOME}/plugins"
|
|
git clone "$repo" "${CLONE_DIR}"
|
|
fi
|
|
fi
|
|
done
|
|
fi
|
|
|
|
# Replace Startup Variables
|
|
MODIFIED_STARTUP=$(echo ${STARTUP} | sed -e 's/{{/${/g' -e 's/}}/}/g')
|
|
echo -e ":/home/container$ ${MODIFIED_STARTUP}"
|
|
|
|
# Run the Server
|
|
eval ${MODIFIED_STARTUP}
|