# ~/.profile: executed by the command interpreter for login shells.
# This file is not read by bash(1), if ~/.bash_profile or ~/.bash_login
# exists.
# see /usr/share/doc/bash/examples/startup-files for examples.
# the files are located in the bash-doc package.

# the default umask is set in /etc/profile; for setting the umask
# for ssh logins, install and configure the libpam-umask package.
#umask 022

# Activate local python virtual environment if present in the root directory.
# You can create a virtual environment with 'uv venv'
# and then install all required packages with 'uv pip install -r python/requirements.txt'
#
# NOTE: the bin/activate script directly sets the PATH variable to a value saved during
# an earlier run of the script to restore the PATH to that state. This may overwrite any
# changes made to the PATH since the last run. Because of this it is EXTREMELY important
# NOT to modify the PATH in the 'setenv' script before activating the virtual env.
# In short, this activation MUST BE at the very beginning of the 'setenv' script.
#
if [ -f "$HOME/.venv/bin/activate" ] ; then
  source $HOME/.venv/bin/activate
fi

# if running bash
if [ -n "$BASH_VERSION" ]; then
    # include .bashrc if it exists
    if [ -f "$HOME/.bashrc" ]; then
        . "$HOME/.bashrc"
    fi
fi

# set PATH so it includes user's private bin if it exists
if [ -d "$HOME/bin" ] ; then
    PATH="$HOME/bin:$PATH"
fi

# set PATH so it includes user's private bin if it exists
if [ -d "$HOME/.local/bin" ] ; then
    PATH="$HOME/.local/bin:$PATH"
fi

if [ -e ~/.nix-profile/etc/profile.d/nix.sh ]; then . ~/.nix-profile/etc/profile.d/nix.sh; fi # NIX packages are required by the following part

COLOR_GRAY() {
  echo -e "\033[38;20m" 
}
COLOR_RED() {
  echo -e "\033[1;31m"
}
COLOR_YELLOW() {
  echo -e "\033[1;33m"
}
COLOR_CYAN() {
  echo -e "\033[0;36m"
}
COLOR_GREEN() {
  echo -e "\033[0;32m"
}
COLOR_RESET() {
  echo -e "\033[0;0m"
}

notice_file=~/.opp_env_upgrade_notice
# override prompt to display opp_env version if opp_env is accessible
opp_env_version() {
  if [ "$(command -v opp_env)" ]; then
    echo "opp_env@"$(opp_env --version)
  else
    echo "opp_env@???"
  fi
}

opp_env_upgrade() {
  if [ -f $notice_file ]; then
    echo "!"
  fi
}

PS1='${debian_chroot:+($debian_chroot)}$(COLOR_GREEN)$(opp_env_version)$(COLOR_RED)$(opp_env_upgrade)$(COLOR_RESET):$(COLOR_CYAN)\w$(COLOR_RESET)\$ '

# Upgrade opp_env at most once a day. Only in interactive shells, so that 'docker run' and
# 'wsl -- <command>' keep running the same version. Not in the background, so that the shell
# being started already uses the new version.
# 'touch ~/.opp_env_auto_upgrade_disable' only shows a notice about new versions instead,
# 'touch ~/.opp_env_upgrade_check_disable' disables the check altogether.
upgrade_message=
timestamp=~/.opp_env_upgrade_check_timestamp
if [ -f ~/.opp_env_upgrade_check_disable ]; then
  rm -f $notice_file
