#!/bin/bash

echo "Checking if $TELEGRAM_DB database exists..."

if psql -lqt | cut -d \| -f 1 | grep -qw $TELEGRAM_DB; then
  echo "Database exists, skipping creation"
else
  echo "Database does not exist, creating..."
  createdb $TELEGRAM_DB
  createuser $TELEGRAM_USER
  psql -c "ALTER USER $TELEGRAM_USER WITH ENCRYPTED PASSWORD '$TELEGRAM_PASSWORD';"
  psql -c "GRANT ALL PRIVILEGES ON DATABASE $TELEGRAM_DB TO $TELEGRAM_USER;"
  psql -c "ALTER DATABASE $TELEGRAM_DB OWNER TO $TELEGRAM_USER;"
fi