#!/usr/bin/perl -w
package ag_cups_server;
BEGIN { push( @INC, '/usr/share/YaST2/modules/' ); }
use ycp;
use YaST::SCRAgent;
use YaPI;
textdomain "printer";
our @ISA = ("YaST::SCRAgent");
use strict;

    use Data::Dumper;



sub createOrInsert {
 my ($line, $data_ref) = @_;
 if ( $line=~/\s*(\w*)\s+([\w\W]*)/){
  # add to existing list
  if (exists $$data_ref{$1}){
   push(@{$$data_ref{$1}}, $2);
  # new item
  } else{
   $$data_ref{$1}=[$2];
  }
 }
}

sub createOrInsertSection {
 my ($line, $section, $data_ref) = @_;
 foreach (@$data_ref){
  if($$section eq "$_->{'Key'} $_->{'Value'}"){
   createOrInsert($line, $_);
  }
 }
}

sub parseSection{
 my ($sec_r, $line, $data_r) = @_;
  	#new section
  	if ($line =~ /<([\w]*)\s*([\w\W]*)>/){
  	  push(@$sec_r, "$1 $2");

	    if(scalar @$sec_r==2){
		my $pos=0;
		foreach (@{$$data_r{'sections'}}){
		 if(@$sec_r[0] eq "$_->{'Key'} $_->{'Value'}"){
		  @{$$data_r{'sections'}[$pos]{'sections'}}=() if (!exists $$data_r{'sections'}[$pos]{'sections'});
	     	  $data_r=$$data_r{'sections'}[$pos];
		 }
		 $pos++;		 
		}
	    }

   	 $$data_r{'sections'}=[] if(!exists $$data_r{'sections'});
  	  push(@{$$data_r{'sections'}}, {'Key'=>$1, 'Value'=>$2});

  	} else{
  	  # global section only
  	  if (scalar @$sec_r==0 or @$sec_r[0] eq ""){
  	    createOrInsert($line, \%$data_r);
  	  #inside sections
 	   }else{
	    my $ref = \@{$$data_r{'sections'}};
	    # in case of sub-section ...
	    if(scalar @$sec_r==2){
		my $pos=0;
		foreach (@{$$data_r{'sections'}}){
		 if(@$sec_r[0] eq "$_->{'Key'} $_->{'Value'}"){
	     	  $ref=\@{$$data_r{'sections'}[$pos]{'sections'}};
		 }
		 $pos++;		 
		}
	    }
  	     createOrInsertSection($line, \@$sec_r[scalar @$sec_r-1], $ref);
 	   }
	  }
}

sub parse_conf {
#    my $class = shift;
    my $file = shift;

    my %data=();
    my @section=();
    if( open( FILE, $file ) ) {
        while( my $line = <FILE> ) {
            chomp($line);
            # remove comments
            next if ( $line =~ /^\s*#/ || $line =~ /^\s*$/);

            # end of the section
            if($line=~/<\/([\w\W]*)>/){
                 pop(@section);
            } else{
		parseSection(\@section, $line, \%data);
	   }
        }
        close(FILE);
    }
 return \%data;
}

sub Execute {
    my $class = shift;
    my ($path, @args) = @_;

    return 1;
}

sub write_params {
 my ($key, $options) = @_;

 my @output=();
 foreach my $params (@$options){
   push (@output, "$key $params");
 }
 return @output;
}

sub write_conf {
    my ($file, $data) = @_;
    my @output = ();

foreach my $key(keys %$data){
 foreach my $options ($$data{$key}){
  if ($key eq "sections"){
   foreach my $item (@$options){  

    my $section=$$item{'Key'};
    push(@output, "<$section $$item{'Value'}>");
    delete $$item{'Key'};
    delete $$item{'Value'};
    foreach my $key2 (keys %$item){
      if ($key2 eq "sections"){

   foreach my $subitem (@{$$item{'sections'}}){  
    my $subsection=$$subitem{'Key'};
    push(@output, "<$subsection $$subitem{'Value'}>");
    delete $$subitem{'Key'};
    delete $$subitem{'Value'};
    foreach my $key2 (keys %$subitem){
        @output=(@output, write_params($key2, $$subitem{$key2}));
    }
    push(@output, "</$subsection>");
   }

      } else{
        @output=(@output, write_params($key2, $$item{$key2}));
      }
    }
    push(@output, "</$section>");
   }
  }else{
    @output=(@output, write_params($key, $options));
  }
 }
}

 open( OUT, $file );
 foreach my $line (@output){
  print OUT "$line\n";
 }
 close(OUT);

 return 1;
}

sub Read {
    my $class = shift;
    my ($path, @args) = @_;
    my $server = "< /etc/cups/cupsd.conf";
    my $client = "< /etc/cups/client.conf";

    if ($path eq ".server"){
	    return parse_conf($server);
    }
    elsif ($path eq ".client"){
	    return parse_conf($client);
    }
    elsif ($path eq "."){
	    return [('server'=> parse_conf($server), 'client'=>parse_conf($client))];
    }else{
	#not implemented
    }
}

sub Write {
    my $class = shift;
    my ($path, @args) = @_;
    my $server = "> /etc/cups/cupsd.conf";
    my $client = "> /etc/cups/client.conf";

    if ($path eq ".client"){
	    return write_conf($client, $args[0]);
    }
    elsif ($path eq ".server"){
	    return write_conf($server, $args[0]);
    }
    else{
	#not implemented
    }
}

sub Dir {
}

package main;

ag_cups_server->Run ();

