#!/usr/bin/env bash
export PANAMA_PATH="$HOME/.local/share/Panama"
export PANAMA_BASH="$PANAMA_PATH/config/bash"

[ -f /etc/bashrc ] && . /etc/bashrc

if [ -d ~/.bashrc.d ]; then
    for rc in ~/.bashrc.d/*; do
        if [ -f "$rc" ]; then
            . "$rc"
        fi
    done
  unset rc
fi

if [ -d "$PANAMA_BASH" ]; then
  for f in "$PANAMA_BASH"/*; do
    [ -f "$f" ] && . "$f"
  done
  unset f

  # Personal environment -- API keys, tokens -- lives OUTSIDE the checkout,
  # where agents, backup tools and `panama update` walk, and is kept
  # owner-only every time it is read: a secrets file that drifts to 644 is
  # quietly re-tightened rather than trusted. (config/bash/env, its old home
  # inside the repo, is still sourced by the glob above if a machine has not
  # been migrated yet.)
  PANAMA_ENV="${XDG_CONFIG_HOME:-$HOME/.config}/panama/env"
  if [ -f "$PANAMA_ENV" ]; then
    [ "$(stat -c %a "$PANAMA_ENV" 2>/dev/null)" = "600" ] || chmod 600 "$PANAMA_ENV"
    . "$PANAMA_ENV"
  fi
  unset PANAMA_ENV
else
  if ! [[ "$PATH" =~ "$HOME/.local/bin:$HOME/bin:" ]]; then
    PATH="$HOME/.local/bin:$HOME/bin:$PATH"
  fi
  export PATH
fi
# rustup writes this file, and initial-packages installs rustup rather than
# running rustup-init -- so on a fresh machine it does not exist yet and an
# unguarded source made every single shell start with an error.
[ -f "$HOME/.cargo/env" ] && . "$HOME/.cargo/env"
