#!/usr/bin/env bash
set -Eeuo pipefail

run_as_root() {
  if [[ "${EUID}" -eq 0 ]]; then
    "$@"
  else
    sudo "$@"
  fi
}

run_as_root apt-get update

run_as_root apt-get install -y \
  ca-certificates \
  curl \
  wget \
  git \
  vim \
  nano \
  htop \
  tree \
  unzip \
  zip \
  tar \
  xz-utils \
  zstd \
  file \
  pkg-config \
  build-essential \
  gcc \
  g++ \
  make \
  cmake \
  ninja-build \
  autoconf \
  automake \
  libtool \
  m4 \
  perl \
  python3 \
  python3-pip \
  python3-venv \
  gdb \
  openssh-server \
  openssh-client \
  mingw-w64 \
  gcc-mingw-w64-x86-64 \
  g++-mingw-w64-x86-64 \
  binutils-mingw-w64-x86-64 \
  gcc-mingw-w64-i686 \
  g++-mingw-w64-i686 \
  binutils-mingw-w64-i686 \
  squashfs-tools \
  texinfo flex bison rsync  debootstrap

if command -v systemctl >/dev/null 2>&1; then
  run_as_root systemctl enable --now ssh
else
  run_as_root service ssh start || true
fi

if command -v ufw >/dev/null 2>&1; then
  run_as_root ufw allow OpenSSH || true
fi

echo "[setup] done"