#!/bin/sh
set -eu

config_file='/etc/tandoor/recipes.conf'

if [ $(grep '@@SECRET_KEY@@' "$config_file") ]; then
	echo "* Generating random secret in $config_file" >&2

	secret_key="$(pwgen -s 50 1)"
	sed -i "s|@@SECRET_KEY@@|$secret_key|" "$config_file"
fi

if [ "${0##*.}" = 'post-upgrade' ]; then
	cat >&2 <<-EOF
	*
	* To finish Recipes upgrade run:
	*
	*     recipes-manage migrate
	*
	EOF
else
	cat >&2 <<-EOF
	*
	* 1. Adjust settings in $config_file
	*
	* 2. Create database for Recipes:
	*
	*     psql -c "CREATE ROLE recipes PASSWORD 'top-secret' INHERIT LOGIN;"
	*     psql -c "CREATE DATABASE recipes OWNER recipes ENCODING 'UTF-8';"
	*     psql -c "CREATE EXTENSION pg_trgm;"
	*
	* 3. User optimizations:
	*
	*     psql -c "ALTER ROLE recipes SET client_encoding TO 'utf8';"
	*     psql -c "ALTER ROLE recipes SET default_transaction_isolation TO 'read committed';"
	*     psql -c "ALTER ROLE recipes SET timezone TO 'UTC';"
	*
	* 4. Run "recipes-manage migrate"
	*
	EOF
fi