#!/bin/sh # This script runs as root to set up permissions, then switches to nodejs user # Ensure uploads directory exists and has correct permissions mkdir -p /app/uploads/logos chown -R nodejs:nodejs /app/uploads chmod -R 755 /app/uploads # Wait for database to be ready echo "Waiting for database to be ready..." while ! nc -z postgres 5432; do echo "Database not ready, waiting..." sleep 1 done echo "Database is ready!" # Check if this is a fresh database by looking for the devices table echo "Checking database state..." if su-exec nodejs node -e " const { Sequelize } = require('sequelize'); require('dotenv').config(); const sequelize = new Sequelize(process.env.DB_NAME, process.env.DB_USER, process.env.DB_PASSWORD, { host: process.env.DB_HOST, port: process.env.DB_PORT, dialect: 'postgres', logging: false }); sequelize.authenticate().then(() => { return sequelize.getQueryInterface().describeTable('devices'); }).then(() => { console.log('TABLES_EXIST'); process.exit(0); }).catch(() => { console.log('FRESH_DATABASE'); process.exit(0); }); " | grep -q "FRESH_DATABASE"; then echo "Fresh database detected. Running initial setup..." su-exec nodejs npm run db:setup else echo "Existing database detected. Running migrations..." su-exec nodejs npm run db:migrate fi # Check if setup/migrations were successful if [ $? -eq 0 ]; then echo "Database initialization completed successfully" else echo "Database initialization failed" exit 1 fi # Switch to nodejs user and execute the command with dumb-init for signal handling exec su-exec nodejs dumb-init -- "$@"