#@IgnoreInspection BashAddShebang
# This file must be used with "source bin/activate" *from bash or zsh*
# you cannot run it directly

# As this script is sourced, the functions will be available and executed in the underlying shell.
# because parameter expansion works slightly differently between bash and zsh, we need to ensure
# that this works under either shell

if [[ -z "$BASH" && -z "$ZSH_NAME" && -z "$DOCKERUTILS_DISABLE_PROMPT" ]]; then
  # If we are going to modify the prompt, then we need to be running
  # either bash or zsh
  echo ""
  echo "You mush either be running bash or zsh"
  ehco ""
  kill -INT $$
fi

MONIKER=${1:-none}

if [ -n "$_DOCK_MONIKER" ]; then
  echo "Remote docker is already configured for '$DOCK_MONIKER'. Try 'castoff' to disconnect."
  return 0
fi

if [ $MONIKER = "none" ]; then
  echo "This script is used to setup a dock with a remote instance"
  echo
  echo "USAGE"
  echo "    $ source dock <ip or hostname configured in /etc/hosts>"
  echo "Examples:"
  echo "    $ dock my-centos1"
  echo "    $ dock 10.93.133.2"
  echo
  echo "You can now run secure remote docker commands."
  echo "To undo this configuration:"
  echo "    $ castoff"
  echo
  echo "If you do not want your command line prompt to be updated, set DOCKERUTILS_DISABLE_PROMPT=true"
  kill -INT $$
fi

# Look up IP from moniker
FOUND_MONIKER=false
for f in $HOME/.docker/*; do
    if [ -d $f ] && [ -f $f/connection_config.txt ]; then
      while read -r line; do declare $line; done < "$f/connection_config.txt"
      if [ $DOCK_MONIKER = $MONIKER ] || [ $DOCK_IP = $MONIKER ]; then
        FOUND_MONIKER=true
        break
      fi
    fi
done

if [ $FOUND_MONIKER = false ]; then
  echo "Can't find dock configuration for $MONIKER"
  kill -INT $$
fi

echo "Docking to $DOCK_USER@$DOCK_IP [Moniker: $DOCK_MONIKER]"

if [ -z "$DOCK_IP" ]; then
  kill -INT $$
fi


# Remove docked condition including command line prompt.
# Python virtual environment prompt (or lack therof) should remain unchanged.
castoff() {
    _DOCK_MONIKER="\[dock:$DOCK_MONIKER\] "
    unset DOCKER_HOST
    unset DOCKER_IP

    if [ -n "$BASH" ]; then
        if [ -n "$_OLD_VIRTUAL_PS1" ]; then
            _OLD_VIRTUAL_PS1=${_OLD_VIRTUAL_PS1//${_DOCK_MONIKER}/}
            export _OLD_VIRTUAL_PS1
        fi
        PS1=${PS1//${_DOCK_MONIKER}/}
    elif [ -n "$ZSH_NAME" ]; then
        if [ -n "$_OLD_VIRTUAL_PS1" ]; then
            _OLD_VIRTUAL_PS1=${_OLD_VIRTUAL_PS1//${~_DOCK_MONIKER}/}
            export _OLD_VIRTUAL_PS1
        fi
        PS1=${PS1//${~_DOCK_MONIKER}/}
    fi

    export PS1

    unset _DOCK_MONIKER
    unset -f castoff
    unset -f sync-up
    unset -f sync-down
    unset -f do-sync-up
    unset -f do-sync-down
    unset -f trim
    unset -f get_version_file
    echo "Castoff complete"
}

. dock-sync

export DOCKER_HOST=ssh://${DOCK_USER}@${DOCK_IP}
export DOCKER_IP=${DOCK_IP}
export _DOCK_MONIKER="[dock:$DOCK_MONIKER] "

if [ -z $DOCKERUTILS_DISABLE_PROMPT ]; then
    # Update command line prompt to reflect docked condition.
    # Python virtual environment prompt (or lack therof) should remain unchanged.

    if [ -n "$_OLD_VIRTUAL_PS1" ]; then
        _OLD_VIRTUAL_PS1="$_OLD_VIRTUAL_PS1${_DOCK_MONIKER}"
        export _OLD_VIRTUAL_PS1
    fi

    PS1=$PS1${_DOCK_MONIKER}

    export PS1
fi