#!/bin/bash

set -ex
NAME=xephem-ubuntu-22.04

if [ ! -f /usr/bin/docker ]
then
    echo 'Error: please "apt install docker.io"' >&2
    exit 2
fi

# Change directory to the directory with this script and its Dockerfile.

cd "$(dirname $0)"

# Build the Docker image from the instructions in the Dockerfile.
# Name the resulting image "xephem".

if [ "$1" = "--no-cache" ]
then
    NO_CACHE=--no-cache
    shift
else
    NO_CACHE=
fi
docker build -t $NAME . 1>&2 $NO_CACHE

# Run the "xephem" image, exposing several files from the host system
# that together should let XEphem run and connect to X Windows:
#
# 1. This XEphem source tree, which gets mounted at "/src".
# 2. The collection of X11 sockets in /tmp.
# 3. Our X session's private authority file.

exec docker run -ti \
     --net=host \
     -e DISPLAY=$DISPLAY \
     -v ${PWD}/../..:/src \
     -v /tmp/.X11-unix:/tmp/.X11-unix \
     -v ${XAUTHORITY}:/root/.Xauthority \
     $NAME \
     "$@"
