#!{{ pkgPathFor "core/bash" }}/bin/bash
set -e

HOME="{{ pkg.svc_data_path }}"
export HOME
. '{{ pkg.svc_config_path }}/app_env.sh'

mkdir -pv '{{ pkg.svc_var_path }}/log'
mkdir -pv '{{ pkg.svc_var_path }}/tmp'
mkdir -pv '{{ pkg.svc_var_path }}/public/system'

{{ #if cfg.app.log_directory ~}}
log_symlink='{{ pkg.path }}/app/log'
if [ -h "${log_symlink}" ]; then
  rm -f "${log_symlink}"
  ln -sv '{{ cfg.app.log_directory }}' "${log_symlink}"
fi
{{~ /if }}

{{ #if cfg.app.var_directory ~}}
var_symlink='{{ pkg.path }}/app/public/system'
if [ -h "${var_symlink}" ]; then
  rm -f "${var_symlink}"
  ln -sv '{{ cfg.app.var_directory }}' "${var_symlink}"
fi
{{~ /if }}

env_file="{{ pkg.path }}/app/.env.{{ cfg.rails_env }}"
if [ -f "${env_file}" ]; then
  rm -f "${env_file}"
fi
ln -sv "{{ pkg.svc_config_path }}/app_env.sh" "${env_file}"

# Check that the 'SECRET_KEY_BASE' environment variable is non-empty
if [ -z "${SECRET_KEY_BASE:-}" ]; then
  >&2 echo "Required environment variable SECRET_KEY_BASE is not set."
  >&2 echo "Set this package's config setting 'secret_key_base' to a"
  >&2 echo "non-empty value. You can run 'rails secret' in development"
  >&2 echo "to generate a random key string."
  >&2 echo ""
  >&2 echo "Aborting..."
  exit 5
fi

{{ #if bind.database ~}}
{{ #with bind.database.first ~}}
PGHOST='{{ sys.ip }}'
PGUSER='{{ cfg.username }}'
PGPASSWORD='{{ cfg.password }}'
PGPORT='{{ cfg.port }}'
{{~ /with }}
{{~ else ~}}
PGHOST='{{ cfg.db.host }}'
PGDATABASE='{{ cfg.db.name }}_{{ cfg.rails_env }}'
PGUSER='{{ cfg.db.user }}'
PGPASSWORD='{{ cfg.db.password }}'
PGPORT='{{ cfg.db.port }}'
{{~ /if }}
PGDATABASE='{{ cfg.db.name }}_{{ cfg.rails_env }}'
export PGHOST PGDATABASE PGUSER PGPASSWORD PGPORT

pushd '{{ pkg.path }}'/app &>/dev/null || exit 1
  if psql --command '' &>/dev/null; then
    echo 'Database Exists'
    bundle exec rails db:migrate
  else
    echo 'Database does not exist'
    bundle exec rails db:create
    bundle exec rails db:schema:load
    bundle exec rails db:migrate
    bundle exec rails db:seed
  fi

  {{ #each cfg.db.extensions as |extension| ~}}
  psql --quiet --command 'CREATE EXTENSION IF NOT EXISTS {{ extension }}'
  {{ /each }}
popd &>/dev/null || exit 1

# Confirm an initial database connection
if ! {{ pkg.path }}/libexec/is_db_connected; then
  >&2 echo ""
  >&2 echo "A database connection is required for this app to properly boot."
  >&2 echo "Is the database not running or are the database connection"
  >&2 echo "credentials incorrect?"
  >&2 echo ""
{{ ~#if bind.database }}
  >&2 echo "This app started with a database bind and will discovery the"
  >&2 echo "hostname and port number in the Habitat ring."
  >&2 echo ""
  >&2 echo "There are 3 remaining config settings which must be set correctly:"
{{ else }}
  >&2 echo "This app started without a database bind meaning that the"
  >&2 echo "database is assumed to be running outside of a Habitat ring."
  >&2 echo "Therefore, you must provide all the database connection values."
  >&2 echo ""
  >&2 echo "There are 5 config settings which must be set correctly:"
{{ ~/if }}
  >&2 echo ""
{{ ~#unless bind.database }}
  >&2 echo " * db.host      - The database hostname or IP address (Current: {{#if cfg.db.host}}{{cfg.db.host}}{{else}}<unset>{{/if}})"
  >&2 echo " * db.port      - The database listen port number (Current: {{#if cfg.db.port}}{{cfg.db.port}}{{else}}5432{{/if}})"
{{ ~/unless }}
  >&2 echo " * db.adapter   - The database adapter (Current: {{#if cfg.db.adapter}}{{cfg.db.adapter}}{{else}}postgresql{{/if}})"
  >&2 echo " * db.user      - The database username (Current: {{#if cfg.db.user}}{{cfg.db.user}}{{else}}<unset>{{/if}})"
  >&2 echo " * db.password  - The database password (Current: {{#if cfg.db.password}}<set>{{else}}<unset>{{/if}})"
  >&2 echo " * db.name      - The database name (Current: {{#if cfg.db.name}}{{cfg.db.name}}{{else}}<unset>{{/if}})"
  >&2 echo ""
  >&2 echo "Aborting..."
  exit 15
fi