#!/bin/sh

set -e

# detect user: first try to use gitlab_ci_multi_runner
for USER in gitlab_ci_multi_runner gitlab-runner; do
  if id -u "$USER" >/dev/null 2>/dev/null; then
    echo "GitLab Runner: detected user $USER"
    break
  fi
done

# Disable
# [skel](https://www.thegeekdiary.com/understanding-the-etc-skel-directory-in-linux/)
# for distributions like Debian buster
# https://gitlab.com/gitlab-org/gitlab-runner/-/issues/1379
GITLAB_RUNNER_DISABLE_SKEL=${GITLAB_RUNNER_DISABLE_SKEL:-false}

# create user if doesn't exist: it will create gitlab-runner if not found
if ! id -u "$USER" >/dev/null 2>/dev/null; then
  echo "GitLab Runner: creating $USER..."

  if [ $GITLAB_RUNNER_DISABLE_SKEL = true ]; then
    echo "Home directory skeleton not used"
    useradd --system --shell /bin/bash --comment 'GitLab Runner' --create-home --skel /dev/null $USER
  else
    useradd --system --shell /bin/bash --comment 'GitLab Runner' --create-home $USER
  fi
fi

# add user to docker group to allow Docker access (insecure)
if id -nG "$USER" | grep -q docker; then
  echo "WARNING: $USER belongs to group docker which is insecure, because allows to have root access to host"
fi

# get USER home directory
eval HOMEDIR=~$USER

# create empty config and re-register runner
mkdir -p /etc/gitlab-runner
chmod 0700 /etc/gitlab-runner
if [ -f $HOMEDIR/config.toml ] && [ ! -f /etc/gitlab-runner/config.toml ]; then
  echo "GitLab Runner: importing configuration to /etc/gitlab-runner/config.toml"
  cp $HOMEDIR/config.toml /etc/gitlab-runner/config.toml
  chmod 0600 /etc/gitlab-runner/config.toml
fi

# uninstall old service
if gitlab-runner status --service="gitlab-runner"; then
  gitlab-runner stop --service="gitlab-runner" >/dev/null 2>/dev/null || :
  gitlab-runner uninstall --service="gitlab-runner" >/dev/null 2>/dev/null || :
fi

# if migrating from pre 10.0.0 installation
if gitlab-runner status --service="gitlab-ci-multi-runner"; then
  gitlab-runner stop --service="gitlab-ci-multi-runner" >/dev/null 2>/dev/null || :
  gitlab-runner uninstall --service="gitlab-ci-multi-runner" >/dev/null 2>/dev/null || :
fi

# re-register runner
gitlab-runner stop >/dev/null 2>/dev/null || :
gitlab-runner uninstall >/dev/null 2>/dev/null || :
gitlab-runner install --user=$USER --working-directory=$HOMEDIR

# start runner service
gitlab-runner start || :
