#!/usr/bin/env bash
# based on https://gist.github.com/bobmaerten/9329752

Container_name="penultimate-dev-postgres"

Function getcontainerstatus(){
Container_id=$(docker ps -a | grep -v exit | grep $container_name | awk '{print $1}')
If [[ -z $container_id ]] ; then
Echo "not running."
Return 1
Else
Echo "running in container: $container_id"
Return 0
Fi
}

Case "$1" in
Start)
Docker ps -a | grep -v exit | grep -q $container_name
If [ $? -ne 0 ]; then
Docker run --rm -p 65432:5432 -v "/tmp/penultimate-dev-postgres-data:/var/lib/postgresql/data" -e postgres_password=penultimatedeveloper --name $container_name -d postgres:13
Fi
Getcontainerstatus
;;
Stop)
Container_id=$(docker ps -a | grep -v exit | grep $container_name | awk '{print $1}')
If [[ -n $container_id ]] ; then
Srv=$(docker stop $container_id)
If [ $? -eq 0 ] ; then
Echo 'stopped.'
Fi
Fi
;;
*)
Printf "usage: `basename $0` {start|stop} \nstarts (or stops and deletes) a postegres docker container named $container_name on port 65432, which the dev environment is expecting to find.\n\n"
Exit 1
;;
Esac

Exit 0