#!/usr/bin/env bash

SCRIPT_PATH=$( cd "$(dirname "$0")" ; pwd -P )

IMAGE_NAME="postgres"
IMAGE_VERSION="10.9"

POSTGIS_MAJOR="2.5"
POSTGIS_VERSION="2.5.2+dfsg-1~exp1.pgdg90+1"

SERVICE_NAME=pgsql-db

source ${SCRIPT_PATH}/../common.shinc

#
# Project specific variables
#

DB_VOLUME=pgsql_persistent_10
PORT=5432

source ${SCRIPT_PATH}/env.shinc 2> /dev/null


__build() {
	docker build \
		--build-arg IMAGE_NAME=${IMAGE_NAME} \
		--build-arg VERSION=${IMAGE_VERSION} \
		--build-arg POSTGIS_MAJOR=${IMAGE_VERSION} \
		--build-arg POSTGIS_VERSION=${IMAGE_VERSION} \
		-t ${SERVICE_NAME}:latest \
		${SCRIPT_PATH}/build

	return $?

}

init() {
	__init

	NETWORKS=(${DB_NETWORK})
	__createNetworks

	__msg "Removing old postgres bin volume..."
	docker volume rm -f ${PGSQL_DB_BIN} &&
	docker create \
		--name ${SERVICE_NAME} \
		--restart=unless-stopped \
		-v ${DB_VOLUME}:/var/lib/postgresql/data \
		-v ${PGSQL_DB_BIN}:/usr/lib/postgresql/${IMAGE_VERSION}/bin \
		-e POSTGRES_USER=${DB_USER} \
		-e POSTGRES_PASSWORD=${DB_PASSWORD} \
		--net ${DB_NETWORK} \
		-p ${PORT}:5432 \
		${IMAGE_NAME}:${IMAGE_VERSION}

	[[ $? -ne 0 ]] && return 1

	__ask_to_start
}

import() {
  local dbname="$1"
  shift
  local filename="$1"
  shift

  if [[ "${dbname}" == "" ]] || [[ "${filename}" == "" ]]; then
    __err "You must provide database name and filename of the file you want to import"
    __msg "E.g. ./run import my-database-name \"\$PWD/my-file.sql\""
    return 137
  fi

  if [[ ! -f "${filename}" ]]; then
    __err "The filename was not located at ${filename}"
    return 137
  fi

  echo -e "${C_WARN}This will import the file located at \"${C_WHITE}${filename}${C_WARN}\" to a database named \"${C_WHITE}${dbname}${C_WARN}\"${C_NONE}"

  echo

  __warn "Do you want to continue? [y/(n)] "

  read CONTINUE

  if [[ "${CONTINUE}" != "y" ]]; then
    __msg "Quiting..."
    return 0
  fi

  __msg "Importing, please wait..."

  local path=$(realpath "${filename}")
  local name=$(basename "${path}")

  docker run --rm -it -v "${path}":"/import/${name}" --net ${DB_NETWORK} ${IMAGE_NAME}:${IMAGE_VERSION} pg_restore --no-owner -Fc --host=${SERVICE_NAME} --password --dbname=${dbname} "/import/${name}"
}

migrate9to10() {
  local migrationVolume="pgsql-9-10-dump-migration"
  local migrationPath="/pgsql-data-dump"
  stop
  docker volume create ${migrationVolume}

  docker run --rm -d \
    --name postgres_migration_from_9 \
    -v ${DB_VOLUME}:/var/lib/postgresql/data \
    -v ${PGSQL_DB_BIN}:/usr/lib/postgresql/${IMAGE_VERSION}/bin \
    -v ${migrationVolume}:${migrationPath} \
    -e POSTGRES_USER=${DB_USER} \
    -e POSTGRES_PASSWORD=${DB_PASSWORD} \
    mdillon/postgis:9.5

  echo "Waiting for postgres to initialize"
  sleep 10

  docker exec -it \
    postgres_migration_from_9 \
    bash -c "pg_dumpall > ${migrationPath}/full.dump"

  if [[ $? -ne 0 ]]; then
    docker stop postgres_migration_from_9
    __err "Bad thing, don't continue, quitsies, bye"
    exit 137
  fi

  docker stop postgres_migration_from_9

  docker volume rm -f ${DB_VOLUME}
  docker volume create ${DB_VOLUME}

  docker run --rm -d \
    --name postgres_migration_to_10 \
    -v ${DB_VOLUME}:/var/lib/postgresql/data \
    -v ${PGSQL_DB_BIN}:/usr/lib/postgresql/${IMAGE_VERSION}/bin \
    -v ${migrationVolume}:${migrationPath} \
    -e POSTGRES_USER=${DB_USER} \
    -e POSTGRES_PASSWORD=${DB_PASSWORD} \
    mdillon/postgis:10

  echo "Waiting for postgres to initialize"
  sleep 30
  docker exec -it \
    postgres_migration_to_10 \
    psql -f ${migrationPath}/full.dump

  docker stop postgres_migration_to_10

  __init
}

"$@"

exit $?
