# version 0.2
#
# Copyright (C) 2004 Mandrakesoft
#
# Author: aginies _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.
#

package MDK::Wizard::Installsrv;
use lib qw(/usr/lib/libDrakX);
use strict;
use standalone;
use common;
use services;
use MDK::Wizard::Varspaceval;
use MDK::Wizard::Wizcommon;


# test root capa
my $wiz = new MDK::Wizard::Wizcommon;

my $INSTALLDIR = "/var/install/";
my $NFSEXPORTS = "/etc/exports";
my $WWWDIR = "/var/www/html";

my $o = {
	 name => 'MDK Install Server Wizard',
	 var => {
		 DESTDIR => '/var/install/cooker',
		 SOURCEDIR => '/mnt/nfs/cooker',
		},
	 needed_rpm => [ 'nfs-utils', 'apache' ],
	 defaultimage => "/usr/share/wizards/installsrv_wizard/images/Install.png"
	};

$o->{pages} = {
	       welcome => {
			   name => N("Configure a MDK install server (via NFS and http)") . "\n\n" . N("Easily configure a Mandrake server installation directory, with NFS and HTTP access."),
			   no_back => 1,
			   next => 'install_srv',
			  },
	       install_srv => {
			       name => N("Install server configuration") . "\n\n" . N("Path to data: specify your source directory, should be base of an Mandrake installation.") . "\n\n" . N("Destination directory: copy file in which directory ?"),
			       pre => sub {
				 $o->{var}{wiz_nfs} ||= 1;
				 $o->{var}{wiz_http} ||= 1;
			       },
			       data => [
					{ label => "Path to data:", val => \$o->{var}{SOURCEDIR} },
					{ label => "Destination directory:", val => \$o->{var}{DESTDIR} },
#					{ label => N("Enable NFS install server:"), type => 'bool', val => \$o->{var}{wiz_nfs} },
#					{ label => N("Enable HTTP  install server:"), type => 'bool', val => \$o->{var}{wiz_http} },
				       ],
			       post => \&test_data,
			       next => 'summary_srv',
			      },
	       error_dir_dest => {
				  name => N("Destination directory could not be '/var/install/'") . "\n\n" . N("ie use: /var/install/mdk-release"),
				  next => 'install_srv',
				 },
	       error_dir => {
			     name => N("Error, source directory must be a directory with full Mandrake installation directory."),
			     next => 'install_srv',
			    },
	       dir_already_use => {
				   name => N("Destination directory already in use, please choose another one."),
				   next => 'install_srv',
				  },
	       summary_srv => {
			       name => N("I Will configure your install server with those parameters"),
			       pre => sub {
				 $o->{var}{nfs} = $o->{var}{wiz_nfs} ? N("enabled") : N("disabled");
				 $o->{var}{http} = $o->{var}{wiz_http} ? N("enabled") : N("disabled");
			       },
			       data => [
					{ label => "Path to data:", fixed_val => \$o->{var}{SOURCEDIR} },
					{ label => "Destination directory:", fixed_val => \$o->{var}{DESTDIR} },
					{ label => N("Enable NFS install server:"), fixed_val => \$o->{var}{nfs} },
					{ label => N("Enable HTTP install server:"), fixed_val => \$o->{var}{http} },
                                       ],
                               post => \&do_it,
                               next => 'end',
			      },
	       end => {
		       name => N("Congratulations, Mandrake Install server is now ready."),
		       end => 1,
		       no_back => 1,
		       next => 0
		      },
	      };


sub add_install_dir {
  if (any { /$INSTALLDIR/ } cat_($NFSEXPORTS)) {
    print " - " . $NFSEXPORTS . " ready\n";
  } else {
    append_to_file($NFSEXPORTS, "$INSTALLDIR       *(async,rw,no_root_squash)\n");
    system("service nfs reload");
  }
}

sub test_data {
  -d $o->{var}{SOURCEDIR} && -f "$o->{var}{SOURCEDIR}/VERSION" or return 'error_dir';
  if ($o->{var}{DESTDIR} eq $INSTALLDIR) { return 'error_dir_dest' }
  if (-e $o->{var}{DESTDIR}) { return 'dir_already_use' }
}

sub add_ftpuser {
    system("/usr/sbin/useradd -u 12383 -d install -r -s /bin/bash install");
}

sub add_http_link {
  my ($PATH) = @_;
  my $LINK = $WWWDIR . '/' . basename($PATH);
  if (! -f $LINK) {
    symlink $PATH, $LINK;
  }
  system("service httpd restart");
}

sub cp_data {
  my ($SRCDIR, $DEST) = @_;
  mkdir_p($DEST);
  system("cp -av $SRCDIR/* $DEST");
}

sub do_it {
  return if $::testing;
  my $in = 'interactive'->vnew('su', 'Install server');
  my $w = $in->wait_message(N("Install Server"), N("Copying data to destination directory, can take a while...."));
  cp_data($o->{var}{SOURCEDIR}, $o->{var}{DESTDIR});
  add_install_dir();
  add_http_link($o->{var}{DESTDIR});
  undef $w;
}

sub new {
    my ($class) = @_;
    bless {
            o   => $o,
       }, $class;
}

1;