#!/usr/bin/perl
#-------------------------------------------------------------------------------
# Copyright (C) 2006-2021 British Crown (Met Office) & Contributors.
#
# This file is part of FCM, tools for managing and building source code.
#
# FCM is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# FCM is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with FCM. If not, see <http://www.gnu.org/licenses/>.
#-------------------------------------------------------------------------------

# This is a simplified version of fcm-daily-update which
# a) doesn't create a log file
# b) doesn't send an email notification
# c) sets the exit code to the number of errors

use strict;
use warnings;

use FindBin;
use lib "$FindBin::Bin/../lib";
use File::Basename qw{basename};

use FCM::Admin::Config;
use FCM::Admin::Runner;
use FCM::Admin::System qw{
    backup_svn_repository
    backup_trac_environment
    backup_trac_files
    get_projects_from_svn_live
    get_projects_from_trac_live
    get_users
    housekeep_svn_hook_logs
    manage_users_in_svn_passwd
    manage_users_in_trac_passwd
    manage_users_in_trac_db_of
};

my $THIS = basename($0);
my $CONFIG = FCM::Admin::Config->instance();
my $UTIL = $FCM::Admin::Config::UTIL;

if (!caller()) {
    main(@ARGV);
}

sub main {
    local(@ARGV) = @_;

    do_tasks();
    my @exceptions = FCM::Admin::Runner->instance()->get_exceptions();
    printf(qq{$THIS finished with %d error(s) \n}, scalar(@exceptions));
    exit(scalar(@exceptions));
}

# ------------------------------------------------------------------------------
# Performs the daily update tasks.
sub do_tasks {
    # (no argument)
    my $RUNNER = FCM::Admin::Runner->instance();
    my @svn_projects = get_projects_from_svn_live();
    my @trac_projects = get_projects_from_trac_live();
    my $user_ref = undef;
    $RUNNER->run_continue(
        "retrieving user accounts",
        sub {$user_ref = get_users(); 1;},
    );
    if (defined($user_ref)) {
        if ($CONFIG->get_svn_passwd_file()) {
            $RUNNER->run_continue(
                "updating SVN user accounts",
                sub {manage_users_in_svn_passwd($user_ref)},
            );
        }
        if ($CONFIG->get_trac_passwd_file()) {
            $RUNNER->run_continue(
                "updating Trac user accounts",
                sub {manage_users_in_trac_passwd($user_ref)},
            );
        }
        for my $project (@trac_projects) {
            $RUNNER->run_continue(
                "updating Trac accounts in $project",
                sub {manage_users_in_trac_db_of($project, $user_ref)},
            );
        }
    }
    for my $project (@svn_projects) {
        $RUNNER->run_continue(
            "housekeep SVN repository logs for $project",
            sub {housekeep_svn_hook_logs($project)},
        );
        $RUNNER->run_continue(
            "backing up SVN repository for $project",
            sub {backup_svn_repository({}, $project)},
        );
    }
    for my $project (@trac_projects) {
        $RUNNER->run_continue(
            "backing up Trac environment for $project",
            sub {backup_trac_environment({}, $project)},
        );
    }
    $RUNNER->run_continue("backing up Trac files", \&backup_trac_files);
}

__END__

=head1 NAME

fcm-daily-update

=head1 SYNOPSIS

    fcm-daily-update

=head1 DESCRIPTION

This program performs the daily update for the FCM system.

=head1 COPYRIGHT

Copyright (C) 2006-2021 British Crown (Met Office) & Contributors.

=cut
