#!@SH@
# $NetBSD: client-prepare,v 1.3 2016/12/18 22:59:35 joerg Exp $

. @PBULK_CONFIG@

set -e

if [ "${config_version}" != "@PBULK_CONFIG_VERSION@" ]; then
	echo "Your configuration has version ${config_version}."
	echo "This version of pbulk expects version @PBULK_CONFIG_VERSION@."
	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 
	/*)
		port=
		client=
		path=${client}
		;;
	*:*)
		port="-p ${client##*:}"
		client=${client%%:*}
		path=
		;;
	*)
		port=
		path=
		;;
	esac
	if [ -z "$path" ]; then
		ssh $port $client "${client_prepare_action}" &
	else
		clients_prepare_chroot "$path" &
	fi
done

wait
