#!/bin/bash

NOMECONTAINER="reverseproxy"

docker_repo="urbancompasspony/reverseproxy:latest"
imagem="reverse-proxy"

CustmN2="local_ip"
CustmN3="servidor_dns"
CustmN4=""
CustmN5=""
CustmN6=""
CustmN7=""
CustmN8=""
CustmN9=""
CustmN10=""

VALUE2="0.0.0.0"
VALUE3="8.8.4.4"
VALUE4=""
VALUE5=""
VALUE6=""
VALUE7=""
VALUE8=""
VALUE9=""
VALUE10=""

export NOMECONTAINER docker_repo imagem
export CustmN2 CustmN3 CustmN4 CustmN5 CustmN6 CustmN7 CustmN8 CustmN9 CustmN10
export VALUE2 VALUE3 VALUE4 VALUE5 VALUE6 VALUE7 VALUE8 VALUE9 VALUE10

source /root/.index.sh

if [ -f /tmp/common-functions.sh ]; then
  source /tmp/common-functions.sh
else
  echo "ERRO: common-functions.sh não encontrado!"
  exit 1
fi

function set_mkdir {
  sudo mkdir -p /srv/containers/"$NOMECONTAINER"/{apache2,certbot,cache}
  sudo mkdir -p /srv/containers/"$NOMECONTAINER"/apache2/{sites-available,sites-enabled}
    
  if ! [ -f "/srv/containers/"$NOMECONTAINER"/apache2/apache2.conf" ]; then
    curl -sSL --connect-timeout 5 --max-time 0 --retry 3 --retry-delay 2 $link071 | sudo tee /srv/containers/"$NOMECONTAINER"/apache2/apache2.conf
  fi

  if ! [ -f "/srv/containers/"$NOMECONTAINER"/apache2/mpm_event.conf" ]; then
    curl -sSL --connect-timeout 5 --max-time 0 --retry 3 --retry-delay 2 $link072 | sudo tee /srv/containers/"$NOMECONTAINER"/apache2/mpm_event.conf
  fi

  if ! [ -f "/srv/containers/"$NOMECONTAINER"/apache2/mpm_worker.conf" ]; then
    curl -sSL --connect-timeout 5 --max-time 0 --retry 3 --retry-delay 2 $link073 | sudo tee /srv/containers/"$NOMECONTAINER"/apache2/mpm_worker.conf
  fi
  
  # Upgrade Model!
  curl -sSL --connect-timeout 5 --max-time 0 --retry 3 --retry-delay 2 $link082 | sudo tee /srv/containers/"$NOMECONTAINER"/apache2/sites-available/my-site-model.conf
:; }

function docker_create {
  local ip_regex="^([0-9]{1,3}\.){3}[0-9]{1,3}$"

  # Se NAO for numerico, execute como host!
  if [[ ! "$VALUE2" =~ $ip_regex ]]; then
  
    docker run -d --name="$NOMECONTAINER" --hostname="$NOMECONTAINER" \
    --network host -p 80:80 -p 443:443 \
    --no-healthcheck --restart=unless-stopped -v /etc/localtime:/etc/localtime:ro \
    --log-driver json-file --log-opt max-size=10m --log-opt max-file=3 \
    -v /srv/containers/"$NOMECONTAINER"/apache2/mpm_event.conf:/etc/apache2/mods-available/mpm_event.conf \
    -v /srv/containers/"$NOMECONTAINER"/apache2/mpm_worker.conf:/etc/apache2/mods-available/mpm_worker.conf \
    -v /srv/containers/"$NOMECONTAINER"/apache2/apache2.conf:/etc/apache2/apache2.conf \
    -v /srv/containers/"$NOMECONTAINER"/apache2/sites-available:/etc/apache2/sites-available \
    -v /srv/containers/"$NOMECONTAINER"/apache2/sites-enabled:/etc/apache2/sites-enabled \
    -v /srv/containers/"$NOMECONTAINER"/certbot:/etc/letsencrypt \
    -v /srv/containers/"$NOMECONTAINER"/cache:/var/cache/apache2/mod_cache_disk/routing \
    "$docker_repo"

    return
  fi

  docker run -d --name="$NOMECONTAINER" --hostname="$NOMECONTAINER" \
  --network macvlan --ip="$VALUE2" --dns="$VALUE3" \
  --no-healthcheck --restart=unless-stopped -v /etc/localtime:/etc/localtime:ro \
  --log-driver json-file --log-opt max-size=10m --log-opt max-file=3 \
  -v /srv/containers/"$NOMECONTAINER"/apache2/mpm_event.conf:/etc/apache2/mods-available/mpm_event.conf \
  -v /srv/containers/"$NOMECONTAINER"/apache2/mpm_worker.conf:/etc/apache2/mods-available/mpm_worker.conf \
  -v /srv/containers/"$NOMECONTAINER"/apache2/apache2.conf:/etc/apache2/apache2.conf \
  -v /srv/containers/"$NOMECONTAINER"/apache2/sites-available:/etc/apache2/sites-available \
  -v /srv/containers/"$NOMECONTAINER"/apache2/sites-enabled:/etc/apache2/sites-enabled \
  -v /srv/containers/"$NOMECONTAINER"/certbot:/etc/letsencrypt \
  -v /srv/containers/"$NOMECONTAINER"/cache:/var/cache/apache2/mod_cache_disk/routing \
  "$docker_repo"
}

function docker_extras {

:; }

execute_main

exit 0
