#!/usr/bin/perl -w

eval 'exec /usr/bin/perl -w -S $0 ${1+"$@"}'
    if 0; # not running under some shell

eval 'exec /usr/bin/perl  -S $0 ${1+"$@"}'
    if 0; # not running under some shell
#----------------------------------------------------------------------
#      ___       ___ ___   P X E S   Universal  Linux  Thin  Client
#     /__/\\_// /__ /__    Copyright(C) 2003 by Diego Torres Milano
#    /    // \\/__  __/    All rights reserved.  http://pxes.sf.net
#
# Author: Diego Torres Milano <diego@pxes.com.ar>
# $Id: pxesconfig,v 1.36 2004/08/06 04:01:54 diego Exp $
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
# USA
#----------------------------------------------------------------------

#======================================================================
#=== This is the main script of pxesconfig
#======================================================================
require 5.000; use strict 'vars', 'refs', 'subs';

BEGIN {
	use constant TRUE => 1;
	use constant FALSE => 0;

   use vars qw(@ISA @EXPORT $PROGNAME $VERSION $PERLVERSION);
	use vars qw(@options $longversion $dumpconfiguration $configuration);
	use vars qw($stockdir $initrd $nonrootinitialize $nowarnings);
	use vars qw($archdir $extradir $distdir);
	use vars qw($initialize $dontreadconfiguration $druid $isolivefs);
	use vars qw(@include @install @isoinclude @isoinstall $isopxes);
	use vars qw($postconfig);
	use vars qw($iso $nbi $squashed);
	use vars qw($kernel $arch);
	our $PXES_VERSION;
	our $PXES_RELEASE;

	@ISA = qw(Exporter);
	@EXPORT = qw($PXES_VERSION $PXES_RELEASE);

	$PROGNAME = 'pxesconfig';
	$VERSION = '0.9';
	$PXES_VERSION = '0.9';
	$PXES_RELEASE = '1 (marlin)';
	
	# this fix Glade-Perl-0.60-1 rpm problem on RedHat 8.0
	use lib "/usr/lib/perl5/site_perl/5.6.0";

	# this fix is for RedHat 7.x, AS 2.x
	use lib "/usr/lib/perl5/site_perl/5.8.0";

	# this fix is for RedHat 9, Fedora Core 1
	use lib "/usr/lib/perl5/site_perl/5.8.1";

	#use lib ".";
	#warn "Lib set for development";

   use Pxesconfig::Pxesconfig;

#  use Carp qw(cluck);
#       $SIG{__DIE__}  = &Carp::confess;
#       $SIG{__WARN__} = &Carp::cluck;
}

@options = (
	# argspec => description
	# argspec = long_arg_name[|single_char_name][=s|i|f] (see Getopt::Long)
	"longversion|V", 			"Get the long version message",
	"dumpconfiguration|d=s",	"Dump the default configuration file",
	"configuration|c=s",		"Use default configuration file",
	"stockdir|s=s",			"Use the specified stock dir",
	"archdir=s",			   "Use the specified arch dir",
	"kernel|k=s",				"Use kernel as the desired kernel",
	"initrd|i=s",				"Use initrd as the ramdisk image",
	"arch|a=s",					"Use architecture arch dependent files",
	"druid=s",					"Use graphical druid (enabled by default)",
	"iso=s",						"Build the specified ISO image",
	"nbi=s",						"Build the specified NBI image",
	"squashed=s",				"Build the specified squashed image",
	"include=s",				"Include additional directories in image",
	"isoinclude=s",			"Include additional directories in ISO image",
	"install=s",				"Install additional packages in image",
	"isoinstall=s",			"Install additional packages in ISO image",
	"isopxes=s",				"Base ISO image",
	"isolivefs",				"Inclue ISO live fs in image",
	"initialize",				"Initialize the ram disk",
	"dontreadconfiguration",	"Don't read previous configuration",
	"nonrootinitialize",		"Allow non-root users to initialize ramdisk",
	"nowarnings",				"Don't show warning messages",
	"postconfig=s",			"Call a postconfig porgram",
);

$longversion = '';
$dumpconfiguration = '';
$configuration = '';
$stockdir = undef;
$archdir = undef;
$kernel = '';
$initrd = '';
$arch = 'i586';	# default arch
$nonrootinitialize = 0;
$nowarnings = 0;
$initialize = 0;
$dontreadconfiguration = 0;
$druid = 1;
@include = ();
@install = ();
@isoinclude = ();
@isoinstall = ();
$isopxes = undef;
$iso = undef;
$isolivefs = 0;
$nbi = undef;
$squashed = undef;

