#!/bin/bash
# This is a Docker extension for the xen-make-guest script
# -------------------------------------------------------------------
# Copyright (C) 2021
# by Intevation GmbH
# Author(s):
# Benedikt Tuchen <benedikt@intevation.de>

# This program is free software under the GNU GPL (>=v2)
# Read the file COPYING coming with the software for details.
# -------------------------------------------------------------------

create_prerequisites()
{
  verbose "Create prerequisites for docker usage..."
  verbose "Install dependencies for official docker repo..."
  runcmd chroot ${DOMUMNT} bash -c "LOGBUCH_BATCHMODE=yes DEBIAN_FRONTEND=noninteractive \
                            DEBIAN_PRIORITY=critical apt-install -y --purge \
                            gnupg2 git mercurial"
  verbose "Installing source list file for docker..."
  add_log_item "# Steps for the installation of upstream docker with docker.xmgext"
  create_file_from_template /etc/apt/sources.list.d/docker.list docker-xmgext.list
  create_file_from_template /etc/apt/preferences.d/docker docker-apt-pinning.txt
  if [ ! -d "${DOMUMNT}/etc/apt/keyrings" ]; then
    verbose "Create /etc/apt/keyrings directory for third-party keys"
    runcmd chroot ${DOMUMNT} bash -c "mkdir /etc/apt/keyrings"
    add_log_item "mkdir /etc/apt/keyrings"
  fi
  verbose "Add official docker gpg to /etc/apt/keyrings/ ..."
  runcmd chroot ${DOMUMNT} bash -c "curl -fsSL -o /etc/apt/keyrings/docker.asc https://download.docker.com/linux/debian/gpg"
  add_log_item "curl -fsSL -o /etc/apt/keyrings/docker.asc https://download.docker.com/linux/debian/gpg"
  write_log_items
}

install_docker()
{
  verbose "Installation of docker-ce..."
  runcmd chroot ${DOMUMNT} bash -c "DEBIAN_FRONTEND=noninteractive DEBIAN_PRIORITY=critical apt update"
  runcmd chroot ${DOMUMNT} bash -c "LOGBUCH_BATCHMODE=yes DEBIAN_FRONTEND=noninteractive \
                            DEBIAN_PRIORITY=critical apt-install -y --purge --no-install-recommends \
                            docker-ce docker-ce-cli containerd.io docker-compose-plugin"
}

additional_steps()
{
  verbose "Add $USERLOGIN to group docker..."
  runcmd chroot ${DOMUMNT} usermod -aG docker $USERLOGIN
  add_log_item "usermod -aG docker $USERLOGIN"
  write_log_items
  verbose "Configure docker for intevation..."
  runcmd chroot ${DOMUMNT} bash -c 'echo -e "\n212.95.107.148	index.docker.io\n212.95.107.148	auth.docker.io\n212.95.107.148	registry-origin.docker.io\n212.95.107.148	registry-1.docker.io\n212.95.107.148	registry.hub.docker.com" >> /etc/hosts'
  add_log_item "/etc/hosts:
    212.95.107.148	index.docker.io
    212.95.107.148	auth.docker.io
    212.95.107.148	registry-origin.docker.io
    212.95.107.148	registry-1.docker.io
    212.95.107.148	registry.hub.docker.com"
  if [ ! -d "${DOMUMNT}/etc/docker" ]; then
    runcmd chroot ${DOMUMNT} mkdir /etc/docker
    add_log_item "mkdir /etc/docker"
  fi
  create_file_from_template /etc/docker/daemon.json docker-daemon-json.txt
  write_log_items
}

echo "Docker extension for xen-make-guest loaded..."

create_prerequisites
install_docker
additional_steps

echo "Docker extension finished..."
