#!/bin/sh

set -e

COMMAND="$1"
ARGUMENT="$2"

create_database() {
  docker exec -i medicine-stack-postgres psql -c 'CREATE DATABASE medicine_remd' --username=postgres
}

drop_database() {
  docker exec -i medicine-stack-postgres psql -c 'DROP DATABASE medicine_remd' --username=postgres
}

install() {
  echo "readonly" | docker login docker.med-logic.ru --username readonly --password-stdin
  docker pull docker.med-logic.ru/medicine-remd:latest
  docker-compose run --rm medicine-remd python manage.py post_install
  docker-compose up -d
}

update() {
  echo "readonly" | docker login docker.med-logic.ru --username readonly --password-stdin
  docker pull docker.med-logic.ru/medicine-remd:latest
  docker-compose run --rm medicine-remd python manage.py migrate --noinput
  docker-compose run --rm medicine-remd python manage.py catalog_loaddata
  docker-compose stop
  docker-compose up -d

  docker rmi $(docker images -f "dangling=true" -q)
}


${COMMAND}
