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

use lib '/scripts';
use strict;
use warnings;
use POSIX                  ();
use cPScript::ProcessCheck ();

$| = 1;

if ( !-e '/usr/local/cpanel/etc/exim/cf/virtual_aliases' ) {
    print "Integrity check failed.  Running auto update & repair.. please wait...";
    if ( my $pid = fork() ) {
        # Parent
    }
    else {
        my $upcppidcount = -1;
        while ( $upcppidcount != 0 ) {
            print "Checking for a running upcp ... ";
            my %PPIDS = cPScript::ProcessCheck::previouspids( 'process' => 'upcp' );
            delete $PPIDS{$$};
            $upcppidcount = scalar keys %PPIDS;
            if ($upcppidcount) {
                print "still running ... waiting 60 seconds ... ";
                sleep 60;
            }
            else {
                print "no running upcp ...";
            }
        }

        if ( -e '/usr/local/cpanel/etc/exim/cf/virtual_aliases' ) {
            print "previous update already repaired install!\n";
            exit 0;
        }
        else {
            print "starting a forced update!\n";
            chdir '/';

            POSIX::setsid();
            for ( 3 .. 1024 ) {
                POSIX::close($_);
            }
            open( STDIN,  '<', '/dev/null' );
            open( STDOUT, '>', '/dev/null' );
            open( STDERR, '>', '/dev/null' );

            system '/scripts/upcp','--force', '--integrity-repair';
            system '/scripts/buildeximconf';
            system '/scripts/restartsrv', 'exim';        

            exit 0;
        }
    }
}
else {
    print "Integrity check passed. No update needed.\n";
}
