Commit 51b89d16 authored by Andreas Dueren's avatar Andreas Dueren
Browse files

Simplify startup script to debug config generation issues

parent 1f84084b
Loading
Loading
Loading
Loading
+30 −81
Original line number Diff line number Diff line
@@ -20,94 +20,43 @@ CONFIG_PATH="/app/data/config.yaml"
REGISTRATION_PATH="/app/data/registration.yaml"
BACKUP_PATH="/app/data/config.yaml.bak"

# Create example config from built-in template
# Try to generate config from built-in template
if [ ! -f "$CONFIG_PATH" ]; then
    echo "=> Generating example configuration"
    # Generate config as root first, then fix permissions
    /app/pkg/mautrix-whatsapp -g -c "$CONFIG_PATH" -r "$REGISTRATION_PATH" || {
        echo "=> Config generation failed, creating minimal config"
        # Generate secure random tokens
        AS_TOKEN=$(openssl rand -hex 32)
        HS_TOKEN=$(openssl rand -hex 32)
    echo "=> Attempting to generate configuration using mautrix-whatsapp"
    /app/pkg/mautrix-whatsapp -g -c "$CONFIG_PATH" -r "$REGISTRATION_PATH"
    
    if [ ! -f "$CONFIG_PATH" ]; then
        echo "=> ERROR: Config generation failed and no config file was created"
        echo "=> Will try to start without config to see error messages"
    else
        echo "=> Config generation successful"
    fi
    
    # Only configure if config was successfully generated
    if [ -f "$CONFIG_PATH" ]; then
        echo "=> Applying basic Cloudron configuration"
        
        cat > "$CONFIG_PATH" << EOF
homeserver:
    address: https://matrix.example.com
    domain: example.com

appservice:
    address: https://example.com
    hostname: 0.0.0.0
    port: 29318
    as_token: $AS_TOKEN
    hs_token: $HS_TOKEN
    database:
        type: postgres
        uri: postgres://user:pass@localhost/db

bridge:
    username_template: whatsapp_{{.}}
    displayname_template: "{{if .BusinessName}}{{.BusinessName}}{{else}}{{.PushName}}{{end}} (WA)"
    
logging:
    handlers:
        console:
            format: json
        file:
            filename: /app/data/mautrix-whatsapp.log
    level: info
EOF
        cat > "$REGISTRATION_PATH" << EOF
id: whatsapp
url: https://example.com
as_token: $AS_TOKEN
hs_token: $HS_TOKEN
rate_limited: false
sender_localpart: whatsappbot
namespaces:
    users:
    - exclusive: true
      regex: '@whatsapp_.*:.*'
    aliases:
    - exclusive: true
      regex: '#whatsapp_.*:.*'
EOF
    }
    
    # Configure for Cloudron environment
        # Configure for Cloudron environment only if config exists
        if [ -n "${CLOUDRON_POSTGRESQL_URL:-}" ]; then
            echo "=> Configuring PostgreSQL database"
        yq eval ".appservice.database.uri = \"$CLOUDRON_POSTGRESQL_URL\"" -i "$CONFIG_PATH"
            yq eval ".appservice.database.uri = \"$CLOUDRON_POSTGRESQL_URL\"" -i "$CONFIG_PATH" 2>/dev/null || echo "=> Warning: Could not configure database"
        fi
        
        if [ -n "${CLOUDRON_APP_DOMAIN:-}" ]; then
            echo "=> Configuring homeserver and appservice settings"
        # For whatsapp.matrix.as.ci -> matrix.as.ci and domain as.ci
            BASE_DOMAIN=$(echo "$CLOUDRON_APP_DOMAIN" | cut -d. -f2-)
            
            # Update homeserver configuration
        yq eval ".homeserver.address = \"https://matrix.$BASE_DOMAIN\"" -i "$CONFIG_PATH"
        yq eval ".homeserver.domain = \"$BASE_DOMAIN\"" -i "$CONFIG_PATH"
            yq eval ".homeserver.address = \"https://matrix.$BASE_DOMAIN\"" -i "$CONFIG_PATH" 2>/dev/null || echo "=> Warning: Could not configure homeserver address"
            yq eval ".homeserver.domain = \"$BASE_DOMAIN\"" -i "$CONFIG_PATH" 2>/dev/null || echo "=> Warning: Could not configure homeserver domain"
            
            # Update appservice configuration
        yq eval ".appservice.address = \"https://$CLOUDRON_APP_DOMAIN\"" -i "$CONFIG_PATH"
        yq eval ".appservice.hostname = \"0.0.0.0\"" -i "$CONFIG_PATH"
        yq eval ".appservice.port = 29318" -i "$CONFIG_PATH"
            yq eval ".appservice.address = \"https://$CLOUDRON_APP_DOMAIN\"" -i "$CONFIG_PATH" 2>/dev/null || echo "=> Warning: Could not configure appservice address"
        fi
        
    # Set log file path
    yq eval ".logging.handlers.file.filename = \"/app/data/mautrix-whatsapp.log\"" -i "$CONFIG_PATH"
    
    chown cloudron:cloudron "$CONFIG_PATH" "$REGISTRATION_PATH"
    
    echo "=> Initial configuration complete"
    echo "=> IMPORTANT: Please review $CONFIG_PATH and configure your Matrix homeserver settings"
    echo "=> You will need to:"
    echo "   1. Copy $REGISTRATION_PATH to your Matrix homeserver"
    echo "   2. Update your homeserver configuration to include the registration file"
    echo "   3. Restart your Matrix homeserver"
    echo "   4. Restart this bridge app"
    echo "   5. Authenticate with WhatsApp using QR code scanning"
        chown cloudron:cloudron "$CONFIG_PATH" "$REGISTRATION_PATH" 2>/dev/null || true
        echo "=> Configuration applied successfully"
    fi
else
    echo "=> Using existing configuration"
    # Fix configuration in existing config if needed