#!/bin/bash

# extract the different element of an url into a JSON structure
parse_url() {
  # extract the protocol
  proto="$(echo $1 | cut -f1 -d: )"
  if [[ ! -z $proto ]] ; then
    # remove the protocol
    url="$(echo ${1/"$proto://"/})"
    # extract the user (if any)
    login="$(echo $url | grep @ | cut -d@ -f1)"
    username="$(echo $login | cut -d: -f1)"
    password="$(echo $login | cut -d: -f2)"
    # extract the host
    host_port="$(echo ${url/$login@/} | cut -d/ -f1) "
    host="$(echo $host_port | cut -f1 -d:) "

    # by request - try to extract the port
    port="$(echo $host_port | sed -e 's,^.*:,:,g' -e 's,.*:\([0-9]*\).*,\1,g' -e 's,[^0-9],,g')"
    # extract the uri (if any)
    resource="/$(echo $url | grep / | cut -d/ -f2-)"
  fi
  echo -n "{ \"uri\": \"$1\" , \"url\": \"$url\" , \"proto\": \"$proto\" , \"login\": \"$login\" ,"
  echo  " \"username\": \"$username\" , \"password\": \"$password\" , \"host\": \"$host\" , \"port\": \"$port\" }"
}

# converts .google.fr,.google.com into *.google.fr|*.google.com
convert_no_proxy() {
  local urls
  urls=$1
  echo $urls | tr , \| | sed 's/\./\*\./' | sed 's/|\./,\*\./'

}

# Generate maven XML structure for proxy configuration
get_xml_proxy_configuration() {
  local proto json
  proto=$1
  json=$2
  config="<proxy>\
         <id>genhttpproxy</id>\
         <active>true</active>\
         <protocol>$proto</protocol>\
         <host>$( echo $json | jq -r .host)</host>\
         <port>$( echo $json | jq -r .port)</port>"
  username=$( echo $json | jq -r .username)
  password=$( echo $json | jq -r .password)
  if [ -n "$username" -a -n "$password" ]; then
    config="$config\
      <username>$username</username>\
      <password>$password</password>"
  fi
  if [ -n "$no_proxy" ]; then
    config="$config\
       <nonProxyHosts>$(convert_no_proxy $no_proxy)</nonProxyHosts>"
  fi
  config="$config\
     </proxy>"
  echo "$config"
}

xml=""
if [ -n "$http_proxy" ]; then
 json=$( parse_url $http_proxy )
 proxy=$(get_xml_proxy_configuration http "$json")
 xml="$xml $proxy"
fi


if [ -n "$https_proxy" ]; then
 json=$( parse_url $https_proxy )
 proxy=$(get_xml_proxy_configuration https "$json")
 xml="$xml $proxy"
fi
if [ -n "$xml" ]; then
  sed -i "s&<!-- ### configured http proxy ### -->&$xml&" $HOME/.m2/settings.xml
fi


if [ -n "$MAVEN_MIRROR_URL" ]; then
  xml="    <mirror>\
    <id>mirror.default</id>\
    <url>$MAVEN_MIRROR_URL</url>\
    <mirrorOf>external:*</mirrorOf>\
  </mirror>"
  sed -i "s&<!-- ### configured mirrors ### -->&$xml&" $HOME/.m2/settings.xml
fi

function linux_available_memory_bytes() {
  local kbytes
  kbytes=$(cat /proc/meminfo | grep MemAvailable | awk '{print $2}')
  echo $((kbytes*(2**10)))
}

function container_max_memory_bytes() {
  local limit
  if [ -f /sys/fs/cgroup/memory/memory.limit_in_bytes ]; then
    # cgroups v1
    cat /sys/fs/cgroup/memory/memory.limit_in_bytes
    return
  fi
  if [ -f /sys/fs/cgroup/memory.max ]; then
    # cgroups v2
    limit=$(cat /sys/fs/cgroup/memory.max)
    if [ "${limit}" == 'max' ]; then
      linux_available_memory_bytes
      return
    fi
    echo $limit
    return
  fi
  linux_available_memory_bytes
}

CONTAINER_MEMORY_IN_BYTES=$(container_max_memory_bytes)
CONTAINER_MEMORY_IN_MB=$((CONTAINER_MEMORY_IN_BYTES/2**20))

