#! /bin/sed 2,5!d;s/^#.//
# This script must be sourced from within a shell
# and not executed. For instance with:
# 
#   . ~/.commonfunc

#
# {{@@ header() @@}}
#

# eza - set aliasses for eza to use it as ls replacement
if [[ $(command -v eza) ]]; then
  ezafunc() {
    eza -l -F -g -h --git --group-directories-first --icons ${@:-}
  }
  lfunc() {
    ezafunc -T -L ${@:-}
  }
  lafunc() {
    ezafunc -a -T -L ${@:-}
  }
  lefunc() {
    ezafunc -a -T -L ${@:-} --extended
  }
  alias l='lfunc 1'
  alias la='lafunc 1'
  alias le='lefunc 1'
  alias ll='lfunc 2'
  alias lla='lafunc 2'
  alias lle='lefunc 2'
fi

# wttr - show the weather forecast in Terminal
wttr() {
    if [ -z "${1}" ]; then
        curl http://wttr.in
    elif [[ "${1}" == "help" ]]; then
        cat << EOF
usage: wttr (City|3-letter airport code|'~Special+Location')
  City:
    Just write down the name of the city.
    e.G.:
      wttr London
  3-letter airport code:
    Use 3-letter airport codes in order to get the weather information at a certain airport.
    e.G.:
      wttr muc #for Munich Internation Airpot, Germany
  Special Location:
    Let's say you'd like to get the weather for a geographical location other than a town or city -
    maybe an attraction in a city, a mountain name, or some special location.
    Add the character '~' before the name to look up that special location name before the weather is then retrieved.
    e.G.:
      wttr '~Eiffel+Tower'
      wttr '~Kilimanjaro'
EOF
    else
        curl http://wttr.in/${1}
    fi
}

{%@@ if profile == 'WVDEWOBMC001307' or profile == 'susanoo.int.r3w.de' @@%}
macnst (){
  netstat -Watnlv | grep LISTEN | awk '{"ps -o comm= -p " $9 | getline procname;colred="\033[01;31m";colclr="\033[0m"; print colred "proto: " colclr $1 colred " | addr.port: " colclr $4 colred " | pid: " colclr $9 colred " | name: " colclr procname;  }' | column -t -s "|"
}
{%@@ endif @@%}{%@@ if profile == 'WVDEWOBMC001307' @@%}
function setProxyEnv(){
  local HIGHLIGHT='\033[36;1m'
  local RESET='\033[0;0m'

  local PROXY='127.0.0.1:9000'
  local PROXY_CFG_HTTP="${PROXY}"
  local PROXY_CFG_HTTPS="${PROXY}"
  local NO_PROXY_CFG="127.0.0.1,localhost,vw.vwg"
  
  export NO_PROXY="${NO_PROXY_CFG}"
  export no_proxy="${NO_PROXY_CFG}"
  export HTTP_PROXY="${PROXY_CFG_HTTP}"
  export HTTPS_PROXY="${PROXY_CFG_HTTPS}"
  export http_proxy="${PROXY_CFG_HTTP}"
  export https_proxy="${PROXY_CFG_HTTPS}"
  export GIT_SSH_COMMAND='ssh -o ProxyCommand="/opt/homebrew/bin/corkscrew localhost 9000 %h %p"'

  echo -e "\n✈️  exported zscaler proxy: ${HIGHLIGHT}http://${PROXY}${RESET}"
}

function unsetProxyEnv(){
  unset NO_PROXY
  unset HTTP_PROXY
  unset HTTPS_PROXY
  unset no_proxy
  unset http_proxy
  unset https_proxy
  unset GIT_SSH_COMMAND

  echo -e "\n🔄 removed zscaler proxy cofiguration"
}

function setVpnGitRemote(){
  local ORIGIN=vpn #e.g.
  local HIGHLIGHT='\033[36;1m'
  local RESET='\033[0;0m'

  local repo="ssh://git@vpn.github.com:443/${$(git config remote.origin.url)#*git@github.com:}"
  git remote add ${ORIGIN} "${repo}" 2> /dev/null

  echo -e "🔗 git remote (${HIGHLIGHT}${ORIGIN}${RESET}): ${HIGHLIGHT}${repo}${RESET}\n"
}
{%@@ endif @@%}