#!/bin/bash
# Extract FCM_KEY from .env and create firebase-service-account.json

cd /opt/sojorn

# Extract the JSON from .env (everything between FCM_KEY={ and the closing })
grep -A 50 "FCM_KEY=" .env | sed -n '/FCM_KEY={/,/^}/p' | sed 's/FCM_KEY=//' > firebase-service-account.json

# Set proper permissions
chmod 600 firebase-service-account.json
chown patrick:patrick firebase-service-account.json

# Add FIREBASE_CREDENTIALS_FILE to .env if not present
if ! grep -q "FIREBASE_CREDENTIALS_FILE" .env; then
    echo "" >> .env
    echo "# Firebase Cloud Messaging" >> .env
    echo "FIREBASE_CREDENTIALS_FILE=/opt/sojorn/firebase-service-account.json" >> .env
fi

echo "Firebase service account JSON created at /opt/sojorn/firebase-service-account.json"
ls -la /opt/sojorn/firebase-service-account.json
