#!/bin/sh

# script/setup: Set up container for the first time after cloning

set -e

cd "$(dirname "$0")/.."

script/bootstrap

echo "==> Setting up pod kontor"
if podman pod exists kontor; then
    echo "  => pod kontor exists"
    if podman pod ps --filter "name=kontor" --filter "status=running"; then
        echo "    => pod kontor is running"
    else
        podman pod start kontor
    fi
else
    podman pod create --name kontor \
    --infra \
    -p 5432:5432 \
    -p 8100:8100 \
    -p 8200:8200 \
    -p 8900:8080 \
    --network bridge
fi

echo "==> Setting up volume kontor-db"
if podman volume exists kontor-db; then
    echo "  => volume kontor-db exists"
else
    podman volume create kontor-db
fi

echo "==> Setting up container postgres"
if podman container exists postgres; then
    if podman ps -q --filter "name=postgres"; then
        echo "  => postgres is running"
    fi
else
    podman run -d --replace --pod kontor --name postgres \
        -e POSTGRES_PASSWORD=kontor \
        -e POSTGRES_DB=kontor \
        -e POSTGRES_USER=kontor \
        -v kontor-db:/var/lib/postgresql/data \
        postgres:17
fi
echo "==> Setting up container adminer"
if podman container exists adminer; then
    if podman ps -q --filter "name=adminer"; then
        echo "  => adminer is running"
    fi
else
    podman run -d --replace --pod kontor --name adminer adminer
fi

