#!/bin/bash

PROGNAME="${BASH_SOURCE[0]}"
HERE="$(cd "$(dirname "$PROGNAME")" &>/dev/null && pwd)"
READIES=$(cd $HERE/.. && pwd)
. $READIES/shibumi/defs

if [[ -f /etc/os-release ]]; then
	os=$(source /etc/os-release; echo "${ID}")
	osver=$(source /etc/os-release; echo "${VERSION_ID}")
fi
arch=$(uname -m)

xinstall bc

packs="ca-certificates wget curl unzip gzip xz"

if is_command apt-get; then
	packs=${packs/xz/xz-utils}
elif is_command yum; then
	packs+=" tar"
elif is_command brew; then
	packs=${packs/ca-certificates/}
fi
if [[ $os == rocky || $os == almalinux || $os == rhel ]]; then
	# has curl-minimal which conflicts with curl
	if [[ $(echo "$osver >= 9.0" | bc -l) ]]; then
		packs=${packs/curl/}
	fi
fi

if [[ $FORCE != 1 ]]; then
	missing=0
	for cmd in ${packs/ca-certificates/}; do
		if ! is_command $cmd; then
			missing=1
			break
		fi
	done
	[[ $missing == 0 ]] && exit 0
fi

xinstall $packs
exit 0

if is_command apt-get; then
	export DEBIAN_FRONTEND=noninteractive
	runn apt-get update -qq
	packs=${packs/xz/xz-utils}
	runn apt-get install --fix-missing -y $packs
elif is_command dnf; then
	runn dnf install -y $packs
elif is_command yum; then
	runn yum install -y $packs
elif is_command zypper; then
	runn zypper install -y $packs
elif is_command apk; then
	runn apk update
	runn apk add $packs
elif is_command pacman; then
	pacman --noconfirm -S $packs
elif is_command brew; then
	packs=${packs/ca-certificates/}
	runn brew install $packs
elif is_command pkg; then
	runn pkg install -y $packs
fi
