#!/usr/bin/perl

use ycp;
use diagnostics;

use strict;
use Getopt::Long;
use POSIX qw(tmpnam);

y2milestone ("ag_ksimport started");

# read the agent arguments
#$_ = <STDIN>;
# no input at all - simply exit
#exit if ! defined $_;



while ( <STDIN> )
{
    my ($command, $path, $argument) = ycp::ParseCommand ($_);
    if ($command eq "Read" )
    {
	

	my   $ks     =  $argument;
	

 	my    %result;

	my %partitioning = ();
	my %raid = ();
	my %networking = ();
	my @packages = ();



	open (KS, "< $ks");
	my $inpre = 0;
	my $inpost = 0;
	my $inpackages = 0;
	my $openedPost = 0;
	my $openedPre = 0;
	my $counter = 0;
	while (<KS>) {
	   
		
	    if (!$inpre && !$inpost) {
		chop;	
	    } 

	    if ($inpackages && !m/^\%pre/ &&  !m/^\%post/) 
	    {			
		my $input = $_;	
		push (@packages	, $input);
	    }
	    elsif ($inpre && !m/^\%pre/ &&!m/^\%packages/) 
	    {		
		print PRE $_;
	    }
	    elsif (m/^%pre/) 
	    {		
		$inpost = 0;
		$inpre = 1;
		$inpackages = 0;
		$openedPre = 1;
		my $pre_file = tmpnam();
		open(PRE,"> " . $pre_file);
		$result{'pre-script'} = $pre_file;
	    }
	    elsif ($inpost && !m/^%post/ && !m/^\%packages/) 
	    {
		print POST $_;
	    }
	    elsif (m/^%post/) 
	    {
		my $input = $_;		
		my $post_file = tmpnam();
		open(POST,"> " . $post_file);
		$result{'post-script'} = $post_file;		
		$openedPost = 1;
		$inpre = 0;
		$inpost = 1;
		$inpackages = 0;


		@ARGV = split(/ / , $input);
		my %h = ();
		GetOptions (\%h,
			    'nochroot', 'interpreter' );
		
		$result{'post'} = \%h;
	    }	  
	    elsif (m/^%packages/) 
	    {
		my $input = $_;					
		$inpackages = 1;
		$inpre = 0;
		$inpost = 0;
		
		
	    }	  

  
	    elsif (m/^install/) 
	    {
		$result{'mode'} = "install";
	    }
	    elsif (m/^lang/) 
	    {
		my @argv = split (/ /);
		$result{'language'} = $argv[1];
	    }
	    elsif (m/^cdrom/) 
	    {
		$result{'cdrom'} = 1;
	    }	 
	    elsif (m/^zerombr/) 
	    {
		my @argv = split (/ /);
		$result{'zerombr'} = $argv[1];
	    }
	    elsif (m/^langsupport/) 
	    {
		my @argv = split (/ /);
		$result{'langsupport'} = $argv[1];
	    }

	    elsif (m/^keyboard/) 
	    {
		my @argv = split (/ /);
		$result{'keyboard'} = $argv[1];
	    }

	    elsif (m/^interactive/) 
	    {		
		$result{'interactive'} = 1;
	    }

	    elsif (m/^reboot/) 
	    {		
		$result{'reboot'} = 1;
	    }
	    elsif (m/^upgrade/) 
	    {		
		$result{'mode'} = "upgrade";
	    }

	    elsif (m/^skipx/) 
	    {		
		$result{'skipx'} = 1;
	    }
	    elsif (m/^text/) 
	    {		
		$result{'textmode'} = 1;
	    }	    
	    elsif (m/^lilocheck/) 
	    {		
		$result{'lilocheck'} = 1;
	    }

	    elsif (m/^part.*/) {
		$counter++;    
		my $part = $_;	
		
		@ARGV = split(/ / , $part);	
		my %h = ();
		GetOptions (\%h, 
			    'size=i',
			    'fstype=s', 
			    'grow',
			    'recommended', 
			    'noformat',
			    'onpart=s',
			    'ondisk=s',
			    'asprimary',
			    'start=i',
			    'end=i',
			    'maxsize=i',
			    'badblocks',
			    'bytes-per-inode=i'

			    );
		
		$partitioning{$ARGV[1] . "_" . $counter} = \%h;
	    }
	    elsif (m/^raid\s.*/) 
	    {
		y2milestone("in RAID section");
		my $input = $_;		
		@ARGV = split(/ / , $input);
		my %h = ();
		GetOptions (\%h,
			    'level=i', 'device=s',
			    'spares=i', 'fstype=s',
			    'noformat'
			    );
	
		$raid{$ARGV[1]} = \%h;

	    }
	    elsif (m/^rootpw.*/) {
		my $rootpw = $_;
		my %users = ();

		@ARGV = split(/ / , $rootpw);

		my %h = ();

		GetOptions (\%h, 'iscrypted');
	
		my $password ;
		my $iscrypted = 0;

		if (!exists($h{'iscrypted'})) {
		    $password = $ARGV[1];
		} else {
		    $password = $ARGV[1];
		    $iscrypted = 1;
		}
		$users{'username'}= "root";
		$users{'password'}= $password;
		$users{'iscrypted'}= $iscrypted;

		$result{'users'} = \%users;
	    }
	    elsif (m/^timezone.*/) {
		my $input = $_;		

		@ARGV = split(/ / , $input);

		my %h = ();
		GetOptions (\%h,
			    'utc'
			    );
	
		my $tz ;
		my $utc = 0;

		if (!exists($h{'utc'})) {
		    $tz = $ARGV[1];
		} else {
		    $tz = $ARGV[1];
		    $utc = 1;
		}
		my %timezone = ();
		$timezone{'utc'}= $utc;
		$timezone{'timezone'}= $tz;
		

		$result{'timezone'} = \%timezone;
	    }
	    elsif (m/^auth.*/ || m/authconfig.*/) 
	    {
		my $auth = $_;		
		@ARGV = split(/ / , $auth);
		my %h = ();
		GetOptions (\%h,
			    'useshadow', 'enableshadow',
			    'enablemd5',
			    'enablenis', 'nisdomain=s', 'nisserver=s',
			    'enableldap', 'enableldapauth', 'ldapserver=s', 'ldapbasedn=s',
			    'enableldaptls', 
			    'enablekrb5', 'krb5realm=s', 'krb5kdc=s', 'krb5adminserver=s',
			    'enablehesiod', 'hesiodlhs=s', 'hesiodrhs=s',
			    'enablesmbauth', 'smbservers=s', 'smbworkgroup=s',
			    'enablecache');	
		$result{'auth'} = \%h;
		
	    }
	    elsif  (m/^bootloader.*/ || m/^lilo.*/) 
	    {
		my $bootloader = $_;		
		@ARGV = split(/ / , $bootloader);
		my %h = ();
		GetOptions (\%h,
			     'append=s', 'location=s', 'useLilo', 'lba32',
			    'password=s', 'md5pass=s', 'linear', 'nolinear',
			    'upgrade');
		
		$result{'bootloader'} = \%h;
	    }
	    elsif  (m/^mouse.*/ ) 
	    {
		my $mouse = $_;		
		@ARGV = split(/ / , $mouse);
		my %h = ();
		GetOptions (\%h,
			    'device=s', 'emulthree' );
		
		my %m = %h;
		$m{'type'} = $ARGV[1];
		$result{'mouse'} = \%m;
	    }
	    elsif (m/^clearpart.*/) {
		my $input = $_;		
		@ARGV = split(/ / , $input);
		my %h = ();
		GetOptions (\%h,
			    'linux', 'all', 'drives=s',
			    'initlabel'
			    );
		
		$result{'clearpart'} = \%h;
	    }
	    elsif (m/^network.*/) {
		my $input = $_;		
		@ARGV = split(/ / , $input);
		my %h = ();
		GetOptions (\%h,
			    'bootproto=s', 'ip=s', 'netmask=s', 'gateway=s', 'nameserver=s',
			    'nodns', 'device=s', 'hostname=s'
			    );
		
		if (exists($h{'device'})) {
		    $networking{$h{'device'}} = \%h;
		}
		else
		{
		    $networking{'eth0'} = \%h;
		}
	    }
	    elsif (m/^nfs.*/) 
	    {
		my $input = $_;		
		@ARGV = split(/ / , $input);
		my %h = ();
		GetOptions (\%h,
			   'server=s', 'dir=s'
			    );
		
		$result{'nfs'} = \%h;
	    }
	    elsif (m/^nfs.*/) {
		my $input = $_;		
		@ARGV = split(/ / , $input);
		my %h = ();
		GetOptions (\%h,
			   'server=s', 'dir=s'
			    );
		
		$result{'nfs'} = \%h;
	    }
	    elsif (m/^hardrive.*/) {
		my $input = $_;		
		@ARGV = split(/ / , $input);
		my %h = ();
		GetOptions (\%h,
			   'partition=s', 'dir=s'
			    );
		
		$result{'harddrive'} = \%h;
	    }
	    elsif (m/^url.*/) {
		my $input = $_;		
		@ARGV = split(/ / , $input);
		my %h = ();
		GetOptions (\%h,
			   'url=s'
			    );
		
		$result{'url'} = \%h;
	    }
	    elsif (m/^xconfig.*/) {
		my $input = $_;		
		@ARGV = split(/ / , $input);
		my %h = ();
		GetOptions (\%h,
			    'server=s', 'card=s', 'videoram=s',
			    'monitor=s', 'hsync=s', 'vsync=s',
			    'resolution=s', 'depth=i', 
			    'startxonboot', 'noprobe', 'defaultdesktop=s'
			    );
		
		$result{'xconfig'} = \%h;
	    }	    
	}
	$result{'partitioning'} = \%partitioning;
	$result{'raid'} = \%raid;
	$result{'networking'} = \%networking;

	$result{'packages'} = \@packages;

	close KS;
	if ($openedPre) {
	    close PRE;
	}
	if ($openedPost) {
	    close POST;
	}
	ycp::Return ( \%result );

	
    }
    else
    {
	y2error ("Unknown instruction $command or argument: ", ref ($argument));
	ycp::Return ("false");
    }
    print "\n";
  
}

#EOF
