# ~/.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 'python3 -m venv .venv'
# and then install all required packages with 'python3 -m 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)\$ '

# 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)."
    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)"

if [ -f $notice_file ]; then
  COLOR_GREEN
  echo
  cat $notice_file
  echo
  COLOR_RESET
  echo "Run 'touch ~/.opp_env_upgrade_check_disable' to disable this automatic upgrade check."
fi

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

# daily opp_env version check - execute 'touch ~/.disable_opp_env_upgrade_check' to disable
timestamp=~/.opp_env_upgrade_check_timestamp
if [ ! -f ~/.opp_env_upgrade_check_disable ]; then
  # See if it's expired, and background update
  lastrun=$(stat -c %Y "$timestamp" 2>/dev/null) || lastrun=0
  expiration=$(expr $lastrun + 86400)
  if [ $(date +%s) -ge $expiration ]; then
    (
      if [ "$(pip install --upgrade --dry-run --disable-pip-version-check opp-env 2> /dev/null | grep 'Would install')" ]; then
        echo -e "A new version of 'opp_env' is available.\nRun 'pip install --upgrade opp_env' to upgrade." > $notice_file
      else
        rm -f $notice_file
      fi
    ) &
    disown
    touch $timestamp
  fi
else
  rm -f $notice_file
fi

echo