sub options {
	my ($name, $val) = @_;
	$val = '' unless defined $val;

	#warn "options: name=$name val=$val";
	longversion() if $name eq 'longversion';
	$dumpconfiguration = $val if $name eq 'dumpconfiguration';
	$configuration = $val if $name eq 'configuration';
	$stockdir = $val if $name eq 'stockdir';
	$archdir = $val if $name eq 'archdir';
	$kernel = $val if $name eq 'kernel';
	$initrd = $val if $name eq 'initrd';
	$arch = $val if $name eq 'arch';
	$nonrootinitialize = 1 if $name eq 'nonrootinitialize';
	$nowarnings = 1 if $name eq 'nowarnings';
	$initialize = 1 if $name eq 'initialize';
	$isolivefs = 1 if $name eq 'isolivefs';
	$dontreadconfiguration = 1 if $name eq 'dontreadconfiguration';
	$druid = $val if $name eq 'druid';
	push(@include, $val) if $name eq 'include';
	push(@install, $val) if $name eq 'install';
	push(@isoinclude, $val) if $name eq 'isoinclude';
	push(@isoinstall, $val) if $name eq 'isoinstall';
	$isopxes = $val if $name eq 'isopxes';
	$iso = $val if $name eq 'iso';
	$nbi = $val if $name eq 'nbi';
	$squashed = $val if $name eq 'squashed';
}

sub longversion() {
	print "$PROGNAME version $VERSION\n";
	print '$Id: pxesconfig,v 1.36 2004/08/06 04:01:54 diego Exp $', "\n";
	print 'by Diego Torres Milano <diego@pxes.com.ar>',"\n";
	print 'Copyright(C) 2001-2004.  All rights reserved.', "\n";
	print <<EOF;

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA


EOF

	exit(0);
}

# initialize gnome

Gnome->init("$PROGNAME", "$VERSION", {
		callback => \&options,
		remove => 1, #remove options from the command line
		options => [@options]
		}
	);

select STDOUT; $| = 1;

my %params = (
);

my $p = new Pxesconfig($configuration);
$p->{model}->kernel_arch($arch);

if ( $stockdir ) {
	$p->{model}->stock_dir($stockdir);
	$p->{model}->arch_dir($stockdir . '/arch');
	$p->{model}->kernel($p->{model}->find_kernel($arch))
		if ( ! $kernel );
}

if ( $archdir ) {
	$p->{model}->arch_dir($archdir);
	$p->{model}->kernel($p->{model}->find_kernel($arch))
		if ( ! $kernel );
}

$p->{model}->initrd($initrd) if ( $initrd );
$p->{model}->isopxes($isopxes) if ( $isopxes );

$p->{model}->initial_checks();

if ( $dumpconfiguration ) {
	if ( $initrd ) {
		$p->{model}->read_configuration(! $dontreadconfiguration);
		if ( $p->{model}->initial() < 0 ) {
			warn "Problems reading configuration from $initrd";
		}
		if ( $p->{model}->umount_initrd() < 0 ) {
			warn "Problems unmounting $initrd";
		}
	}
	exit($p->{model}->dump_configuration($dumpconfiguration));
}

$p->{model}->kernel($kernel) if ( $kernel );

$p->{model}->initialize(TRUE) if $initialize;
$p->{model}->read_configuration(FALSE) if $dontreadconfiguration;
$p->{model}->non_root_initialize($nonrootinitialize);
$p->{model}->no_warnings($nowarnings);
$p->{model}->mkiso_livefs(TRUE) if $isolivefs;
$p->{model}->include(\@include) if @include;
$p->{model}->install(\@install) if @install;
$p->{model}->isoinclude(\@isoinclude) if @isoinclude;
$p->{model}->isoinstall(\@isoinstall) if @isoinstall;
$p->{model}->postconfig($postconfig) if $postconfig;

if ( $nbi ) {
	if ( $nbi ne 'no' ) {
		$p->{model}->mknbi(TRUE);
		$p->{model}->nbi($nbi);
	}
	else {
		$p->{model}->mknbi(FALSE);
	}
}

