#!/bin/sh
# $NetBSD: client-prepare,v 1.5 2020/04/04 15:17:48 riastradh Exp $

. /usr/pkg/etc/pbulk.conf

set -e

if [ "${config_version}" != "0.68" ]; then
	echo "Your configuration has version ${config_version}."
	echo "This version of pbulk expects version 0.68."
	exit 1
fi

unique_clients=
for client in ${scan_clients} ${build_clients}; do
	case ${unique_clients} in
	"${client}" | "${client} "* | *" ${client} "* | *" ${client}" )
		: # exists already
		;;
	*)
		if [ -n "$unique_clients" ]; then
			unique_clients="$unique_clients "
		fi
		unique_clients="$unique_clients ${client}"
		;;
	esac
done

for client in ${unique_clients}; do
	case ${client} in
	/*)
		path=${client}
		port=
		client=
		;;
	*:*)
		port="-p ${client##*:}"
		client=${client%%:*}
		path=
		;;
	*)
		port=
		path=
		;;
	esac
	if [ -z "$path" ]; then
		ssh $port $client "${client_prepare_action}" &
	else
		$client_prepare_chroot "$path" &
	fi
done

wait
