#!/usr/bin/perl -w

# Form implementation generated from reading ui file 'installprogressdlg.ui'
#
# Created: Fri Dec 16 17:50:40 2005
#      by: The PerlQt User Interface Compiler (puic)
#
# WARNING! All changes made in this file will be lost!


use strict;
use utf8;


package installProgressDlg;
use Config;
use Cwd;
use threads;
use threads::shared;
use Qt;
use Qt::debug qw|verbose ambiguous calls|;
use Qt::isa qw( Qt::Dialog );
use Qt::attributes qw(
    progressGroup
    instProgrsTxtBox
    currentPackageLbl
    cPackagesPBar1
    totalProgressLbl
    tPackagesRBar_
    exitInstallPrematurelyBttn
);

BEGIN {
    $Config{useithreads} or die "Recompile Perl with threads to run this program.";
}

sub NEW
{
    shift->SUPER::NEW(@_[0..3]);

    if ( name() eq "unnamed" )
    {
        setName("installProgressDlg" );
    }
    setSizePolicy(Qt::SizePolicy(7, 7, 100, 100, this->sizePolicy()->hasHeightForWidth()) );
    setMinimumSize(Qt::Size(600, 460) );
    setSizeGripEnabled(0 );


    progressGroup = Qt::GroupBox(this, "progressGroup");
    progressGroup->setGeometry( Qt::Rect(10, 0, 580, 470) );

    my $LayoutWidget = Qt::Widget(progressGroup, '$LayoutWidget');
    $LayoutWidget->setGeometry( Qt::Rect(10, 20, 561, 440) );
    my $layout3 = Qt::VBoxLayout($LayoutWidget, 10, 5, '$layout3');

    instProgrsTxtBox = Qt::TextBrowser($LayoutWidget, "isntProgrsTxtBox");
    instProgrsTxtBox->setCursor( Qt::Cursor(4) );
    instProgrsTxtBox->setFrameShape( &Qt::TextBrowser::Box() );
    instProgrsTxtBox->setFrameShadow( &Qt::TextBrowser::Sunken() );
    instProgrsTxtBox->setDragAutoScroll( 1 );
    instProgrsTxtBox->setTextFormat( &Qt::TextBrowser::RichText() );
    instProgrsTxtBox->setLinkUnderline( 0 );
    instProgrsTxtBox->setWordWrap( &Qt::TextBrowser::NoWrap() );
    instProgrsTxtBox->setWrapPolicy( &Qt::TextBrowser::AtWordBoundary() );
    $layout3->addWidget(instProgrsTxtBox);

    my $layout1 = Qt::VBoxLayout(undef, 0, 5, '$layout1');

    currentPackageLbl = Qt::Label($LayoutWidget, "currentPackageLbl");
    $layout1->addWidget(currentPackageLbl);

    cPackagesPBar1 = Qt::ProgressBar($LayoutWidget, "cPackagesPBar1");
    $layout1->addWidget(cPackagesPBar1);

    totalProgressLbl = Qt::Label($LayoutWidget, "totalProgressLbl");
    $layout1->addWidget(totalProgressLbl);

    tPackagesRBar_ = Qt::ProgressBar($LayoutWidget, "tPackagesRBar_");
    $layout1->addWidget(tPackagesRBar_);
    $layout3->addLayout($layout1);

    my $layout2 = Qt::HBoxLayout(undef, 0, 5, '$layout2');
    my $spacer = Qt::SpacerItem(430, 20, &Qt::SizePolicy::Expanding, &Qt::SizePolicy::Minimum);
    $layout2->addItem($spacer);

    exitInstallPrematurelyBttn = Qt::PushButton($LayoutWidget, "exitInstallPrematurelyBttn");
    $layout2->addWidget(exitInstallPrematurelyBttn);
    $layout3->addLayout($layout2);
    languageChange();
    my $resize = Qt::Size(600, 480);
    $resize = $resize->expandedTo(minimumSizeHint());
    resize( $resize );
    clearWState( &Qt::WState_Polished );
}


