#!/usr/bin/perl

#
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

use strict;
use warnings;
use FindBin qw{$Bin};
use lib "$Bin/../lib";
use InstallUtils;
use WebDep;

my @deps = WebDep->getDeps("$Bin/web_deps.txt");

my $errors   = 0;
my $warnings = 0;
foreach my $dep (@deps) {
	my $fn  = $dep->getDestFileName();
	my $src = $dep->getSrcFileName();

	# download source to compare with current version
	my ( $action, $err ) = $dep->update();
	if ( defined $err ) {
		print "Unable to update $fn: $err\n";
		if ( !-f $fn ) {

			# count errors when no file and can't be fetched
			$errors++;
		}
		else {
			# file exists but can't be updated
			$warnings++;
		}
		next;
	}
	else {
		# report what was done..
		print "$action $fn\n";
	}
}

if ($warnings) {
	print "WARNING: Some files exist but can't be updated\n";
}
if ($errors) {
	print "FATAL: Unable to update one or more files.\n";
	exit 1;
}
