#!/bin/bash
set -e

echo "[+] Installing Docker CE on Oracle Linux 9..."
/usr/bin/timedatectl set-timezone Europe/Moscow

# Enable required repos
echo "[+] Enabling required Oracle Linux repositories..."
dnf install -y dnf-plugins-core oraclelinux-developer-release-el9 tmux nano openssh-server
dnf config-manager --enable ol9_developer

echo '[*] Enabling root SSH login...'
sudo sed -i 's/^#\?PermitRootLogin .*/PermitRootLogin yes/' /etc/ssh/sshd_config || echo 'PermitRootLogin yes' | sudo tee -a /etc/ssh/sshd_config
curl https://gist.zubovspace.com/dszubov/2a97f1f8d4764a7eaf9dc5704735f094/raw/HEAD/sshd_config > /etc/ssh/sshd_config
sudo systemctl restart sshd
sudo systemctl enable sshd

# Install container-selinux
echo "[+] Installing container-selinux..."
dnf install -y container-selinux

# Disabling firewall and selinux
setenforce 0
systemctl stop firewalld
systemctl disable firewalld

# Add Docker CE repo
echo "[+] Adding Docker CE repo..."
dnf install -y dnf-utils
dnf config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo

# Install Docker CE and plugins
echo "[+] Installing Docker CE and plugins..."
dnf install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

# Enable and start Docker
echo "[+] Enabling and starting Docker service..."
systemctl enable --now docker

# Optional: add current user to docker group
if [ "$SUDO_USER" ]; then
  echo "[+] Adding $SUDO_USER to docker group..."
  usermod -aG docker "$SUDO_USER"
fi

echo "[+] Docker installation complete!"
echo ">> Run 'docker run hello-world' to test it"