#!/usr/bin/perl
# cpanel - autofixer2/fix_cpupdate_conf           Copyright(c) 2011 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 $cpupdate_conf = '/etc/cpupdate.conf';
my @lines;
my $cpanel_setting;

if ( open CPUPCONF_FH, '+<', $cpupdate_conf ) {
    while ( my $line = readline(CPUPCONF_FH) ) {
        chomp $line;
        if ( $line =~ m/^CPANEL=/ ) {
            undef $cpanel_setting;    # Reset just in case we already encountered a value
            if ( $line =~ m/^CPANEL=(\S+)/ ) {
                $cpanel_setting = $1;
            }
        }
        else {
            push @lines, $line;
        }
    }
    seek( CPUPCONF_FH, 0, 0 );
}
elsif ( !-e $cpupdate_conf ) {
    open CPUPCONF_FH, '>', $cpupdate_conf or die "Failed to create $cpupdate_conf: $!";
}
else {
    die "Unable to open $cpupdate_conf: $!";
}

if ( !$cpanel_setting ) {
    push @lines, 'CPANEL=release';
    print CPUPCONF_FH join( "\n", @lines ) . "\n";
    truncate( CPUPCONF_FH, tell(CPUPCONF_FH) );
}
close(CPUPCONF_FH);
