#!/usr/bin/perl
# cpanel - network_manager                        Copyright(c) 2016 cPanel, Inc.
#                                                           All rights Reserved.
# copyright@cpanel.net                                         http://cpanel.net
# This code is subject to the cPanel license. Unauthorized copying is prohibited

use strict;

my $flag_file = '/var/cpanel/version/network_manager';

sub we_have_not_updated {
    return 1 if ( !-e $flag_file );
    return 0;
}

# Log lines will look something like:
# [2016-02-15 20:53:30 +0000]      Installing : 1:NetworkManager-1.0.6-27.el7.x86_64
sub grep_update_log {
    my @where = qw( /var/cpanel/updatelogs );
    push @where, '/var/log/cpanel-install.log' if ( -e '/var/log/cpanel-install.log' );
    return 0 == system( 'grep', '-rq', 'Install.*NetworkManager-', @where );
}

sub disable_networkmanager {
    return 0 == system( 'systemctl', 'disable', 'NetworkManager' );
}

sub set_updated {
    system( 'touch', $flag_file );
    return;
}

sub script {
    if ( we_have_not_updated() ) {
        if ( !-x '/bin/systemctl' ) {

            # CentOS 6 and earlier don't even have systemctl, so we'll
            # just turn this off.
            set_updated();
        }
        else {
            # We do not want to create the updated flag unless we
            # actually make the update - we may be on a lower version,
            # and NetworkManager may be removed later.
            grep_update_log() && disable_networkmanager() && set_updated();
        }
    }
    return 0;
}

exit script() unless caller;