elif [[ $- == *i* ]] && [ "$(command -v uv)" ]; then
  lastrun=$(stat -c %Y "$timestamp" 2>/dev/null) || lastrun=0
  if [ "$(date +%s)" -ge $((lastrun + 86400)) ]; then
    touch $timestamp
    if [ -f ~/.opp_env_auto_upgrade_disable ]; then
      (
        if uv pip install --upgrade --dry-run opp-env 2>&1 | grep -q '^ + opp-env=='; then
          echo -e "A new version of 'opp_env' is available.\nRun 'uv pip install --upgrade opp_env' to upgrade." > $notice_file
        else
          rm -f $notice_file
        fi
      ) &
      disown
    else
      echo "Checking for opp_env upgrades..."
      if dry_run=$(timeout 10 uv pip install --upgrade --dry-run opp-env 2>&1); then
        rm -f $notice_file
        new_version=$(echo "$dry_run" | sed -n 's/^ + opp-env==//p')
        old_version=$(echo "$dry_run" | sed -n 's/^ - opp-env==//p')
        if [ "$new_version" ]; then
          # a longer timeout than for the check, because killing the install halfway could leave the venv without opp_env
          if timeout 120 uv pip install --quiet "opp-env==$new_version"; then
            upgrade_message="$(COLOR_GREEN)opp_env has been upgraded from ${old_version:-???} to $new_version.$(COLOR_RESET)\nRun 'touch ~/.opp_env_auto_upgrade_disable' to disable automatic upgrades."
          else
            upgrade_message="$(COLOR_RED)Upgrading opp_env to $new_version failed.$(COLOR_RESET) Run 'uv pip install --upgrade opp_env' to retry."
          fi
        fi
      else
        upgrade_message="$(COLOR_YELLOW)Could not check for opp_env upgrades (offline?). Will retry tomorrow.$(COLOR_RESET)"
      fi
      unset dry_run new_version old_version
    fi
  fi
fi

# printed after the banners, because they clear the screen
show_upgrade_info() {
  if [ "$upgrade_message" ]; then
    echo
    echo -e "$upgrade_message"
    upgrade_message=
  fi
  if [ -f $notice_file ]; then
    echo
    COLOR_GREEN
    cat $notice_file
    COLOR_RESET
    echo "Run 'rm ~/.opp_env_auto_upgrade_disable' to enable automatic upgrades again, or 'touch ~/.opp_env_upgrade_check_disable' to disable this check."
  fi
}

# if there is already an installation of OMNeT++ already in the workspace
# invoke an 'opp_env shell' on that to drop the user directly into the development shell
# which is the inteded action in almost all cases. In that case we drop back to the main shell
# where opp_env can be used only after the user exits the deveelopment shell

# if the content of the .opp_env_last_workspace file points to a directory, 
# put the directory name into the OPP_ENV_LAST_WORKSPACE variable
export OPP_ENV_LAST_WORKSPACE=~/default_workspace
if [[ -f ~/.opp_env_last_workspace && -d "$(cat ~/.opp_env_last_workspace)" ]]; then
  OPP_ENV_LAST_WORKSPACE=$(cat ~/.opp_env_last_workspace)
fi

if [ -d "$OPP_ENV_LAST_WORKSPACE" ]; then
  cd $OPP_ENV_LAST_WORKSPACE
  # invoke opp_env shell in the last used workspace if some version of omnetpp is already installed
  shopt -s nullglob
  _omnetpp_installs=(omnetpp-*)
  shopt -u nullglob
  if [ "$_omnetpp_installs" ]; then
    clear
    echo "OMNeT++ Development Environment - $(COLOR_GREEN)opp_env shell$(COLOR_RESET) for $(COLOR_CYAN)'$OPP_ENV_LAST_WORKSPACE'$(COLOR_RESET)."
    show_upgrade_info
    echo
    echo "You can leave the development shell and drop back to the installation environment by typing 'exit' or CTRL-d."
    echo
    opp_env shell --chdir
  fi
  unset _omnetpp_installs
else
  cd
fi

# opp_env main shell intro where 'opp_env can be used'
clear
echo "OMNeT++ Installation Environment - $(COLOR_GREEN)$(opp_env_version)$(COLOR_RED)$(opp_env_upgrade)$(COLOR_RESET)"
show_upgrade_info

if [ ! -f ~/.opp_env_intro_disable -a "$(command -v opp_env)" ]; then
  echo
  opp_env --help-intro
  echo
  echo "Run 'touch ~/.opp_env_intro_disable' to remove this intro."
fi

echo