if ( $iso ) {
	if ( $iso ne 'no' ) {
		$p->{model}->mkiso(TRUE);
		$p->{model}->iso($iso);
	}
	else {
		$p->{model}->mkiso(FALSE);
	}
}

if ( $squashed ) {
	if ( $squashed ne 'no' ) {
		$p->{model}->mksquashed(TRUE);
		$p->{model}->squashed($squashed);
	}
	else {
		$p->{model}->mksquashed(FALSE);
	}
}

if ( ! $druid || grep(/$druid/i, ('no', 'off', 'false')) ) {
	if ( $p->{model}->finish('text') < 0 ) {
		print STDERR "pxesconfig: ERROR generating image.\n";
		print STDERR $p->{model}->error_message(),"\n"
			if $p->{model}->error_message();
		$p->{model}->umount_initrd();
		exit(1);
	}
	exit(0);
}

# normal druid operation

# FIXME
#my $icon = $p->{view}->create_image('pxes-tiny.png');
#my $window = $p->{view};
#$window->TOPLEVEL->set_title("pxesconfig " . $VERSION);
#$window->TOPLEVEL->????->set_icon(undef, $icon , "0000");

$p->{model}->changed(FALSE);
$p->{view}->app_run(%params) || exit 1;

exit(0);

1;

__END__

#=======================================================================
#==== Documentation
#=======================================================================
=pod

=head1 NAME

pxesconfig - version 0.8 	           Fri Feb 20 00:18:05 ART 2004

This is the PXES druid configuration utility

=head1 SYNOPSIS

pxesconfig [OPTION...]
 
GNOME options
  --disable-sound               Disable sound server usage
  --enable-sound                Enable sound server usage
  --espeaker=HOSTNAME:PORT      Host:port on which the sound server to use is
                                running
  --version
 
Help options
  -?, --help                    Show this help message
  --usage                       Display brief usage message
 
GTK options
  --gdk-debug=FLAGS             Gdk debugging flags to set
  --gdk-no-debug=FLAGS          Gdk debugging flags to unset
  --display=DISPLAY             X display to use
  --sync                        Make X calls synchronous
  --no-xshm                     Don't use X shared memory extension
  --name=NAME                   Program name as used by the window manager
  --class=CLASS                 Program class as used by the window manager
  --gxid_host=HOST
  --gxid_port=PORT
  --xim-preedit=STYLE
  --xim-status=STYLE
  --gtk-debug=FLAGS             Gtk+ debugging flags to set
  --gtk-no-debug=FLAGS          Gtk+ debugging flags to unset
  --g-fatal-warnings            Make all warnings fatal
  --gtk-module=MODULE           Load an additional Gtk module
 
GNOME GUI options
  --disable-crash-dialog
 
Session management options
  --sm-client-id=ID             Specify session management ID
  --sm-config-prefix=PREFIX     Specify prefix of saved configuration
  --sm-disable                  Disable connection to session manager
 
pxesconfig options
  -V, --longversion             Get the long version message
  -d, --dumpconfiguration=ARG   Dump the default configuration file
  -c, --configuration=ARG       Use default configuration file
  -s, --stockdir=ARG            Use the specified stock dir
  --archdir=ARG                 Use the specified arch dir
  -k, --kernel=ARG              Use kernel as the desired kernel
  -i, --initrd=ARG              Use initrd as the ramdisk image
  -a, --arch=ARG                Use architecture arch dependent files
  --druid=ARG                   Use graphical druid (enabled by default)
  --iso=ARG                     Build the specified ISO image
  --nbi=ARG                     Build the specified NBI image
  --include=ARG                 Include additional directories in image
  --isoinclude=ARG              Include additional directories in ISO image
  --install=ARG                 Install additional packages in image
  --isoinstall=ARG              Install additional packages in ISO image
  --isopxes=ARG                 Base ISO image
  --initialize                  Initialize the ram disk
  --dontreadconfiguration       Don't read previous configuration
  --nonrootinitialize           Allow non-root users to initialize ramdisk
  --nowarnings                  Don't show warning messages
  --postconfig=ARG              Call a postconfig porgram

=head1 DESCRIPTION

Pxesconfig is the druid (wizard) configuration tools of PXES.
It simplyfies the initial ram disk creation, attribute configuration,
hardware dependencies and kernel selection.

=head1 AUTHOR

Diego Torres Milano <diego@pxes.com.ar>

=cut
