#!/usr/bin/perl
#
# Copyright (C) 2001 by Sebastien DUPONT <sdupont@mandrakesoft.com>
# Redistribution of this file is permitted under the terms of the GNU
# Public License (GPL)
## description:
#
#  Drakbacup is use to backup system files and user files
#  Drakbacup allow to restore the system (etc, var files)
#  from starup or on drakconf utility.
#
#backup name format: all the time from the /
#                    backup_sys_12102001.tar.gz                    -> default system backup
#		     backup_sys_etc_var_proc_12102001.tar.gz       -> specific system directories
#		     backup_user_james_12102001.tar.gz             -> default user backup
#		     backup_user_james_documents_12102001.tar.gz   -> specific user directories
#                    backup_other_proc_12102001.tar.gz             -> specific other directories
#
# backup name rules: system: begin by sys
#                    user:   begin by user
#                    other:   begin by other
#                    end of all `date`.tar.gz
#
# seems to be good idea to have a txt file where user explain the differences
# between all the backup
#
# save only the differences...
# find / -mtime -1 \! -type d -print > /tmp/liste.jour
#
# build iso fs with rescue.
#
# configuration file on /etc/drakconf/drakbackup/drakbakup.conf
#

use Gtk;
use lib qw(/usr/lib/libDrakX );
use interactive;
use standalone;
use my_gtk qw(:helpers :wrappers);
use common;
use strict;


if ("@ARGV" =~ /--help|-h/) {
    print q(Backup and monitoring application

--list           : list of files or directories to backup.
--default        : save default directories.
--build_cd       : build restore iso with the currents backups files
                       & rescue options.
--build_floppy   : build restore floppy.
--update         : don t replace the old backup, only update it. 
--replace        : delete backup files before build new.
--save_dir       : by default the backup files are saved in 
                       in /var/backup directory so write other directory
	               to change to change it.
--conf_file      : to read other configuration file. 
);
    exit(0);
}

# Backend Options.
my $default = 0;
my $build_cd = 0;
my $build_floppy = 0;
my $mode = 0;
my $conf_file = 0;
my @list_arg = ();
my $windows = 0;
my $central_widget;
my $central_widget_save;
my $interactive;
my $up_box;
my $box;
my $backup_choice = 0;

# config. FILES ->  PATH  & Global variables.
my @sys_files = ("/etc");
	# fixme check system to find all users.
my @home_files = ("/home/seb") ;
my @other_files = ("/var/www") ;
my $cfg_file = "/etc/drakconf/drakbackup/drakbackup.conf";
my $save_path = "/var/drakbackup";
my $option_replace = 0;
my $option_update = 0;
my $comp_mode = "tar.gz";
my $backup_mode = 0; # 1: save all files don't remove if remove on system.


my $DEBUG = 1;


foreach (@ARGV) {
    /--default/ and $default = 1, $mode=-1;
    /--build_cd/ and $build_cd = 1, $mode=-1;
    /--build_floppy/ and $build_floppy = 1, $mode=-1;
    /--replace|-r/ and $option_replace = 1, $mode=-1;
    /--update|-u/ and $option_update = 1, $mode=-1;
    /--conf_file/ and $mode = 0, next;
#    $mode == 0 and push $conf_file, $_;
    /--list/ and $mode = 1, next;
    $mode == 1 and push @list_arg, $_;
}

sub debug {
    print "SYS_FILES: $_ \n" foreach (@sys_files);
    print "HOME_FILES: $_ \n" foreach (@home_files);
    print "OTHER_FILES: $_ \n" foreach (@other_files);
    print "PATH_TO_SAVE: $save_path \n";
    print "OPTION_REPLACE: $option_replace \n";
    print "OPTION_UPDATE: $option_update \n";
    print "OPTION_COMP: $comp_mode \n";
}


sub set_defaultvar {

}


