#!/usr/bin/env perl
# lshw-gtk.wrapper - Wrapper for "lshw-gtk"
# License:  BSD-style [for this file only]
# Revision: 070831

#---------------------------------------------------------------------
#                              overview
#---------------------------------------------------------------------

# 1. Overview.

# This is a wrapper for the main "lshw-gtk" executable  [which must be
# renamed to "lshw-gtk.bin"]. This program serves three purposes:
#
#     a. The wrapper  opens and  closes the  CD/DVD drive [if any] at-
#        tached to "/dev/hdc".  This fixes a  problem that caused  the
#        target program to lock-up occasionally.
#
#     b. The wrapper displays a dialog box, which advises  the user of
#        some  possible problems and  gives him/her  a chance to exit.
#        If the user decides  to  continue, the wrapper chains  to the
#        target  program  [passing  any/all command-line arguments un-
#        changed].
#
#     c. The wrapper doesn't chain directly to the target program.  It
#        uses  "nice" *and*  "ionice" to  run the program.  This fixes
#        another lock-up problem.

#---------------------------------------------------------------------

# 2. Requirements [excluding the target program].

# This wrapper requires  Perl, Zenity, "nice", "ionice", and a distro-
# specific version of "eject".  Note: You should be able to obtain the
# latter program from the same place as this file.

#---------------------------------------------------------------------
#                            module setup
#---------------------------------------------------------------------

require 5.6.1;
use strict;
use Carp;
use warnings;
                                # Trap warnings
$SIG {__WARN__} = sub { die @_; };

#---------------------------------------------------------------------
#                           basic constants
#---------------------------------------------------------------------

use constant ZERO  => 0;        # Zero
use constant ONE   => 1;        # One

use constant FALSE => 0;        # Boolean FALSE
use constant TRUE  => 1;        # Boolean TRUE

#---------------------------------------------------------------------
#                         program parameters
#---------------------------------------------------------------------

my $TARGET_PROGRAM = 'lshw-gtk.bin';

#---------------------------------------------------------------------
#                            main routine
#---------------------------------------------------------------------

sub Main
{
    my $cmd_dialog;             # Shell-level "command" string
    my $msg;                    # Message string
    my $title;                  # Title   string

#---------------------------------------------------------------------
# Prepare the dialog.

    $msg = << 'END';
1. Warning: The hardware browser will try to open your CD or
DVD drive. Move any obstructions out of the way!
__NL__
__NL__
2. Close as many programs as possible before proceeding.
__NL__
__NL__
3. Allow three or four minutes for the hardware browser to run.
If it takes much longer than that, run the Task Manager and
terminate the lshw-gtk.bin process.
__NL__
__NL__
Select OK to run the hardware browser __NL__
Select Cancel to exit
END
    $msg   =~ s@\s+@ @gs;
    $msg   =~ s@\s*\z@\n@;
    $title =  "lshw warnings\n";

    $cmd_dialog =  << "END";
zenity --question --title="$title" --text="$msg"
END
    $cmd_dialog =~ s@\s*\n\s*@ @gs;
    $cmd_dialog =~ s@__NL__@\n@g;
    $cmd_dialog =~ s@\n +@\n@g;

#---------------------------------------------------------------------
# Run the dialog.

    my $n = system $cmd_dialog;

#---------------------------------------------------------------------
# Wrap it up.

    if ($n)                     # Abort?
    {                           # Yes
        exit ONE;
    }
    else
    {                           # No
        system << 'END';        # See notes near start of this file
eject --open  /dev/hdc >& /dev/null
eject --close /dev/hdc >& /dev/null
END
                                # Run the target program
        exec qw (nice -n 10 ionice -c2 -n7), $TARGET_PROGRAM, @ARGV;
    }

    undef;
}

#---------------------------------------------------------------------
#                            main program
#---------------------------------------------------------------------

&Main();                        # Call the main routine
exit ONE;                       # Shouldn't be reached
