#!/bin/bash

set -e

SUPERUSER=postgres

repodir="$(dirname $(readlink -f "$0"))/.."
cd "$repodir"
basedir="${repodir}/dump/"

function do_cleanup () {
    make -C $repodir/../swh-storage/sql distclean filldb >/dev/null
    make -C $repodir/../swh-storage/sql/archiver distclean filldb >/dev/null
}

function extract () {
    git merge -s recursive -X theirs --no-edit master
    pg_extractor --getall --delete --basedir "$basedir" --nodbnamedir "$@"
    git add .
    git commit -m "Extracted database at $(LC_ALL=C date -u)" || true
}

for db in softwareheritage softwareheritage-archiver; do
    git checkout "${db}/swh-storage"
    extract -d "${db}-dev"
done

db=softwareheritage
for hostname in prado somerset; do
    git checkout "${db}/${hostname}"
    extract -d "${db}" --host "${hostname}.internal.softwareheritage.org" -U "${SUPERUSER}" -P 5433
done

db=softwareheritage-archiver
for hostname in prado; do
    git checkout "${db}/${hostname}"
    extract -d "${db}" --host "${hostname}.internal.softwareheritage.org" -U "${SUPERUSER}" -P 5434
done

git checkout master
