#!/bin/sh
# docker-entrypoint.sh
# Ensures data directories have correct permissions on container startup

set -e

echo "Checking data directory permissions..."

# Directories that need to be writable by nodejs user (UID 1001)
DATA_DIRS="/app/data/backups /app/data/documents"

for dir in $DATA_DIRS; do
  if [ ! -d "$dir" ]; then
    echo "Creating directory: $dir"
    mkdir -p "$dir"
  fi

  # Check if we can write to the directory
  if ! touch "$dir/.write-test" 2>/dev/null; then
    echo "WARNING: Cannot write to $dir"
    echo "This may cause backup/document operations to fail"
    echo "Fix: Run 'sudo chown -R 1001:1001 ./data' on the host"
  else
    rm "$dir/.write-test"
  fi
done

echo "Permission checks complete"
echo "Starting application..."

# Execute the CMD from Dockerfile
exec "$@"
