#!/bin/bash -e

in_toolbox=/run/.containerenv

if [ -f $in_toolbox ]; then
  podman="flatpak-spawn --host env podman"
else
  podman="env podman"
fi

container_exists=$( $podman images -q cockpit-website )

if [[ -n $container_exists ]]; then
  # Use the container with podman; first assign to $run
  # Port 35729 is for the port when Jekyll auto-reloads
  # Port 4000 is the Jekyll server port
  run="$podman run -it -p 4000:4000 -p 35729:35729 -v $PWD:/src/site:Z --replace --name cockpit-website cockpit-website"

  if [ -n "$1" ]; then
    $run "$@"
  else
    $run bundle exec jekyll server -H 0.0.0.0 -l "$*"
  fi
else
  echo "## Container does not exist"
fi
