#! /usr/bin/env bash

panic()
{
	echo "ERROR: $@"
	exit 1
}

usage()
{
	echo "bad usage: $@"
	exit 2
}

verbose=0
os=

while getopts vs: opt; do
	case $opt in
	s)
		os="$OPTARG";;
	v)
		verbose=$((verbose + 1));;
	\?)
		usage
		break;;
	esac
done
shift $((OPTIND - 1))

if [ -z "$os" ]; then
	if [ -n "$RUNNER_OS" ]; then
		case "$RUNNER_OS" in
		MacOS|macOS)
			os=macos;;
		Linux|linux)
			os=linux;;
		Windows|windows)
			os=windows;;
		*)
			os=unknown;;
		esac
	fi
fi

echo "OS: $os"

case "$os" in
linux)
	sudo apt-get update || \
	  panic "apt-get update failed"
	packages=(
		##########
		#cmake
		#make
		#gcc
		##########
		libx11-dev
		libxext-dev
		libxrandr-dev
		libxi-dev
		libxt-dev
		libxcursor-dev
		libxinerama-dev
		libxft-dev
		##########
		libpng-dev
		libjpeg-dev
		libgd-dev
		libnetpbm-dev
		##########
	)
	sudo apt-get install -y "${packages[@]}" || \
	  panic "cannot install packages"
	;;
macos)
	packages=()
	packages+=(libx11)
	packages+=(libxt)
	echo "installing packages ${packages[*]}"
	brew install "${packages[@]}" || panic "cannot install packages"
	;;
windows)
	;;
*)
	panic "invalid OS"
	;;
esac

exit 0
