#!/usr/bin/env bash

set -euo pipefail

. "$(dirname "$0")/db-functions"

SRC_DIR=/var/lib/stackrox
TGT_DIR=/var/lib/stackrox/current
INIT_DIR=/var/lib/stackrox/.db-init

# The following process should be executed successfully only once.
# The following codes must be idempotent and allowing re-entry. The previous run may crash at any point.
if [ ! -L "$TGT_DIR" ]; then
  echo "Move databases to current"
  mkdir -p "$INIT_DIR"

  move_dbs "$SRC_DIR" "$INIT_DIR"

  ln -s "$INIT_DIR" "$TGT_DIR" # This must be the last step.
fi