#  Sets the strings of the subwidgets using the current
#  language.

sub languageChange
{
    setCaption(trUtf8("Installation Progress") );
    progressGroup->setTitle( trUtf8("Installation Progress") );
    currentPackageLbl->setText( trUtf8("Current Package:") );
    totalProgressLbl->setText( trUtf8("Total Progress:") );
    exitInstallPrematurelyBttn->setText( trUtf8("&Cancel Install") );
    exitInstallPrematurelyBttn->setAccel( Qt::KeySequence( trUtf8("Alt+C") ) );
}

sub appendMessage {
    chomp( my $inputMsg = $_[ 0 ] );
    print STDOUT "  = ",$inputMsg,"\n";
    instProgrsTxtBox->append( "$inputMsg" );
    Qt::Application::processEvents();
}

sub rpmDbInit {
    sleep 1;
    &appendMessage( "Create RPM base directories...." );
    mkdir "/DEST/var/", 0755;
    mkdir "/DEST/var/lib", 0755;
    mkdir "/DEST/var/lib/rpm", 0755;
    mkdir "/DEST/var/lock", 0755;
    mkdir "/DEST/var/lock/rpm", 0755;
    mkdir "/DEST/var/tmp", 1777;

    sleep 1;
    &appendMessage( "Create RPM Berkley v4 DB...." );
    system( "/bin/rpm --root /DEST --initdb" );
}

sub installBase {
    chomp( my $argsList = $_[ 0 ] );
    our @packages;
    our $tempPkg;
    share( @packages );
    share( $tempPkg );
    opendir( RPMS, "/RPMS/basesystem" ) or warn "Cannot open directory for reading: $!\n";
    my $i = 0;
    chomp( @packages = readdir( RPMS ) );
    closedir( RPMS );

    my @retval = ();

    if ( $argsList eq "noscripts" ) {
        &appendMessage( "Preparing base operating system." );
    } else {
        &appendMessage( "Installing base operating system." );
    }

    if ( $argsList eq "noscripts" ) {
        for ( my $i = 0; $i < @packages; $i++ ) {
            next if ( $packages[ $i ] eq "." || $packages[ $i ] eq ".." );
            &appendMessage( "Pre-installing: $packages[ $i ] binaries" );
            our $tempPkg = $packages[ $i ];
            my $passOneBaseInstaller = threads->create( \&firstPassBaseInstaller, $tempPkg );
            print "Outside thread.\n";
            @retval = $passOneBaseInstaller->join;
        }
    } else {
        my @basePackages = ();
        my $dItor = 0;
        for ( my $j = 0; $j < @packages; $j++ ) {
            next if ( $packages[ $j ] eq "." || $packages[ $j ] eq ".." );
            $basePackages[ $dItor ] = "/RPMS/basesystem/".$packages[ $j ]." ";
            $dItor++;
        }

        print STDOUT @basePackages,"\n";
        my $passTwoBaseInstaller = threads->new( \&secPassBaseInstaller, @basePackages );
        @retval = $passTwoBaseInstaller->join;
    }
}

sub firstPassBaseInstaller {
    chomp( my $package = $_[ 0 ] );
    print "INSIDE THREAD.\n";
    system( "rpm -i --nodeps --noscripts --notriggers --excludedocs --root /DEST /RPMS/basesystem/$package" );
    print "PACKAGE ( $package ) Installed.\n";
    $package = undef;
}

sub secPassBaseInstaller {
    chomp( my @packages = @_ );
    print "INSIDE THREAD.\n";
    system( "rpm -ivh --replacefiles --replacepkgs @packages --root /DEST" );
}


1;


package main;

use Qt;
use installProgressDlg;

my $a = Qt::Application(\@ARGV);
my $w = installProgressDlg;
$a->setMainWidget($w);
$w->show();

Qt::Application::processEvents();

$w->rpmDbInit();
$w->installBase( 'noscripts' );

exit $a->exec();
