#!/bin/bash
# EZWeb 0.4.2: download, verify and start the interactive installer.
#   curl -fsSLo ezweb-install.sh 'https://licente.ezweb.ro/downloads/install.sh' && sudo bash ezweb-install.sh
set -euo pipefail
EZWEB_VERSION='0.4.2'
ARCHIVE='ezweb-installer-0.4.2.tar.gz'
URL='https://licente.ezweb.ro/downloads/ezweb-installer-0.4.2.tar.gz'
SHA256='530ecaa1da5eca4fb62bf93c07b9c49984f5be39636eb43343bb40bad2f60226'
ISSUER_KEY='c44c28694abc2265fd8eb2da3aecef2ae3eb574181ecc25e4f114d5f5a06237f'
WORK="/root/ezweb-download-$EZWEB_VERSION"

fail() { printf '\n%s\n' "$1" >&2; exit 1; }
[ "$(id -u)" -eq 0 ] || fail 'Rulează cu sudo: sudo bash ezweb-install.sh'
platform=$(. /etc/os-release && printf '%s %s' "${ID:-}" "${VERSION_ID:-}")
[ "$platform" = 'ubuntu 24.04' ] && [ "$(uname -m)" = x86_64 ] \
  || fail 'EZWeb se instalează pe Ubuntu Server 24.04 x86_64. Reinstalează serverul din panoul furnizorului.'
command -v python3 >/dev/null && command -v curl >/dev/null \
  || fail 'Lipsesc python3 sau curl. Instalează-le cu: sudo apt-get install -y python3 curl'

# Keep the installation running when SSH drops: restart this script inside tmux.
SELF=$(readlink -f "$0")
if [ -z "${TMUX:-}" ] && [ -z "${EZWEB_NO_TMUX:-}" ] && command -v tmux >/dev/null \
    && [ -f "$SELF" ] && [ "$(head -n 1 "$SELF")" = '#!/bin/bash' ]; then
  if tmux has-session -t ezweb 2>/dev/null; then
    printf 'Installerul rulează deja. Revii în el cu:\n  sudo tmux attach -t ezweb\n'
    exit 0
  fi
  printf 'Pornesc installerul într-o sesiune tmux. Dacă se rupe conexiunea, revii cu:\n  sudo tmux attach -t ezweb\n'
  sleep 3
  exec tmux new-session -s ezweb "bash '$SELF'; printf '\\nApasă Enter pentru a închide.'; read -r _"
fi

# Below 3 GiB of RAM, add swap so the image builds during installation have headroom;
# 1 GiB on small disks, where every GiB counts for the installation itself.
mem_kib=$(awk '/^MemTotal:/ {print $2}' /proc/meminfo)
if [ "$mem_kib" -lt 3145728 ] && [ -z "$(swapon --show --noheadings)" ] && [ ! -e /swapfile ]; then
  swap_mib=2048
  [ "$(df --output=avail -k / | tail -n 1)" -lt 26214400 ] && swap_mib=1024
  printf 'Serverul are mai puțin de 3 GB RAM: adaug %s MB de swap în /swapfile.\n' "$swap_mib"
  fallocate -l "${swap_mib}M" /swapfile 2>/dev/null || dd if=/dev/zero of=/swapfile bs=1M count="$swap_mib" status=none
  chmod 600 /swapfile
  mkswap /swapfile >/dev/null
  swapon /swapfile
  grep -q '^/swapfile ' /etc/fstab || printf '/swapfile none swap sw 0 0\n' >> /etc/fstab
fi

umask 077
install -d -m 700 "$WORK"
cd "$WORK"
if ! printf '%s  %s\n' "$SHA256" "$ARCHIVE" | sha256sum --check --status - 2>/dev/null; then
  printf 'Descarc EZWeb %s…\n' "$EZWEB_VERSION"
  rm -f "$ARCHIVE.part"
  curl --fail --location --retry 3 --progress-bar --output "$ARCHIVE.part" "$URL" \
    || fail 'Descărcarea a eșuat. Verifică conexiunea serverului și rulează din nou comanda.'
  printf '%s  %s\n' "$SHA256" "$ARCHIVE.part" | sha256sum --check --status - \
    || { rm -f "$ARCHIVE.part"; fail 'Arhiva descărcată nu corespunde. Rulează din nou comanda.'; }
  mv "$ARCHIVE.part" "$ARCHIVE"
fi
chmod 600 "$ARCHIVE"
printf '%s\n' "$ISSUER_KEY" > issuer-public-key.new
mv issuer-public-key.new issuer-public-key
rm -rf installer.new
mkdir installer.new
tar --extract --gzip --no-same-owner --file "$ARCHIVE" --directory installer.new
rm -rf installer
mv installer.new installer
exec /usr/bin/python3 "$WORK/installer/installer/scripts/setup-public-panel.py" \
  --bundle "$WORK/$ARCHIVE" --bundle-sha256 "$SHA256" --issuer-public-key-file "$WORK/issuer-public-key"
