#!/bin/bash -eo pipefail

# Install build dependencies
curl -s https://apt.kitware.com/keys/kitware-archive-latest.asc | apt-key add -
apt-add-repository -y 'deb https://apt.kitware.com/ubuntu/ focal main'
apt-get update
apt-get install -y \
	software-properties-common \
	cmake \
	g++-10 \
	libstdc++-10-dev \
	libasound2-dev \
	libjack-jackd2-dev \
	ladspa-sdk \
	libfreetype6-dev \
	libx11-dev \
	libxcomposite-dev \
	libxcursor-dev \
	libxcursor-dev \
	libxext-dev \
	libxinerama-dev \
	libxrandr-dev \
	libxrender-dev \
	libglu1-mesa-dev \
	mesa-common-dev \
	upx-ucl

# Create a new user with the same UID/GID as the host to
# avoid file-permission issues with the mounted volume

# Sanity check
if [[ "$#" -ne 2 ]]; then
	echo "ERROR: Wrong number of arguments: only provide both UID and GID."
	exit 1
fi

# Get UID/GID arguments
WITH_UID=$1
WITH_GID=$2

# Check sanity
if [[ "$WITH_UID" -lt 1000 ]] || [[ "$WITH_GID" -lt 1000 ]]; then
	echo "ERROR: both UID($WITH_UID) and GID($WITH_GID) must be >= 1000."
	exit 1
fi

addgroup --gid "${WITH_GID}" arkostracker
adduser --uid "${WITH_UID}" --ingroup arkostracker build

# Install CircleCI-CLI to provide .circleci/config.yml validation
curl -fLSs https://raw.githubusercontent.com/CircleCI-Public/circleci-cli/main/install.sh | bash
