#!/usr/bin/env bash

hostname=$(cat /proc/sys/kernel/hostname)

_systemctl () {
	systemctl --user "${@:2}" "autostart@$1.service"
}

_dasel () {
	dasel -f "$HOME/.config/autostart.toml" -r toml -w plain "${@}"
}

_list () {
	part_root='all().filter(not(equal(type(),object))).key()'
	part_host="all().filter(equal(type(),object)).filter(equal(key(),$hostname)).all().key()"
	query="merge($part_host,$part_root).all()"
	programs=$(_dasel "$query" \
		| sort \
		| uniq)

	for program in $programs
	do
		cmd=$(_get_autostart_cmd "$program")
		if [ -n "$cmd" ]; then
			echo "$program"
		fi
	done
}

_echo_table () {
	for unit in "$@"
	do
		printf "%s\t%s\t%s\n" \
			"$unit" \
			"$(_systemctl "$unit" is-enabled)" \
			"$(_systemctl "$unit" is-active)"
	done
}

_autostart_manage_list () {
	_list
}

_autostart_manage_info () {
	autostart_units=()
	while IFS='' read -r line
	do
		autostart_units+=("$line")
	done < <(_list)

	_echo_table "${autostart_units[@]}" | column -t -s$'\t' --table-columns 'Unit,Enabled?,Active?'
}

_autostart_manage_log () {
	journalctl --user -fu "autostart@$1.service"
}

_autostart_manage_sync () {
	rm "$HOME/.config/systemd/user/autostart.target.wants/"*

	autostart_units=()
	while IFS='' read -r line
	do
		autostart_units+=("$line")
	done < <(_list)

	for unit in "${autostart_units[@]}"
	do
		_systemctl "$unit" add-wants autostart.target
	done
}

_get_autostart_cmd () {
	selector_base="all()"
	selector_host="all().filter(equal(type(),object)).filter(equal(key(),$hostname)).all()"
	selector_entry="filter(not(equal(type(),object))).filter(equal(key(),$1))"
	cmd=$(_dasel "$selector_base.$selector_entry")
	cmd_local=$(_dasel "$selector_host.$selector_entry")

	if [ -n "$cmd_local" ]; then
		if [ "$cmd_local" = "false" ]; then
			cmd=""
		else
			cmd="$cmd_local"
		fi
	fi
	echo "$cmd"
}

_autostart_manage_exec () {
	cmd=$(_get_autostart_cmd "$1")
	bash -c "$cmd"
}

_autostart_run_graphical () {
	start-audio pipewire
	wait-for-service "network-online.target"

	if ! pgrep keepassxc >/dev/null
	then
		if pass x
		then
			(pass keepass | head -n 1 | keepassxc --pw-stdin ~/sync/passwords.kdbx) &
		fi
	fi

	autoinstall graphical
	autostart-manage run

}

_autostart_run_wayland () {
	_autostart_run_graphical

	killall -SIGUSR2 waybar
}

_autostart_run_xorg () {

	feh --bg-fill "$XDG_PICTURES_DIR/wallpaper/active_wallpaper"

	"$HOME/.config/polybar/launch.sh" &
	"$HOME/.config/bspwm/focus-voip.py" &

	numlockx on
	xsetroot -cursor_name left_ptr
	xrdb "$HOME/.Xresources"

	_autostart_run_graphical
}

case $1 in
	list)			_autostart_manage_list ;;
	info)			_autostart_manage_info ;;
	sync)			_autostart_manage_sync ;;
	enable)			_systemctl "$2" add-wants autostart.target ;;
	disable)		_systemctl "$2" disable ;;
	restart)		_systemctl "${2:-*}" restart;;
	log)			_autostart_manage_log "$2";;
	start)			_systemctl "${2:-*}" start;;
	status)			_systemctl "${2:-*}" status ;;
	stop)			_systemctl "${2:-*}" stop ;;
	run)			systemctl --user start autostart.target ;;
	run-xorg)		_autostart_run_xorg ;;
	run-wayland)	_autostart_run_wayland ;;
	exec)			_autostart_manage_exec "$2" ;;
	*)				echo "'$1' is not valid" ;;
esac