#!/bin/sh -e
# Allow geoserver container write perm on its data dir.
#
# Geoserver container runs with hardcoded uid 1000 and gid 10001.
#
# Set env var FIRST_RUN_ONLY to only execute this script once, before first
# startup of Geoserver.  If Geoserver has already run at least once, file
# global.xml will exist and this script will not execute.  Without
# FIRST_RUN_ONLY, this script will always execute.

THIS_FILE="`realpath "$0"`"
THIS_DIR="`dirname "$THIS_FILE"`"

# Go to repo root.
cd $THIS_DIR/../..

. birdhouse/read-configs.include.sh

# Get BASH_IMAGE
# Get GEOSERVER_DATA_DIR
read_configs

set -x

DATA_DIR="$GEOSERVER_DATA_DIR"

if [ -n "$1" ]; then
    DATA_DIR="$1"; shift
fi

docker run --rm --name fix-geoserver-data-dir-perm \
    --volume ${DATA_DIR}:/datadir \
    --env FIRST_RUN_ONLY \
    $BASH_IMAGE \
    bash -xc 'if [ -z "$FIRST_RUN_ONLY" -o ! -f /datadir/global.xml ]; \
    then chown -R 1000:10001 /datadir; else echo "No execute."; fi'
