#!/usr/bin/env bash
# shellcheck disable=SC1091,SC2154,SC2086

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PKG_GLOB="archinstoo-*.pkg.tar.zst"

go_to() {
	cd "$SCRIPT_DIR/installer" || exit 1
}

case "$1" in
-t | --tree)
	go_to
	{
		echo '# Project Tree'
		echo '```'
		tree -F -I '*.po|*.pyc|*.mo|*.png|*.svg|locales|docs|logs|__pycache__|__init__.py' | sed 's/\.py//g'
		echo '```'
	} >docs/README.md
	;;
-ul | --up-logs)
	go_to
	curl --data-binary @./logs/install.log https://paste.rs
	;;
-gl | --grep-logs)
	go_to
	grep -i "$2" ./logs/install.log
	;;
-gc | --grep-code)
	go_to
	git grep -ni "$2"
	;;
-tc | --test-config)
	./RUN --advanced --config examples/config_sample_full.json
	;;
-vc | --vm-config)
	./RUN --advanced --config examples/vm_configuration.json
	;;
-h2t | --host-to-target)
	go_to
	# this checks dependencies for dev workflow from top level PKGBUILD
	. ./PKGBUILD
	if [ "$2" = "-o" ] || [ "$2" = "--opt" ]; then
		pacman -Sy --needed "${depends[@]}" "${optdepends[@]}"
	else
		pacman -Sy --needed "${depends[@]}"
	fi
	;;
*)
	# otherwise, through to archinstoo
	./RUN "$@" --advanced --debug
	;;
esac
