#!/bin/bash

repoName='qminer/mrfrys-node-server'

function getContainerId() {
  docker ps -a | grep -i "${repoName}" | xargs | cut -d' ' -f1
}

function getDockerRepoId() {
  docker images | grep "${repoName}" | xargs | cut -d' ' -f3
}

if [ "$1" == "build" ]; then
  docker build . -t qminer/mrfrys-node-server
elif [ "$1" == "rebuild" ]; then
  docker rmi -f $(getDockerRepoId)
  docker build . -t qminer/mrfrys-node-server
elif [ "$1" == "build" ]; then
  docker build . -t qminer/mrfrys-node-server
elif [ "$1" == "stop" ]; then
  docker stop $(getContainerId)
elif [ "$1" == "start" ]; then
  # --entrypoint bash \
  scripts/setup.sh
  docker start $(getContainerId)
elif [ "$1" == "run" ]; then
  # --entrypoint bash \
  scripts/setup.sh

  docker run \
    --network host \
    --mount src="$(pwd)/stats",target="/server/stats",type=bind \
    --mount src="$(pwd)/data",target="/server/data",type=bind \
    --mount src="$(pwd)/publicDirs",target="/server/publicDirs",type=bind \
    --mount src="/etc/letsencrypt",target="/etc/letsencrypt",type=bind \
    qminer/mrfrys-node-server
elif [ "$1" == "bash" ]; then
  docker exec -it $(getContainerId) /bin/bash
else
  echo 'params: "build" or "run"'
fi
