#!/usr/bin/perl
# $Id: cce_construct_dialog
#
# the much simpler sysreset
# it's like the old one, but it makes cce do all of the hard work.
#
# syntax: $0 <type>
# where type can be one of 
#   sysinit
#   sysreset
#   [nothing]
#
# This scripts has been rewritten to use Dialog for a nicer presentation of the startup constructor run.
# We use it for the CD-Installer of Aventurine and BlueOnyx.

my $consdir = '/usr/sausalito/constructor';
my $destroydir = '/usr/sausalito/destructor';
my $preferdir = 'base';

system("/usr/bin/logger", "cce_construct started");

sub get_files
{
    my ($dir, $allowpat, $excludepat) = @_;
    my $name;
    my %hash, %dirhash;
    local *DIR;

    if (opendir(DIR, $dir)) {
        while ($_ = readdir(DIR)) {
            next if /^\./; # always ignore stuff starting with .
            $name = "$dir/$_";
            if (-d $name) {
                %dirhash = get_files($name, $allowpat, $excludepat);
                foreach $name (keys %dirhash) {
                    $hash{$name} = $dirhash{$name};
                }
            } 
            else {
                next if $allowpat and not /^$allowpat/;
                next if $excludepat and /^$excludepat/;
                $hash{"$_$dir"} = $name;
            }
        }
        closedir(DIR);
    }
    return %hash;
}

my %hash;
my ($key, $allowpat, $excludepat);

# if this were really working, it would go through the database
# and destroy all of the objects in it before running the 
# destructors.  
while ($ARGV[0] =~ /^\-/) {
    if ($ARGV[0] eq '-f') {
        print "i'm pretending to reset the machine to factory defaults.\n";
    }

    shift;
}

if ($ARGV[0]) {
    $allowpat = ':' . $ARGV[0] . ':';
}
else {
    $excludepat = ':';
}

$CDTITLE="Team BlueOnyx";
$TITLE="BlueOnyx 5212R: CCEd Constructor";

# We should run the Constructor that creates the 'System' Object first of all.
# Too much stuff depends on the 'System' Object being there.
if (-f "/usr/sausalito/constructor/base/system/10_addSystem.pl") {
    system("/usr/sausalito/constructor/base/system/10_addSystem.pl")
}

# running constructors
# run the base one first 
%hash = get_files("$consdir/$preferdir", $allowpat, $excludepat);
if (%hash) {
    foreach $key (sort keys %hash) {
        #system("echo percentage | dialog --nocancel --backtitle \"$CDTITLE\" --title \"$TITLE\" --gauge \"text\" height width percent");
        system("echo \"10\" | dialog --nocancel --backtitle \"$CDTITLE\" --title \"$TITLE\" --gauge \"Running CCE Constructor:\n$hash{$key}\" 10 70 0");

        if ($hash{$key} eq "/usr/sausalito/constructor/base/email/syncEmailService.pl") {
            system("echo \"45\" | dialog --nocancel --backtitle \"$CDTITLE\" --title \"$TITLE\" --gauge \"Running CCE Constructor: (This one will take some time!)\n$hash{$key}\" 10 70 0");
        }
        else {
            system("echo \"45\" | dialog --nocancel --backtitle \"$CDTITLE\" --title \"$TITLE\" --gauge \"Running CCE Constructor:\n$hash{$key}\" 10 70 0");
        }
        system('/usr/bin/logger', "***** cce_construct: $hash{$key}");
        `$hash{$key} /dev/null 2>&1 |pv -l -q | dialog --nocancel --backtitle \"$CDTITLE\" --title \"$TITLE\" --gauge \"Running CCEd constructors, please wait..." 10 70 0`;
        if ($hash{$key} eq "/usr/sausalito/constructor/base/email/syncEmailService.pl") {
            system("echo \"100\" | dialog --nocancel --backtitle \"$CDTITLE\" --title \"$TITLE\" --gauge \"Running CCE Constructor: (This one will take some time!)\n$hash{$key}\" 10 70 0");
        }
        else {
            system("echo \"100\" | dialog --nocancel --backtitle \"$CDTITLE\" --title \"$TITLE\" --gauge \"Running CCE Constructor:\n$hash{$key}\" 10 70 0");
        }
    }
}

# now, collate the rest of the directories and run
# them in alphabetical order.
my (@dirs, $dir);
if (opendir(PDIR, $consdir)) {
    while ($_ = readdir(PDIR)) {
        next if /^\./;
        next if /^$preferdir$/;
        push @dirs, $_;
    }
}

foreach $dir (sort @dirs) {
    %hash = get_files("$consdir/$dir", $allowpat, $excludepat);
    if (%hash) {
        foreach $key (sort keys %hash) {
            
            #system("echo percentage | dialog --nocancel --backtitle \"$CDTITLE\" --title \"$TITLE\" --gauge \"text\" height width percent");
            system("echo \"10\" | dialog --nocancel --backtitle \"$CDTITLE\" --title \"$TITLE\" --gauge \"Running CCE Constructor:\n$hash{$key}\" 10 70 0");
        
            if ($hash{$key} eq "/usr/sausalito/constructor/base/email/syncEmailService.pl") {
                system("echo \"45\" | dialog --nocancel --backtitle \"$CDTITLE\" --title \"$TITLE\" --gauge \"Running CCE Constructor: (This one will take some time!)\n$hash{$key}\" 10 70 0");
            }
            else {
                system("echo \"45\" | dialog --nocancel --backtitle \"$CDTITLE\" --title \"$TITLE\" --gauge \"Running CCE Constructor:\n$hash{$key}\" 10 70 0");
            }
            system('/usr/bin/logger', "***** cce_construct: $hash{$key}");
            `$hash{$key} /dev/null 2>&1 |pv -l -q | dialog --nocancel --backtitle \"$CDTITLE\" --title \"$TITLE\" --gauge \"Running CCEd constructors, please wait..." 10 70 0`;
            if ($hash{$key} eq "/usr/sausalito/constructor/base/email/syncEmailService.pl") {
                system("echo \"100\" | dialog --nocancel --backtitle \"$CDTITLE\" --title \"$TITLE\" --gauge \"Running CCE Constructor: (This one will take some time!)\n$hash{$key}\" 10 70 0");
            }
            else {
                system("echo \"100\" | dialog --nocancel --backtitle \"$CDTITLE\" --title \"$TITLE\" --gauge \"Running CCE Constructor:\n$hash{$key}\" 10 70 0");
            }
        }
    }
}
system("/usr/bin/logger", "***** cce_construct finished");

# 
# Copyright (c) 2016-2025 Michael Stauber, SOLARSPEED.NET
# All Rights Reserved.
# 
# 1. Redistributions of source code must retain the above copyright 
#     notice, this list of conditions and the following disclaimer.
# 
# 2. Redistributions in binary form must reproduce the above copyright 
#     notice, this list of conditions and the following disclaimer in 
#     the documentation and/or other materials provided with the 
#     distribution.
# 
# 3. Neither the name of the copyright holder nor the names of its 
#     contributors may be used to endorse or promote products derived 
#     from this software without specific prior written permission.
# 
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 
# COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
# POSSIBILITY OF SUCH DAMAGE.
# 
# You acknowledge that this software is not designed or intended for 
# use in the design, construction, operation or maintenance of any 
# nuclear facility.
# 