#!/bin/zsh
setopt no_unset extended_glob warn_create_global
typeset -g DSTROOT CONTAINER_NAME REPO_ROOT
set -x
DSTROOT=$1
CONTAINER_NAME=$2
REPO_ROOT=${0:P:h:h}

die() {
	printf >&2 "FATAL: %s\n" "$@"
	exit 1
}

ensure-container-started() {
	[[ $1 == */* ]] && die "Invalid container name: ${(qqq)1}"
	s6-svc -wU -T 4000 -o /run/service/container.$1
}

to-current-inbox() {
	local from_container=$1
	shift
	ensure-container-started $from_container
	link-to-container-inbox $from_container $CONTAINER_NAME.$USER "$@" || exit $?
}

# Copy over the versioned filesystem image
copy-skel() {
	local skel dst
	[[ -d $REPO_ROOT/$1 ]] || die "skel dir $REPO_ROOT/$1 does not exist"
	s6-hiercopy $REPO_ROOT/$1/homedir $DSTROOT/home/$USER || exit $?
	for skel in $REPO_ROOT/$1/*/*(DN); do
		[[ ${skel:h:t} == homedir ]] && continue
		dst=$DSTROOT/${skel:h:t}/${skel:t}
		s6-hiercopy $skel $dst || exit $?
	done
}

setup-sndio() {
	local sndio_container=sndiod.$USER
	local sndio_dir=$DSTROOT/run/inbox/$sndio_container/tmp/sndio-$UID
	s6-svc -wU -T 4000 -o /run/service/container.$sndio_container || exit $?
	mkdir -p $sndio_dir || exit $?
	ln -fv /run/containers/$sndio_container/tmp/sndio-$UID/sock0 $sndio_dir/ || exit $?
	#ln -fv /run/containers/$sndio_container/tmp/sndio-$UID/sock{0,1} $sndio_dir/ || exit $?
	ln -sfv ../run/inbox/$sndio_container/tmp/sndio-$UID $DSTROOT/tmp || exit $?
}

setup-gpg-agent() {
	to-current-inbox gpg.$USER run/gnupg/gpg-agent.sock
}

setup-gpg-ssh-agent() {
	to-current-inbox gpg.$USER run/gnupg/gpg-agent-ssh.sock
}

setup-urxvt() {
	local sock session
	for sock in /run/containers/xsession.*.$USER/run/exec/urxvt(N); do
		session=${sock:h:h:h:t}
		link-to-container-inbox $session $CONTAINER_NAME.$USER run/exec/urxvt || exit $?
	done
}

setup-pass-show() {
	local -a svc_names
	local s
	svc_names=( "${@//\//!}" )
	for s in $svc_names; do
		to-current-inbox pass.$USER run/pass/show/$s || exit $?
	done
}

setup-ssh-exec() {
	to-current-inbox ssh.$USER run/ssh-exec/$^@ || exit $?
}

setup-rcm-ssh-exec() {
	to-current-inbox rcm-ssh.$USER run/ssh-exec/$^@ || exit $?
}

main() {
	if ! [[ -d $DSTROOT ]]; then
		echo >&2 "Destination root not a directory: $DSTROOT" 
		exit 2
	fi

	local -a git_mirrors=(
		# codeberg.org
		git.busybox.net
		git.code.sf.net
		git.kernel.org
		git.skarnet.org
		# git.sr.ht
		# git.vuxu.org
		# gitea.blesmrt.net
		github.com
		gitlab.alpinelinux.org
		gitlab.com
	)

	setup-urxvt

	case $CONTAINER_NAME in
		(xpra)
			copy-skel xpra-skel
			;;
		(ssh|rcm-ssh)
			setup-gpg-ssh-agent
			;;
		(pass)
			setup-gpg-agent
			;;
		(ff-*-audio|mpd|mpv)
			setup-sndio
			;;
		(alpine-recombee)
			setup-gpg-agent
			setup-gpg-ssh-agent
			;;
		(mail-net)
			setup-pass-show smtp:/te2000.cz/ccx smtp:/disroot.org/ccx
			;;
		(rcm-mail-net)
			setup-pass-show mailto:/recombee.com/jan.pobrislo
			;;
		(mail|rcm-mail)
			to-current-inbox $CONTAINER_NAME-net.$USER run/exec/sync-mail
			setup-gpg-agent
			;;
		(profanity)
			setup-pass-show xmpp:/blesmrt.net/ccx
			;;
		(weechat)
			setup-pass-show weechat/P14s-gen2
			;;
		(bzr)
			setup-ssh-exec ccx@eva ccx@saturnin ccx@www_te2000 root@masterozo root@masterozo2
			;;
		(git)
			setup-ssh-exec ccx@eva ccx@saturnin ccx@www_te2000
			setup-rcm-ssh-exec git@git.recombee.net
			to-current-inbox tinyproxy.$USER run/http_proxy/git-forges
			;;
		(socials)
			to-current-inbox tor.$USER run/tor/socks
			;;
		(pthbs-dev)
			to-current-inbox tinyproxy.$USER run/http_proxy/pthbs
			to-current-inbox git.$USER run/git/ro.$^git_mirrors run/git/rw.pthbs
			;;
		(x11-dev)
			to-current-inbox tinyproxy.$USER run/http_proxy/pypi
			to-current-inbox git.$USER run/git/ro.github.com
			;;
		(ssrn-master-dev)
			to-current-inbox tinyproxy.$USER run/http_proxy/pypi
			;;
		(rcm-postgresql-dev)
			to-current-inbox git.$USER run/git/rw.git.recombee.net
			;;
		(notes)
			to-current-inbox git.$USER run/git/rw.notes
			;;
	esac
	case $CONTAINER_NAME in
		(ff-*)
			if [[ -d $HOME/chome/$CONTAINER_NAME/. && ! -d  $HOME/chome/$CONTAINER_NAME/.mozilla/firefox/default ]]; then
				rsync -avi $HOME/chome/ff-template/.mozilla $HOME/chome/$CONTAINER_NAME/ || exec $?
			fi
			;;
	esac

	return 0
}

main