#!/usr/bin/perl

# Drakwizard

# Copyright (C) 2002,2003 Mandrakesoft
#
# Authors: Arnaud Desmons <adesmons@mandrakesoft.com>
#          Florent Villard <warly@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::NFS;
use strict;

use common;
use services;
use MDK::Wizard::Wizcommon;

my $wiz = new MDK::Wizard::Wizcommon;

my $o = { 
    name => N("NFS Wizard"),
	var => { 
	    wiz_nfs_dir => '',
	    wiz_nfs_level => '',
	    wiz_netmask => ''
	},
	needed_rpm => [ 'nfs-utils' ],
	defaultimage => "$ENV{__WIZ_HOME__}nfs_wizard/images/NFS.png"
    };

my %level = (
    1 => N("All - No access restriction"),
    2 => N("Local Network - access for local network (recommended)")
);

$o->{pages} = { 
    welcome => {
        name => N("NFS Server Configuration Wizard") . "\n\n" . N("This wizard will help you configuring the NFS server for your network."),
        no_back => 1,
        next => 'nfs'
    },
    nfs => {
        name => N("NFS server") . "\n\n" .N("Directory which will be exported to NFS clients. This directory will be exported in read only mode. It denies any request which requires changes to the filesystem."),
        data => [
            { label => N("Directory:"), val => \$o->{var}{wiz_nfs_dir} },
            ],
        complete => sub {
            if (! -d $o->{var}{wiz_nfs_dir}) {
                $::in->ask_warn(N("Error"), N("The path you entered does not exist."));
                return 1;
            }
        },
        next => 'ask_level'
    },
    ask_level => {
      name => N("Access control") . "\n\n" . N("NFS can be restricted to a certain ip class") . "\n\n" . N("Choose the level that suits your needs. If you don't know, the local network level is usually the most appropriate. Beware that the all level may be not secure."),
      pre => sub {
	$o->{var}{wiz_netmask} = network_mask() if !$o->{var}{wiz_netmask} || $o->{var}{wiz_netmask} eq '0.0.0.0/0.0.0.0'
      },
      data => [ 
            { val => \$o->{var}{wiz_nfs_level}, list => [ keys %level ], format => sub { $level{$_[0]} } },
	    ],
      post => \&chooser,
      next => 'summary'
    },
    shownet => {
       name => N("Grant access on local network") . "\n\n" . N("Access will be allowed for hosts on the network. Here is the information found about your current local network, you can modify it if needed."),
      data => [ 
            { label => N("Authorized network:"), val => \$o->{var}{wiz_netmask} },
	    ],
      next => 'summary'

    },
    summary => {
      name =>  N("The wizard collected the following parameters."),
      pre => sub {
	  $o->{var}{wiz_text_level} = $level{$o->{var}{wiz_nfs_level}};
	  $o->{var}{wiz_netmask} = $o->{var}{wiz_nfs_level} == 1 ? "0.0.0.0/0.0.0.0" : $o->{var}{wiz_netmask} 
      },
      data => [
            { label => N("Exported dir:"), fixed_val => \$o->{var}{wiz_nfs_dir} },
            { label => N("Access:"), fixed_val => \$o->{var}{wiz_text_level} },
            { label => N("Netmask:"), fixed_val => \$o->{var}{wiz_netmask} },
        ],
      post => \&do_it,
      next => 'end'
    },
	       end => { 
		       name => N("Congratulations") . "\n\n" . N("The wizard successfully configured your NFS server."),
		       end => 1,
		       next => 0
		      },
	       error_end => {
			     name => N("Failed"),
                             data => [ { label => N("Relaunch drakwizard, and try to change some parameters.") } ],
                             no_back => 1,
                             end => 1,
                             next => 0,
                            },

	      };

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

sub network_mask {
    my $wiz_ip_server = $wiz->{net}->itf_get("IPADDR");
    my $mask = $wiz->{net}->itf_get("NETMASK");
    $mask = $mask || "255.255.255.0";
    $wiz_ip_server = $wiz_ip_server || "192.168.1.0";
    "$1.$2.$3.0/$mask" if $wiz_ip_server =~ /(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})/;
}

sub chooser {
    $o->{var}{wiz_nfs_level} == 2 || 'shownet'
}

sub do_it {
    $::testing and return;
    my $line;
    my $file = "/etc/exports";
    chomp($o->{var}{wiz_nfs_dir});
    -f $file and cp_af($file, $file.".orig");
    if ($o->{var}{wiz_nfs_level} == 2) {
	#my $mask = $wiz->{net}->itf_get("NETMASK");
	$line = "$o->{var}{wiz_nfs_dir} $o->{var}{wiz_netmask}(ro,no_root_squash,sync)\n";
    }
    else {
	$line = "$o->{var}{wiz_nfs_dir} *(rw,no_root_squash,sync)\n";
    }
    my $t;
    foreach (cat_($file)) {
	if (/^(?!#).*$o->{var}{wiz_nfs_dir}\s/) {
	    $t = $_;
	    last;
	}
    }
    substInFile { s|^(?!#).*$o->{var}{wiz_nfs_dir}\s.*|#$&| } $file;
    append_to_file($file, $line);
    system("/usr/sbin/exportfs -a");
    services::start('nfs') if services::is_service_running('nfs');
    check_started('nfsd');
}

1;