#!/bin/bash
set -e

hosts="$@"

if [ -z "$hosts" ]; then
	echo "Usage: $(basename $0) [user1@]host1 [[user2@]host2]..."
	exit 1
fi

packages=""

while read -r line; do
    packageName=$(echo $line | cut -d"_" -f1)
    packages="$packages $packageName"
done < "$PWD/debian/files"

if [ -z "$packages" ]; then
	echo "No packages found to install."
	exit 1
fi

# FIXME: Packages are not upgraded when config files are changed

echo -e "\n# Upgrading servers\n"

for host in $hosts; do
    echo " * Upgrading $host"
    ssh $host "update-repo vn && apt-get --quiet --yes install $packages"
done