#!/usr/bin/env bash
#
BASE_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)"

DOCKER_CONFIG="/etc/docker/daemon.json"
docker_copy_failed=0

cd "${BASE_DIR}" || exit 1

function check_docker_install() {
  command -v docker >/dev/null || {
    install_docker
  }
}

function check_compose_install() {
  command -v docker-compose >/dev/null || {
    install_compose
  }
}

function install_docker() {
  pwd
  \cp -f ./docker/* /usr/bin/
  \cp -f ./docker.service /etc/systemd/system/
}

function install_compose() {
  \cp -f ./docker/docker-compose /usr/bin/
  chmod +x /usr/bin/docker-compose
}

function start_docker() {
  if command -v systemctl >/dev/null; then
    systemctl daemon-reload
    systemctl enable docker
    systemctl start docker
  fi
  if ! docker ps >/dev/null 2>&1; then
    echo "failed"
    exit 1
  fi
}

function main() {
  echo "Install Docker"
  check_docker_install
  check_compose_install
  start_docker
}

if [[ "$0" == "${BASH_SOURCE[0]}" ]]; then
  main
fi
 
