#!/bin/zsh
setopt no_unset warn_create_global extended_glob

() {
	local -a cmds
	cmds=(
		lock
		show-inbox
		root-terminal
		terminal-wpa_cli
		exit
		adjust-brightness/{1,2,3,4,5,6,7,8,9,10}0%
	)
	cat <<<${(F)cmds}
}

() {
	local svc
	for svc in $HOME/chome/ssh/ssh-rc/out/s6-rc-source/*:master(N); do
		printf "!c.urxvt ssh.%s ssh-t %s\n" \
			$USER ${${svc:t}%:master}
	done
}

() {
	local svc
	for svc in $HOME/chome/rcm-ssh/ssh-rc/out/runimage/*:master(N); do
		printf "!c.urxvt rcm-ssh.%s ssh-t %s\n" \
			$USER ${${${svc:t}%:master}#ssh.}
	done
}

() {
	local xsvc container
	for xsvc in $HOME/chome/*/run/rc.extra/xsvc.*(/N); do
		container=${xsvc:h:h:h:t}
		printf "!xsvc-in-container-xpra %s %s\n" \
			$container.$USER ${${xsvc:t}#xsvc.}
	done
}

img_has_bins() {
	local base p b found
	base=$1
	shift
	for b in "$@"; do
		found=0
		for p in $base/{bin,usr/bin,usr/local/bin}; do
			[[ -x $p/$b ]] && found=1 && break
		done
		((found)) || return 1
	done
	return 0
}

(){
	local cntdir imgdir ignored
	for cntdir in "$@"; do
		if [[ -f $cntdir/data/fstab ]] && read imgdir ignored <$cntdir/data/fstab; then
			if img_has_bins $imgdir zsh; then
				img_has_bins $imgdir tmux && \
					printf "!c.tmux %s\n" ${${cntdir:t}#container.}
				#img_has_bins $imgdir tmux && \
				#	printf "container-urxvt/%s/tmux -2u new-session -s %s -A\n" \
				#	${${cntdir:t}#container.} ${(qqq)${${cntdir:t}#container.}%.*}
				#img_has_bins $imgdir abduco && \
				#	printf "abduco/%s/shell/zsh -li\n" ${${cntdir:t}#container.}
				printf "container-urxvt/%s/zsh -li\n" ${${cntdir:t}#container.}
			fi
		fi
	done
} /run/service/container.*.$USER

# vim: ft=zsh sts=4 ts=4 sw=4 noet