#!/usr/bin/perl

# $Id: wizdrake,v 1.8 2001/11/14 17:17:39 philippe Exp $

# Copyright (C) 2001 MandrakeSoft
# Yves Duret <yduret at mandrakesoft.com>
#
# 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, 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.

use MDK::Common;
use POSIX;
use Gtk;
use lib qw(/usr/lib/libDrakX);
use interactive;
use standalone;
use Config;
use Config;
use Getopt::Long;
use Data::Dumper;
init Gtk;
Gtk->set_locale;
use my_gtk qw(:helpers :wrappers);

$::isEmbedded = ($::XID, $::CCPID) = "@ARGV" =~ /--embedded (\w+) (\w+)/;
if ($::isEmbedded) {
  print "EMBED\n";
  print "parent XID\t$::XID\n";
  print "mcc pid\t$::CCPID\n";
}

my $in = 'interactive'->vnew('su');

local $_ = join '', @ARGV;

/-h/ and die sprintf( _("usage: wizdrake [--version]\n"));
/-version/ and die 'version: $Id: wizdrake,v 1.8 2001/11/14 17:17:39 philippe Exp $ '."\n";

my $trans = get_translation();
my $_wizdir = "/usr/share/wizards";

#  my %wizard = (
#  	      "client" => ["wiz $_wizdir/client_wizard/client.wiz", "wiz-net-mdk", "Adding a DNS entry", "This wizard adds a new client to your network."],
#  	      "dhcp" => ["wiz $_wizdir/dhcp_wizard/dhcp.wiz", "wiz-net-mdk", "DHCP Server", "This wizard configures a DHCP server."],
#  	      "dns" => ["wiz $_wizdir/dns_wizard/dns.wiz", "wiz-net-mdk", "Domain Name Server", "This wizard configures the Domain Name Server"],
#  	      "firewall" => ["wiz $_wizdir/firewall_wizard/firewall.wiz", "wiz-net-mdk", "Firewall", "This wizard set up a Firewall"],
#  	      "ftp" => ["wiz $_wizdir/ftp_wizard/ftp.wiz", "wiz-net-mdk", "Ftp Server", "This wizard configures a Ftp Server"],
#  	      "news" => ["wiz $_wizdir/news_wizard/news.wiz", "wiz-net-mdk", "News Server", "This wizard configures a News Server"],
#  	      "postfix" => ["wiz $_wizdir/postfix_wizard/postfix.wiz", "wiz-net-mdk", "Postfix Server", "This wizard configures the Postfix mail service"],
#  	      "samba" => ["wiz $_wizdir/samba_wizard/samba.wiz", "wiz-net-mdk", "Samba Server", "This wizard configures the Samba service"],
#  	      "server" => ["wiz $_wizdir/server_wizard/server.wiz", "wiz-net-mdk", "Network Configuration", "This wizard configures the network"],
#  	      "time" => ["wiz $_wizdir/time_wizard/time.wiz", "wiz-net-mdk", "Time Configuration", "This wizard sets the time of the machine"],
#  	      "web" => ["wiz $_wizdir/web_wizard/web.wiz", "wiz-net-mdk", "Web Server", "This wizard configures a web server"],
#  	     );

my %wizard = (
	      "client" => ["wiz $_wizdir/client_wizard/client.wiz", "wiz-net-mdk", $trans->{client}{NAME}, $trans->{client}{DESC}],
	      "dhcp" => ["wiz $_wizdir/dhcp_wizard/dhcp.wiz", "wiz-net-mdk", $trans->{dhcp}{NAME}, $trans->{dhcp}{DESC}],
	      "dns" => ["wiz $_wizdir/dns_wizard/dns.wiz", "wiz-net-mdk", $trans->{dns}{NAME}, $trans->{dns}{DESC}],
	      "firewall" => ["wiz $_wizdir/firewall_wizard/firewall.wiz", "wiz-net-mdk", $trans->{firewall}{NAME}, $trans->{firewall}{DESC}],
	      "ftp" => ["wiz $_wizdir/ftp_wizard/ftp.wiz", "wiz-net-mdk", $trans->{ftp}{NAME}, $trans->{ftp}{DESC}],
	      "news" => ["wiz $_wizdir/news_wizard/news.wiz", "wiz-net-mdk", $trans->{news}{NAME}, $trans->{news}{DESC}],
	      "postfix" => ["wiz $_wizdir/postfix_wizard/postfix.wiz", "wiz-net-mdk", $trans->{postfix}{NAME}, $trans->{postfix}{DESC}],
	      "proxy" => ["wiz $_wizdir/proxy_wizard/proxy.wiz", "wiz-net-mdk", $trans->{proxy}{NAME}, $trans->{proxy}{DESC}],
	      "samba" => ["wiz $_wizdir/samba_wizard/samba.wiz", "wiz-net-mdk", $trans->{samba}{NAME}, $trans->{samba}{DESC}],
	      "server" => ["wiz $_wizdir/server_wizard/server.wiz", "wiz-net-mdk", $trans->{server}{NAME}, $trans->{server}{DESC}],
	      "time" => ["wiz $_wizdir/time_wizard/time.wiz", "wiz-net-mdk", $trans->{time}{NAME}, $trans->{time}{DESC}],
	      "web" => ["wiz $_wizdir/web_wizard/web.wiz", "wiz-net-mdk", $trans->{web}{NAME}, $trans->{web}{DESC}],
	     );


