#!/bin/sh

set -eu

# Must be inside a git working tree.
if ! git rev-parse --is-inside-work-tree >/dev/null 2>&1; then
    echo "Current directory is not inside a git working tree" >&2
    exit 6
fi

# Current worktree root.
WT_ROOT=$(git rev-parse --show-toplevel)

# Main repository root.
COMMON_DIR=$(git rev-parse --git-common-dir)
REPO_ROOT=$(cd "$COMMON_DIR/.." && pwd)

# Find devcontainer attached to the main repository.
CID=$(
    docker ps -q \
        --filter "label=devcontainer.local_folder=$REPO_ROOT"
)

if [ -z "$CID" ]; then
    echo "No running devcontainer found for $REPO_ROOT" >&2
    exit 7
fi

# Find repository mount inside the container.
if ! CONTAINER_ROOT=$(
    docker inspect \
        --format '{{range .Mounts}}{{if eq .Source "'"$REPO_ROOT"'"}}{{println .Destination}}{{end}}{{end}}' \
        "$CID"
); then
    echo "Failed to inspect devcontainer $CID" >&2
    exit 8
fi

if [ -z "$CONTAINER_ROOT" ]; then
    echo "Repository is not mounted inside the devcontainer" >&2
    exit 9
fi

# Worktree relative to repository.
if [ "$WT_ROOT" = "$REPO_ROOT" ]; then
    REL_WT=""
else
    REL_WT=${WT_ROOT#"$REPO_ROOT"/}
fi

# Current directory relative to worktree.
if [ "$PWD" = "$WT_ROOT" ]; then
    REL_PWD=""
else
    REL_PWD=${PWD#"$WT_ROOT"/}
fi

WORKDIR=$CONTAINER_ROOT

if [ -n "$REL_WT" ]; then
    WORKDIR="$WORKDIR/$REL_WT"
fi

if [ -n "$REL_PWD" ]; then
    WORKDIR="$WORKDIR/$REL_PWD"
fi

exec docker exec -w "$WORKDIR" "$CID" "$@"
