#!/bin/bash
# Info: Script to install proxy
# Author: JesusPerezLorenzo  
# Release: 1.0 
# Date: 12-12-2023

USAGE="install-proxy.sh "
[ "$1" == "-h"  ] && echo "$USAGE"  && exit 1

[ -r "global.sh" ] && . ./global.sh
[ -r "env-proxy" ] && . ./env-proxy

VERSION=${PROXY_VERSION:-2.9}

CMD_TSKSRVC=${1:-install}

PROXY_RUN_USER=${PROXY_RUN_USER:-haproxy}
PROXY_RUN_GROUP=${PROXY_RUN_GROUP:-haproxy}
PROXY_RUN_USER_HOME="${PROXY_RUN_USER_HOME:-/home/haproxy}" 

export LC_CTYPE=C.UTF-8 
export LANG=C.UTF-8

_init() {
  [ -z "$VERSION" ] && exit 1
  curr_vers=$(haproxy -v 2>/dev/null | grep HA-Proxy | cut -f3 -d" " | cut -f1-2 -d".")
  [ "$curr_vers" == "$VERSION" ] && return
   curl -s https://haproxy.debian.net/bernat.debian.org.gpg \
      | sudo gpg --dearmor | sudo tee /usr/share/keyrings/haproxy.debian.net.gpg >/dev/null
  sudo echo deb "[signed-by=/usr/share/keyrings/haproxy.debian.net.gpg]" \
      http://haproxy.debian.net bookworm-backports-${VERSION} main \
  #     > /etc/apt/sources.list.d/haproxy.list
  #sudo add-apt-repository -y ppa:vbernat/haproxy-${VERSION}
  #local codename=$(grep VERSION_CODENAME /etc/os-release | cut -f2 -d"=" ) 
  #if [ "$codename"  == "bookworm" ] ; then 
  #  su -c 'echo "APT::Get::Update::SourceListWarnings::NonFreeFirmware \"false\";" > /etc/apt/apt.conf.d/no-bookworm-firmware.conf'
  #fi
  # Create the file repository configuration:
  # https://www.debian.org/releases/bookworm/amd64/release-notes/ch-information.html#non-free-split
  sudo DEBIAN_FRONTEND=noninteractive apt-get update 
  # sudo DEBIAN_FRONTEND=noninteractive apt-get upgrade -y
  #sudo DEBIAN_FRONTEND=noninteractive apt install -y haproxy=${VERSION}.\\* >/dev/null 2>&1 
  sudo DEBIAN_FRONTEND=noninteractive apt install -y haproxy >/dev/null 2>&1 
}

_config_proxy() {
  # started via /etc/rc2.d/S01haproxy
  # if not user/group haproxy created
  local has_user=""
  has_user=$(grep "$PROXY_RUN_USER" /etc/passwd)
  if [ -z "$has_user" ] ; then 
	  sudo adduser \
			--system \
			--shell /bin/bash \
			--gecos 'Haproxy' \
			--group \
			--disabled-password \
			--home /home/haproxy \
			"${PROXY_RUN_USER}"
  fi
  if [ ! -d "$PROXY_RUN_USER_HOME" ] ; then
    sudo mkdir -p "$PROXY_RUN_USER_HOME"
		sudo chown -R "$PROXY_RUN_USER":"$PROXY_RUN_GROUP" "$PROXY_RUN_USER_HOME"
	fi
  [ -d "errors" ] && sudo cp -pr errors ${PROXY_ETC_PATH} && sudo chown "${PROXY_RUN_USER}:${PROXY_RUN_GROUP}" "${PROXY_ETC_PATH}"/errors
  [ -r "haproxy.cfg" ] && sudo cp haproxy.cfg "$PROXY_ETC_PATH/$PROXY_CONFIG_FILE" && sudo chown "${PROXY_RUN_USER}:${PROXY_RUN_GROUP}" "$PROXY_ETC_PATH/$PROXY_CONFIG_FILE"
}

_stop_proxy() {
  sudo timeout -k 10 20 systemctl stop haproxy >/dev/null 2>&1
  sudo timeout -k 10 20 systemctl disable haproxy >/dev/null 2>&1
}

_remove_proxy() {
  sudo timeout -k 10 20 systemctl stop haproxy  >/dev/null 2>&1
  sudo timeout -k 10 20 systemctl disable haproxy >/dev/null 2>&1
  sudo apt remove -y haproxy
}

_start_proxy() {
  sudo timeout -k 10 20 systemctl enable haproxy >/dev/null 2>&1
  sudo timeout -k 10 20 systemctl restart haproxy >/dev/null 2>&1
}

_restart_proxy() {
  sudo timeout -k 10 20 systemctl restart haproxy.service >/dev/null 2>&1
  sudo timeout -k 10 20 systemctl status haproxy.service >/dev/null 2>&1
}

if [ "$CMD_TSKSRVC" == "remove" ] ; then
  _remove_proxy
  exit
fi
if ! _init ; then
  echo "error proxy init"
  exit 1
fi
[ "$CMD_TSKSRVC"  == "update" ] &&  _restart_proxy && exit 0
if ! _config_proxy ; then 
  echo "error proxy config"
  exit 1
fi
if ! _start_proxy ; then 
  echo "error proxy start"
  exit 1
fi
exit 0 