my $window = $::isEmbedded ? new Gtk::Plug ($::XID) : new Gtk::Window ("toplevel");
$window->signal_connect( delete_event => sub { $::isEmbedded ? kill(USR1, $::CCPID) : Gtk->exit(0) });
$window->set_title( _("wizdrake") );
$window->set_policy(1, 1, 1);
$::isEmbedded or $window->border_width (10);
### menus definition
# the menus are not shown
# but they provides shiny shortcut like C-q
my @menu_items = ( 
		  { path => _("/_File"), type => '<Branch>' },
		  { path => _("/File")._("/_Quit"), accelerator => _("<control>Q"), callback => sub { $::isEmbedded ? kill(USR1, $::CCPID) : Gtk->exit(0) } },
		  { path => _("/_Help"),type => '<Branch>' },
		  { path => _("/Help")._("/_About..."), callback => \&print_hello},
		 );
my $menubar = get_main_menu( $window );
######### menus end
$window->realize;

my $v = new Gtk::VBox(0,5);
#remove common

my @a;
foreach (split('\n', `rpm -qa | grep wizards_lib-`)) {
    if(s/wizards_lib-(\w+)-(\d+).*/$1/) { push @a, chomp_($_) if member($1, (keys %wizard)) }
}

my $c = 0;
my @b;
my @d;
my $tooltips = new Gtk::Tooltips();
$window->{tooltips} = $tooltips;
foreach my $n (@a) {
    push @d, gtksignal_connect(gtkadd(
				      gtkset_tip($tooltips, new Gtk::Button(), $wizard{$n}->[3]),
				      gtkpack__(new Gtk::VBox(0,3),
						gtkpng($wizard{$n}->[1]),
						new Gtk::Label($wizard{$n}->[2])
					       )
				     ), clicked => sub { system($wizard{$n}->[0] . " &") }
			      );
    $c++;
    if ($c ==3) {
	push @b, [ @d ];
	$c = 0;
	@d= ();
    }
}
if (@d) {
    push @d, new Gtk::Label("") foreach (1..(3 - @d));
    push @b, [ @d ];
}
gtkpack__($v,
	  create_packtable({ homogeneous => 1, col_spacings => 10, row_spacings => 10 },
			   @b
			  ));

#$::isEmbedded or $window->set_usize(500, 500);
gtkadd($window,
       gtkpack_(new Gtk::VBox(0,10),
  		1, gtkpng("wizdrake_logo"),
  		1, $v,
  		0, new Gtk::HSeparator(),
  		0, gtkpack(create_hbox(),
  			   gtksignal_connect(new Gtk::Button (_("Close")),
  					     clicked => sub {$::isEmbedded ? kill(USR1, $::CCPID) : Gtk->exit(0)})
  			  )
  	       )
      );

$window->realize;
$window->show_all();

Gtk->main_iteration while Gtk->events_pending;
$::isEmbedded and kill USR2, $::CCPID;
Gtk->main;

#-------------------------------------------------------------
# menu callback functions
#-------------------------------------------------------------

sub get_main_menu {
  my ($window) = @_;

  my $accel_group = new Gtk::AccelGroup();
  my $item_factory = new Gtk::ItemFactory( 'Gtk::MenuBar', '<main>', $accel_group );
  $item_factory->create_items( @menu_items );
  $window->add_accel_group( $accel_group );
  return ( $item_factory->get_widget( '<main>' ) );
}


my %tr;
my $name;
my $LANG;
sub get_translation {
    my $arch = $Config{'archname'}; $arch =~ s|-.*||;
    (my $LANG2 = $ENV{'LANGUAGE'} || $ENV{'LC_ALL'} || $ENV{'LC_MESSAGES'} || $ENV{'LANG'}) =~ s|_.*||;
    $LANG = $LANG2;
    if ( -r '/etc/sysconfig/system') {
	$expert = cat2_("/etc/sysconfig/system") =~ /^TYPE="?expert/m; #";
    }

    my $conf_dir = '/etc/wizdrake/';

    opendir D, $conf_dir or die "Can't open $conf_dir\n";
    for (readdir(D)) {
	my $file = $_;
	next if /^(\.\.?|CVS)/;
	$name = $_;
	my $f = "$conf_dir$file";
	open(FH, $f);
	$/="";
	while (<FH>) {
	    my ($program, $icon_name);
	    &get_arch;
	    eval ($1) if /EVAL=(.*)$/m;
	    s|\"||g;

	    $program=$1 if /PROGRAM=(.*)/ && !$program; 
	    (my $t = $program) =~ s| .*||; next unless  -x $t || $options{t};
	    next if /^DANGEROUS=YES/m && !$expert;

	    &parse_lang('NAME');
	    $tr{$name}{PROGRAM} = $program;
	    $icon_name ? $tr{$name}{ICON_NAME} = $icon_name : &parse_entry(ICON_NAME);
	    &parse_lang('DESC');
	}
	close FH;
    }
    \%tr;
}
sub parse_lang {
    my $s = shift @_;
    if (/$s\($LANG\)=(.*)/) { $tr{$name}{$s} = $1; } elsif (/$s=(.*)/) { $tr{$name}{$s} = $1; }
}

sub parse_entry {
    my $s = shift @_;  $tr{$name}{$s} = $1 if /$s=(.*)/;
}

sub cat2_ { local *F; open F, $_[0] or $_[1] ? die "cat of file $_[0] failed: $!\n" : return; my @l = <F>; wantarray ? @l : join '', @l }

sub get_arch { next if /Exclusivearch=\s*(.*)/ and $1 !~ $arch;}
