#!/usr/bin/perl

#
# The Thingy upgrade tool
#
# (C) 2006-2011 Julian Mehnle <julian@mehnle.net>
# $Id$
#
##############################################################################

=head1 NAME

thingy-upgrade - The Thingy upgrade tool.

=head1 VERSION

1.107

=head1 SYNOPSIS

B<thingy-upgrade>

=head1 DESCRIPTION

Should be called after a Thingy installation was upgraded (this happens
automatically on Debian systems).  Performs necessary upgrade maintenance on
all existing Thingy instances.

=head1 AUTHOR

Julian Mehnle <julian@mehnle.net>

=cut

use version; our $VERSION = qv('1.107');

use warnings;
use strict;
use locale;
use lib '/usr/share/thingy/lib';

use IO::Handle;
use Error ':try';

use Thingy;

use constant TRUE   => (0 == 0);
use constant FALSE  => not TRUE;

STDOUT->autoflush(TRUE);
STDERR->autoflush(TRUE);

my $logger = sub {
    my ($log_level, @messages) = @_;
    STDOUT->print(map('    ' x $log_level . "$_\n", @messages));
};

try {
    $logger->(0, 'Upgrading existing Thingy instances...');
    foreach my $thingy_name (Thingy->enumerate()) {
        my $thingy = Thingy->get(name => $thingy_name);
        $thingy->upgrade($logger, 1);
    }
    $logger->(0, 'Finished.');
    exit(0);
}
catch Thingy::Exception with {
    my ($e) = @_;
    STDERR->print("Error: $e");
    exit(1);
}
otherwise {
    my ($error) = @_;
    STDERR->print("An unexpected error occurred: $error\n");
    exit(1);
};

