#!/bin/bash -e
# Install required dependencies for building Keyman on Linux
# (copied from wesay/build/agent/install-deps)

# get packages from config file listed in the variable $1
getPackages()
{
	packages=$(git config -f $(dirname "$0")/dependencies.config --get "$1" 2>/dev/null)
	for package in $packages; do
		if [ "${package:0:1}" = "@" ]; then
			getPackages "${package:1}"
		else
			echo -n "$package "
		fi
	done
}

# Install packages $@
install()
{
	if [ $# -gt 0 ]; then
		sudo apt install -y "$@"
	fi
}

distribution=$(lsb_release -i -s)
version=$(lsb_release -c -s)
arch=$(dpkg --print-architecture)

sudo apt update
install $(getPackages common.any)
install $(getPackages common.$arch)
install $(getPackages $distribution.any)
install $(getPackages $distribution.$arch)
install $(getPackages $version.any)
install $(getPackages $version.$arch)