sub read_conf_file {
    if (-e $cfg_file) {
	open CONF_FILE, "<"."$cfg_file" || die;
	while (<CONF_FILE>) {
	    next unless /\S/;
	    next if /^#/;
	    chomp;
	    if (/^SYS_FILES/)      { s/^SYS_FILES=//gi;   @sys_files = split(' ', $_ );	}
	    if (/^HOME_FILES/)     { s/^HOME_FILES=//gi;  @home_files = split(' ', $_ );	}
	    if (/^OTHER_FILES/)    { s/^OTHER_FILES=//gi; @other_files = split(' ', $_ );	}
	    if (/^PATH_TO_SAVE/)   { s/^PATH_TO_SAVE=//gi; $save_path = $_;	}
	    if (/^OPTION_REPLACE/) { $option_replace = 1; $option_update  = 0; }
	    if (/^OPTION_UPDATE/)  { $option_replace = 0; $option_update  = 1; }
	    if (/^OPTION_COMP/)    { s/^OPTION_COMP=//gi; /TAR.GZ/ and  $comp_mode = 0; /TAR.BZ2/ and $comp_mode = 1; }
	}
	close CONF_FILE;
    }
    $DEBUG and debug; 
}

$build_floppy || $build_cd || $default || @list_arg || $conf_file ? backend_mod() : interactive_mode();

sub backend_mod {
    read_conf_file();
}

sub build_cd_fct {

}

sub build_floppy_fct {

}

sub build_backup_files {

}

sub interactive_mode {
    $interactive = 1;
    my $mask;
    my $style;
    my $box;
    init Gtk;
    my $text2 = new Gtk::Text(undef, undef);
    my $window1 = $::isEmbedded ? new Gtk::Plug ($::XID) : new Gtk::Window -toplevel;
    $window1->signal_connect (delete_event => sub { Gtk->exit(0) });
    $window1->set_position(1);
    $window1->set_title(_("Drakbackup"));

    my ($pix_u_map, $pix_u_mask) = gtkcreate_png("pixmaps/backup_title.png");
    my ($pix_l_map, $pix_l_mask) = gtkcreate_png("pixmaps/backup_left.png");
    my ($pix_r_map, $pix_r_mask) = gtkcreate_png("pixmaps/backup_bot.png");
    my ($pix_hd_map, $pix_hd_mask) = gtkcreate_png("pixmaps/backup_hd.png");

    my $box2 = new Gtk::VBox( 0, 10 );

    my $label3 = gtkpack(new Gtk::HBox(0,10),
			 new Gtk::Pixmap($pix_hd_map, $pix_hd_mask),
			 _("Backup Wizard")
			 );

    my $radio1 = new Gtk::RadioButton( "Backup Wizard");
    $box2->add( $radio1);
    $radio1->set_active(1);
    my $radio2 = new Gtk::RadioButton( "Backup Advanced", $radio1);
    $box2->add( $radio2);
    my $radio3 = new Gtk::RadioButton( "Build Backup now", $radio2);
    $box2->add( $radio3);
    my $radio4 = new Gtk::RadioButton( "Restore Backup", $radio3);
    $box2->add( $radio4);
    gtksignal_connect( $radio1, clicked => sub { $backup_choice = 0 });
    gtksignal_connect( $radio2, clicked => sub { $backup_choice = 1 });
    gtksignal_connect( $radio3, clicked => sub { $backup_choice = 2 });
    
    gtkadd($window1,
	   gtkpack(new Gtk::VBox(0,0),
		    gtkpack(new Gtk::VBox(0,0),
			    new Gtk::Pixmap($pix_u_map, $pix_u_mask),
			    gtkpack_(new Gtk::HBox(0,0),
				     0, new Gtk::Pixmap($pix_l_map, $pix_l_mask), 
				     1, gtkpack($up_box = new Gtk::VBox(0,5),
						$box = gtkpack_(new Gtk::VBox(0,0),
							       0, _("Please choose the method to configure your system backup."),
							       1, gtkpack(new Gtk::HBox(0,20),
								       gtkpack(new Gtk::VBox(0,10),
									       new Gtk::HBox(0,15),
									       $box2,
									       new Gtk::HBox(0,5),
									       ),
									  ),
								1, gtkadd(gtkset_layout(new Gtk::HButtonBox, -end),
									  gtksignal_connect(new Gtk::Button(_("Help")), clicked => sub {
									      ${$central_widget}->destroy(); help() }),
								      gtksignal_connect(new Gtk::Button(_("Next")), clicked => sub { 
									  ${$central_widget}->destroy(); 
									  $backup_choice == 0  and print "Wizard\n";
									  $backup_choice == 1  and advanced();
									  $backup_choice == 2  and print "now\n";
								      }),
								      ),
							       ),
						),
				     ),
			    new Gtk::Pixmap($pix_r_map, $pix_r_mask),
			    gtkadd(gtkset_layout(new Gtk::HButtonBox, -end),
				   gtksignal_connect(new Gtk::Button(_("Close")), clicked => sub { Gtk->main_quit() }),
				   gtksignal_connect(new Gtk::Button(_("About")), clicked => sub { $central_widget_save = $central_widget; 
				       ${$central_widget}->destroy(); about() }),
				   gtksignal_connect(new Gtk::Button(_("            Help           ")), clicked => sub { 
				       ${$central_widget}->destroy(); help() }),
				   ),
			    ),
		   ),
	   );
    $central_widget = \$box;
    $window1->show_all;
    $window1->realize;
    $window1->show_all();
    
    Gtk->main;
    Gtk->exit(0);
}


sub retore_window {
    my $restore_box;
    gtkpack($up_box,
	    $restore_box = $central_widget_save,
	    );
    $up_box->show_all();
}

sub about {
    my $text = new Gtk::Text(undef, undef);
    my $about_box;
    gtkpack($up_box,
	    $about_box = gtkpack_(new Gtk::VBox(0,10),
				  1, gtkpack_(new Gtk::HBox(0,0),
					      1, gtktext_insert(gtkset_editable($text, 1), _("
 Copyright (C) 2001 by MandrakeSoft (sdupont\@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.

")),
					      0, new Gtk::VScrollbar($text->vadj),
					      ),
				  0, gtkadd(gtkset_layout(new Gtk::HButtonBox, -spread),
					    gtksignal_connect(new Gtk::Button(_("OK")), clicked => 
							      sub { ${$central_widget}->destroy(); retore_window  }),
					    ),
				  )
	    );
    $central_widget = \$about_box;
    $up_box->show_all();
}


sub help {
    my $text = new Gtk::Text(undef, undef);
    my $about_box;
    gtkpack($up_box,
	    $about_box = gtkpack_(new Gtk::VBox(0,10),
				  1, gtkpack_(new Gtk::HBox(0,0),
					      1, gtktext_insert(gtkset_editable($text, 1), _("
description:

 Drakbacup is use to backup system files and user files
 Drakbacup allow to restore the system (etc, var files)
 from starup or on drakconf utility.

backup name format: all the time from the / dir.

	system backup:
                   backup_sys_12102001.tar.gz 
		   backup_sys_etc_var_proc_12102001.tar.gz
	user backup
		   backup_user_james_12102001.tar.gz      
		   backup_user_james_documents_12102001.tar.gz
	other directories
                   backup_other_proc_12102001.tar.gz          

backup name rules: system:   begin by sys
                   user:     begin by user
                   other:    begin by other
                   end of all `date`.tar.gz


save only the differences...
find / -mtime -1 \! -type d -print > /tmp/liste.jour

build iso fs with rescue.

configuration file:

	/etc/drakconf/drakbackup/drakbakup.conf


")),
					      0, new Gtk::VScrollbar($text->vadj),
					      ),
				  0, gtkadd(gtkset_layout(new Gtk::HButtonBox, -spread),
					    gtksignal_connect(new Gtk::Button(_("OK")), clicked => 
							      sub { ${$central_widget}->hide();  }),
					    ),
				  )
	    );
    $central_widget = \$about_box;
    $up_box->show_all();
}



sub wizard {
    my $wizard_box;
 
    gtkpack($up_box,
	    new Gtk::VBox(0,10),
	    $wizard_box = new Gtk::FontSelection,
	    );
    $central_widget = \$wizard_box;
    $up_box->show_all();

}


sub advanced {
    my $notebook; 
    my $notebook_what; 
    my $notebook_when; 
    my $notebook_where; 
    my $notebook_options; 
    my $about_box;
    my $book_open;
    my $book_open_mask;
    my $transparent;
    my $book_closed;
    my $book_closed_mask;
    
    my $checkbutton = new Gtk::CheckButton( "Check me please!" );
    $checkbutton->set_usize( 100, 75 );
    $checkbutton->show();
    my ($pix_net_map, $pix_net_mask) = gtkcreate_png("pixmaps/backup_net.png");
    my ($pix_hd_map, $pix_hd_mask) = gtkcreate_png("pixmaps/backup_hd.png");
    my ($pix_time_map, $pix_time_mask) = gtkcreate_png("pixmaps/backup_time.png");
    my ($pix_user_map, $pix_user_mask) = gtkcreate_png("../pixmaps/user.png");
    my ($pix_sys_map, $pix_sys_mask) = gtkcreate_png("../pixmaps/bootloader.png");
    my ($pix_other_map, $pix_other_mask) = gtkcreate_png("../pixmaps/net_u.png");
    my ($pix_options_map, $pix_options_mask) = gtkcreate_png("pixmaps/backup_options.png");

#    ($book_open, $book_open_mask) = Gtk::Gdk::Pixmap->create_from_xpm_d($up_box->window, $transparent, @book_open_xpm);
#    ($book_closed, $book_closed_mask) = Gtk::Gdk::Pixmap->create_from_xpm_d($up_box->window, $transparent, @book_closed_xpm);

    my $label_what = gtkpack(new Gtk::VBox(0,10),
			 new Gtk::Pixmap($pix_hd_map, $pix_hd_mask),
			 _("What backup.")
			 );
    my $label_where = gtkpack(new Gtk::VBox(0,10),
			 new Gtk::Pixmap($pix_net_map, $pix_net_mask),
			 _("Where backup.")
			 );
    my $label_when = gtkpack(new Gtk::VBox(0,10),
			 new Gtk::Pixmap($pix_time_map, $pix_time_mask),
			 _("When backup.")
			 );

    my $label_options = gtkpack(new Gtk::VBox(0,10),
			 new Gtk::Pixmap($pix_options_map, $pix_options_mask),
			 _("Options")
			 );

    my $label_what_sys = gtkpack(new Gtk::HBox(0,10),
			 new Gtk::Pixmap($pix_sys_map, $pix_sys_mask),
			 _("System", " ")
			 );
    my $label_what_user = gtkpack(new Gtk::HBox(0,10),
			 new Gtk::Pixmap($pix_user_map, $pix_user_mask),
			 _("Users", " ")
			 );

    my $label_what_other = gtkpack(new Gtk::HBox(0,10),
			 new Gtk::Pixmap($pix_other_map, $pix_other_mask),
			 _("Other", " ")
			 ); 



    my $box_what_sys =  gtkpack(new Gtk::VBox(0,1),
			new Gtk::Pixmap($pix_time_map, $pix_time_mask),
			_("When backup.")
			);

    my $box_what_user =  gtkpack(new Gtk::VBox(0,1),
			new Gtk::Pixmap($pix_time_map, $pix_time_mask),
			_("When backup.")
			);
    
    my $box_what = gtkpack(new Gtk::VBox(0,1),
			   $notebook_what = new Gtk::Notebook,
			   $notebook_what->insert_page( $box_what_sys, $label_what_sys, 0 ),
			   $notebook_what->insert_page( $box_what_user, $label_what_user, 1 ),
			   $notebook_what->insert_page( $box_what_user, $label_what_other, 1 ),
			   $notebook_what->set_scrollable( 1),
			   );

    my $box_where =  gtkpack(new Gtk::VBox(0,1),
 			$notebook_where = new Gtk::Notebook,
 			$notebook_where->insert_page( _("toto") , "titi", 0 ),
			);
    my $box_when =  gtkpack(new Gtk::VBox(0,1),
 			$notebook_when = new Gtk::Notebook,
 			$notebook_when->insert_page( _("toto") , "titi", 0 ),
			);
    my $box_options =  gtkpack(new Gtk::VBox(0,1),
 			$notebook_options = new Gtk::Notebook,
 			$notebook_options->insert_page( _("toto") , "titi", 0 ),
			);


    gtkpack($up_box,
	    $about_box = gtkpack_(new Gtk::VBox(0,10),
				  1, gtkpack(new Gtk::HBox(0,0),
					     gtkpack(my $notebook_box = new Gtk::VBox(0,5),
						     $notebook = new Gtk::Notebook,
						     $notebook->insert_page( $box_what, $label_what, 0),
						     $notebook->insert_page( $box_where, $label_where, 1),
						     $notebook->insert_page( $box_when, $label_when, 2),
						     $notebook->insert_page( $box_options, $label_options, 3),
						     $notebook->set_scrollable( 1 )
							 ),
					     ),
				  0, gtkadd(new Gtk::HButtonBox,
					    gtksignal_connect(new Gtk::Button(_("Previous")), clicked => sub { Gtk->main_quit() }),
					    gtksignal_connect(new Gtk::Button(_("Save and Next")), clicked => sub { 
						${$central_widget}->destroy(); 
						$backup_choice == 0  and print "Wizard\n";
						$backup_choice == 1  and advanced();
						$backup_choice == 2  and print "now\n";
					    }),
					    ),				  
				  ),
	    ),

	my %rotate = (
		top => "right",
		right => "bottom",
		bottom => "left",
		left => "top"
	);

	$notebook->set_tab_pos( $rotate{ $notebook->tab_pos } );

    $central_widget = \$about_box;
    $up_box->show_all();
}










# 							       0, gtktext_insert(gtkset_editable($text2, 0), _("Drakbackup allow you to build a backup of your system. With Drakbackup you will be able to save your backups on your Hard-disk or on CD, across the network with mandrake-online or other place, it accept ftp, sftp, scp, rsync, nfs. "),









#    my $checkbutton = new Gtk::CheckButton( "Check me please!" );
#    $checkbutton->set_usize( 100, 75 );
#    $checkbutton->show();


#     $pixmap_fst_choice = new Gtk::Notebook,
#     my $label1 = new Gtk::Label( "What backup." );
#     my $label2 = new Gtk::Label( "Where backup." );
#     my $label3 = new Gtk::Label( "When backup." );
#
#     gtkadd($window1,
# 	   gtkpack_(new Gtk::VBox(0,2),
# 		    1, gtkpack_(new Gtk::VBox(0,2),
# 				1, new Gtk::VBox(0,0),
# 				1, gtkpack(gtkset_usize($notebook_box = new Gtk::VBox(0,5),500, 350),
# 					   $notebook = new Gtk::Notebook,
# 					   $notebook->insert_page( $checkbutton, $label1, 2 ),
# 					   $notebook->insert_page( $checkbutton, $label2, 2 ),
# 					   $notebook->insert_page( $checkbutton, $label3, 2 )
# 					   ),
# 				1, new Gtk::VBox(0,0)
# 				),
# 		    0, gtkpack_(new Gtk::HBox(0,5),
# 				1, new Gtk::HBox(0,0),
# 				1, gtkadd(gtkset_layout(new Gtk::HButtonBox, -end),
# 					  gtksignal_connect(new Gtk::Button(_("Help")), clicked => sub { 
# 					       ${$central_widget}->destroy(); help() }),
# 					  gtksignal_connect(new Gtk::Button(_("About")), clicked => sub { 
# 					       ${$central_widget}->destroy(); help() }),
# 					  gtksignal_connect(new Gtk::Button(_("Close")), clicked => sub { Gtk->main_quit() }),
# 					  ),
# 				)
# 		    ),
# 	   );

#________________________________________________________________________________________________________


#				0, _("DrakFont"),
#				0, gtkadd(gtkset_layout(new Gtk::VButtonBox, -end),
# 					  gtksignal_connect(new Gtk::Button(_("Windows Importation")), clicked 	=>
# 							    sub { ${$central_widget}->destroy(); $windows = 1; license(\&appli_choice)}),
# 					  gtksignal_connect(new Gtk::Button(_("Configuration")), clicked =>
# 							    sub {  ${$central_widget}->destroy(); $windows = 0; license(\&advanced_install)}),
# 					  gtksignal_connect(new Gtk::Button(_("Uninstall Fonts")), clicked => 
# 							    sub { ${$central_widget}->destroy(); uninstall() }),
# 					  gtksignal_connect(new Gtk::Button(_("Font List")), clicked 	=>
# 							    sub { ${$central_widget}->destroy(); create_fontsel()}),
#					  ),










# sub interactive_mode {
#     my $fst_choice_box;
#     my $fst_choice_box1;
#     my $pixmap_fst_choice;
#     $interactive = 1;
#     my $pixmap;
#     my $pixmapwid;
#     my $mask;
#     my $style;
#     init Gtk;
    
#     $window1 = $::isEmbedded ? new Gtk::Plug ($::XID) : new Gtk::Window -toplevel;
#     $window1->signal_connect (delete_event => sub { Gtk->exit(0) });
#     $window1->set_position(1);
#     $window1->set_title(_("Drakbackup"));

#     my ($pix_u_map, $pix_u_mask) = gtkcreate_png("/home/seb/draklogo.png");
#     my ($pix_l_map, $pix_l_mask) = gtkcreate_png("/home/seb/back_l.png");
#     my ($pix_r_map, $pix_r_mask) = gtkcreate_png("/home/seb/back_r.png");

#     gtkadd($window1,
# 	   gtkpack_(new Gtk::VBox(0,0),
# 		    1, gtkpack_(gtkset_usize(new Gtk::VBox(0,0), 550, 360), 
# 		       1, gtkpack_(my $up_box = new Gtk::VBox(0,0),
# 				   1, new Gtk::VBox(0,0),
# 				   1, gtkpack_($fst_choice_box = new Gtk::VBox(0,0),
# 					       0, my $pix_c = new Gtk::Pixmap($pix_u_map, $pix_u_mask),
# 					       0, gtkpack_(new Gtk::HBox(0,1),
# 							   0, my $pix_l = new Gtk::Pixmap($pix_l_map, $pix_l_mask),
# 							   1, gtkpack_(new Gtk::VButtonBox,
# 								       0, gtksignal_connect(new Gtk::Button(_("Wizard")), clicked => sub { 
# 									   ${$central_widget}->destroy(); wizard() }),
# 								       0, gtksignal_connect(new Gtk::Button(_("Advanced")), clicked => sub {
# 									   ${$central_widget}->destroy(); about() }),		
# 								       ),
# 								    ),
# 					       1, my $pix_r = new Gtk::Pixmap($pix_r_map, $pix_r_mask),
# 					       ),
# 				   ),
# 				1, gtkpack_(new Gtk::HBox(0,5),
# 					    1, new Gtk::HBox(0,5),
# 					    1, gtkadd(gtkset_layout(new Gtk::HButtonBox, -end),
# 						      gtksignal_connect(new Gtk::Button(_("Help")), clicked => sub { 
# 							  ${$central_widget}->destroy(); help() }),
# 						      gtksignal_connect(new Gtk::Button(_("About")), clicked => sub { 
# 							  ${$central_widget}->destroy(); help() }),
# 						      gtksignal_connect(new Gtk::Button(_("Close")), clicked => sub { Gtk->main_quit() }),
# 						      ),
# 					    )
# 				),
# 		    ),
# 		    );
#     $central_widget = \$up_box;
#     $window1->show_all;
#     $window1->realize;
#     $window1->show_all();
    
#     Gtk->main;
#     Gtk->exit(0);
# }