#!/usr/bin/env bash

create_manifest() {
  local backup_dir="$1"
  local mode="$2"
  local services="$3"
  local repo_root
  local created_at
  local git_commit
  local service
  local first=1

  repo_root="$(backup_repo_root)"
  created_at="$(backup_utc_now)"
  git_commit="$(backup_git_commit "$repo_root")"

  {
    printf '{\n'
    printf '  "format_version": "1",\n'
    printf '  "created_at": "%s",\n' "$created_at"
    printf '  "git_commit": "%s",\n' "$git_commit"
    printf '  "mode": "%s",\n' "$mode"
    printf '  "environment_scope": "same-env-only",\n'
    printf '  "services": ['
    for service in $services; do
      if [[ "$first" -eq 1 ]]; then
        first=0
      else
        printf ', '
      fi
      printf '"%s"' "$service"
    done
    printf '],\n'
    printf '  "restore_policy": {\n'
    printf '    "requires_empty_target": true,\n'
    printf '    "requires_confirmation": "baron-sso",\n'
    printf '    "auto_run_migrations": false,\n'
    printf '    "works_relay_auto_resume": false\n'
    printf '  }\n'
    printf '}\n'
  } >"$backup_dir/manifest.json"
}
