#!/usr/bin/env bash
# Copyright (c) 2025 b-data GmbH
# Copyright (c) 2023 Microsoft Corporation
# Distributed under the terms of the MIT License.
#
# https://docs.github.com/en/authentication/connecting-to-github-with-ssh/working-with-ssh-key-passphrases?platform=windows
#
# Modified by b-data for seamless integration with development containers on Linux/macOS.

set -e

# Do not execute on GitHub Codespaces
if [ -z "$CODESPACES" ]; then
  # Check the state of the currently running instance of the agent
  # 0: running with key(s); 1: running w/o key(s); 2: not running
  AGENT_RUN_STATE="$(ssh-add -l > /dev/null 2>&1; echo $?)"
  # Only add key(s) if running w/o key(s)
  if [ "$AGENT_RUN_STATE" = "1" ]; then
    ssh-add 2> /dev/null || true
  fi
  # Means: Not adding key(s) to a forwarded agent with key(s)
fi

# https://code.visualstudio.com/remote/advancedcontainers/sharing-git-credentials
#
# On Linux: For an X session, the agent is usually started automatically.
#           For a login session, add the following to your `~/.bash_profile`
#           or `~/.zprofile`:
# 
# if [ -z "$SSH_AUTH_SOCK" ]; then
#   # Check for a currently running instance of the agent
#   RUNNING_AGENT="$(pgrep -f 'ssh-agent -s' -u "$USER" | wc -l | tr -d '[:space:]')"
#   if [ "$RUNNING_AGENT" = "0" ]; then
#     mkdir -p -m 0700 "$HOME/.ssh"
#     # Launch a new instance of the agent
#     ssh-agent -s &> "$HOME/.ssh/ssh-agent"
#   fi
#   eval "$(cat "$HOME/.ssh/ssh-agent")" > /dev/null
# fi
