#!/bin/bash

    << CHANGELOG
    2021-03-10: Template Created & Added to GIT
    2021-03-30: Forked from template
    2021-08-03: Specify installation of version 13.8.0
CHANGELOG


if [[ "$EUID" -ne 0 ]]; then
	echo "This installer needs to be run with superuser privileges."
	exit
fi




echo -e "
$(tput setaf 3; tput setab 0)
    >> # [GitLab-01] - Basic Dependencies (apt update)
$(tput sgr 0)
"

apt -qq update




echo -e "
$(tput setaf 3; tput setab 0)
    >> # [GitLab-02] - Add Official GitLab repository. Enable APT pinning to specify repo priority
$(tput sgr 0)
"

    : '
    What this does:
        # Add Official GitLab repository
        # Enable APT pinning (to prefer gitlab.com packages over repository)
        # Update package information
    '

(curl -L "https://packages.gitlab.com/install/repositories/runner/gitlab-runner/script.deb.sh" | sudo bash) && \
{
    cat <<EOF | sudo tee /etc/apt/preferences.d/pin-gitlab-runner.pref
Explanation: Prefer GitLab provided packages over the Debian native ones
Package: gitlab-runner
Pin: origin packages.gitlab.com
Pin-Priority: 1001
EOF
} && \
sudo apt -qq update && \
preparationCompleted=true




echo -e "
$(tput setaf 3; tput setab 0)
    >> # [GitLab-03] - Change user account for gitlab runner
$(tput sgr 0)
"

if [ "$preparationCompleted" = true ]; then

    ## Uncomment the line below to detect the first account not named "root", instead of the user running this script
    ## Acct=$(getent passwd {1000..60000} | head -1 | awk  -F: '{ print $1 }') && echo "Account: ${Acct}" && \

    Acct=`who | awk '{print $1}'` && \
    HomeDir=`sudo -H -u ${Acct} -s eval 'echo $HOME'` && \
    WorkDir=${HomeDir}/gitlab-scratch && \
    sudo mkdir -p ${WorkDir} && \
    sudo chown ${Acct}:${Acct} ${WorkDir} && \
    sudo mkdir -p /etc/systemd/system/gitlab-runner.service.d && \
    {
    cat << EOF | sudo tee /etc/systemd/system/gitlab-runner.service.d/exec_start.conf
[Service]
ExecStart=
ExecStart=/usr/lib/gitlab-runner/gitlab-runner "run" "--working-directory" "${WorkDir}" "--config" "/etc/gitlab-runner/config.toml" "--service" "gitlab-runner" "--syslog" "--user" "${Acct}"
EOF
    } && \
    gitlabConfigured=true
else
    echo -e "$(tput setaf 0; tput setab 3)" && \
    echo "ERROR: GitLab repository was not successfully added/configured, so skipping this step..." && \
    echo -e "$(tput sgr 0)"
fi




echo -e "
$(tput setaf 3; tput setab 0)
    >> # [GitLab-04] - Install GitLab
$(tput sgr 0)
"

if [ "$gitlabConfigured" = true ]; then
    # Disable skel (skeleton directory) to prevent 'No such file' job failures
    export GITLAB_RUNNER_DISABLE_SKEL=true; apt-cache madison gitlab-runner && sudo -E apt -qq install -y gitlab-runner=13.8.0 && \
    echo -e "$(tput setaf 7; tput setab 0)" && \
    (ps aux | grep gitlab) && \
    echo -e "$(tput sgr 0)" && \
    installationCompleted=true
else
    echo -e "$(tput setaf 0; tput setab 3)" && \
    echo "ERROR: GitLab Working Directory & Service Account was not successfully configured, so skipping this step..." && \
    echo -e "$(tput sgr 0)"
fi




echo -e "
$(tput setaf 3; tput setab 0)
    >> # [GitLab-05] - Register Runner
$(tput sgr 0)
"

if [ "$installationCompleted" = true ]; then
    export HostName=`cat /etc/hostname`
    export SampleConfig=$(cat << "END"
# Registration Token is gotten by Project > Settings > CI/CD > Runners > "Set up a specific runner manually"
    
  ** SAMPLE CONFIGURATION:
  
  
        sudo gitlab-runner register
            GitLab instance URL =   https://gitlab.com/
            Registration Token  =   ***
            Description         =   $HostName
            Tags                =   self-hosted, linux, x64, $HostName
            Executor            =   shell
END
    ) && \
    echo -e "$(tput setaf 7; tput setab 0)" && \
    echo "${SampleConfig}" && \
    echo -e "$(tput sgr 0)"
else
    echo -e "$(tput setaf 0; tput setab 3)" && \
    echo "ERROR: GitLab was not installed. Skipping Sample Configration printout" && \
    echo -e "$(tput sgr 0)"
fi




echo -e "
$(tput setaf 3; tput setab 0)
    >> # [GitLab-06] - Script Cleanup
$(tput sgr 0)
"

if ! command -v realpath &> /dev/null
then
    echo -e "COMMAND 'realpath' could not be found. $(tput setaf 3; tput setab 0)Please move script manually.$(tput sgr 0)"
else
    SCRIPT=`realpath -s $0` && \
    DESTINATION=~/Deploy && \
    mkdir -p ${DESTINATION} && \
    mv ${SCRIPT} ${DESTINATION} && \
    ls -laR ${DESTINATION}
fi




echo -e "
$(tput setaf 3; tput setab 0)
    >> # [GitLab-07] - Post-Cleanup - Invoke Runner Registration
$(tput sgr 0)
"

if [ "$installationCompleted" = true ]; then
    sudo gitlab-runner register && \
    configCompleted=true && \
    echo -e "$(tput setaf 0; tput setab 2)" && \
    echo "SUCCESS: Configuration of gitlab-runner completed" && \
    echo -e "$(tput sgr 0)"
else
    echo -e "$(tput setaf 0; tput setab 3)" && \
    echo "ERROR: GitLab was not installed. Skipping Runner Registration" && \
    echo -e "$(tput sgr 0)"
fi




echo -e "
$(tput setaf 3; tput setab 0)
    >> # [GitLab-08] - Post-Configuration - Verify Runner is Communicating
$(tput sgr 0)
"
    # Source - https://stackoverflow.com/a/60074020/11295787
    

if [ "$configCompleted" = true ]; then

    echo -e "$(tput setaf 7; tput setab 0)" && \
    sudo gitlab-runner verify && \
    echo -e "$(tput sgr 0)"
else
    echo -e "$(tput setaf 0; tput setab 3)" && \
    echo "ERROR: GitLab-Runner was not successfully configured / registered." && \
    echo -e "$(tput sgr 0)"
fi

