#!/bin/sh
set -eu

group=www-data
config_file='/etc/bookstack/bookstack.conf'

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

	secret_key="$(bookstack-manage key:generate --show)"
	sed -i "s|@@SECRET_KEY@@|$secret_key|" "$config_file"
fi

if [ "${0##*.}" = 'post-upgrade' ]; then
	cat >&2 <<-EOF
	*
	* To finish BookStack upgrade run:
	*
	*     bookstack-manage migrate
	*
	EOF
else
	cat >&2 <<-EOF
	*
	* 1. Adjust settings in /etc/bookstack/bookstack.conf
	*
	* 2. Create database for BookStack:
	*
	*     sudo mysql -e "create database bookstack;"
	*     sudo mysql -e "grant all privileges on bookstack.* TO 'bookstack'@'localhost' identified by '(password)';" 
	*     sudo mysql -e "flush privileges;"
	*
	* 3. Create tables:
	*
	*     bookstack-manage migrate
	*
	* 4. Create admin user
	*
	*     bookstack-manage bookstack:create-admin
	*
	EOF
fi

