#!/bin/sh
### CRIMSON --- A simple PHP framework.
### Copyright © 2024 Freya Murphy <contact@freyacat.org>
###
### This file is part of CRIMSON.
###
### CRIMSON is free software; you can redistribute it and/or modify it
### under the terms of the GNU General Public License as published by
### the Free Software Foundation; either version 3 of the License, or (at
### your option) any later version.
###
### CRIMSON is distributed in the hope that it will be useful, but
### WITHOUT ANY WARRANTY; without even the implied warranty of
### MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
### GNU General Public License for more details.
###
### You should have received a copy of the GNU General Public License
### along with CRIMSON. If not, see <http://www.gnu.org/licenses/>.

# `compose`
# This script will provide a docker compose interface with all the CRIMSON
# environment setup. This script may request root privilages since it needs all
# docker containers to run with user 1000:1000. If you are not user 1000:1000,
# root privlage is required to create volume folders with uid/gid 1000.

# Make sure errors fail to avoid nasal demons
set -e

# ========================================================= PERMISSION CHECK ==
# Make sure we are either root or user 1000:1000. This is required for crimson.

args="$@"
command="$0 $args"

uid=$(id -u)
gid=$(id -g)

if [[ $uid -eq 1000 ]] && [[ $gid -eq 1000 ]]; then
	true # all set
elif [[ $uid -eq 0 ]] && [[ $gid -eq 0 ]]; then
	true # all set
else
	# root required (1000:1000 may not exist)
	exec sudo -E $command
fi

# ========================================================= LOAD ENVIRONMENT ==
# We need to load the environment variables provided by crimson and the user
# making the project.
SCRIPT_DIR="$(dirname "$0")"
source "$SCRIPT_DIR/setup_env"

# ================================================================ BOOTSTRAP ==
# Choose which docker compose files are to be loaded, and setup which arguemnts
# to pass into docker compose.

docker_args=""

# add env files to docker args

docker_env_args=$(echo "$ENV_FILES" | tr ':' '\n' | sed 's/^/--env-file /' | tr '\n' ' ')
docker_args="$docker_args $docker_env_args"

# get list of env files for docker containers

docker_env_files="[$(echo "$ENV_FILES" | tr ':' "\n" | sed 's/.*/"&"/' | tr '\n' ',')]"

# add compose files to docker args

function include_docker {
	local name src dest bool
	name="$(basename "$1")"
	src="$1"
	dest="/tmp/crimson/docker/$name"

	bool="$2"
	if [[ "$bool" == "true" ]] && [[ -f "$src" ]]; then

		mkdir -p "$(dirname "$dest")"
		sed "s#DOCKER_ENV_FILES#$docker_env_files#" "$src" > "$dest"

		docker_args="$docker_args -f $dest"
	fi
}

include_docker	"$CRIMSON_ROOT/docker/docker-compose.base.yml"   "true"
include_docker	"$CRIMSON_ROOT/docker/docker-compose.db.yml"     "$POSTGRES_ENABLED"
include_docker	"$CRIMSON_ROOT/docker/docker-compose.api.yml"    "$API_ENABLED"
include_docker	"$PROJECT_ROOT/conf/docker-compose.override.yml" "true"

# set project name

docker_args="$docker_args -p $PROJECT_NAME"

# source check

if [ ! -d "$PROJECT_SOURCE" ]; then
	printf "fatal: cannot find PROJECT_SOURCE: '$PROJECT_SOURCE'\n" > /dev/stderr
	exit 1
fi

# data check

if [ ! -d "$PROJECT_DATA" ]; then
	mkdir -p "$PROJECT_DATA"
	mkdir -p "$PROJECT_DATA/schemas"
	mkdir -p "$PROJECT_DATA/crimson"
	chown -R 1000:1000 "$PROJECT_DATA"
fi

# run docker compose

exec -a docker -- docker compose $docker_args "$@"