#!/usr/bin/perl

# ------------------------------------------------------------------
#
#    Copyright (C) 2002-2005 Novell/SUSE
#
#    This program is free software; you can redistribute it and/or
#    modify it under the terms of version 2 of the GNU General Public 
#    License published by the Free Software Foundation.
#
# ------------------------------------------------------------------


################################################################################
# ag_subdomain
#
#	Version 0.61
################################################################################

use strict;
use ycp;
use Data::Dumper;
use Locale::gettext;
use POSIX;
use Immunix::Notify;
use Immunix::SubDomain;

setlocale(LC_MESSAGES, "");
textdomain("yast2-apparmor");


# Subroutines
################################################################################

sub getSubdomainStatus {

  my $sdStatus = "disabled";

  # Ok check that there are profiles loaded to 
  # determine status
  my $mountpoint = Immunix::SubDomain::check_for_subdomain();
  if ( $mountpoint ) {
    open( PROFILES, "cat $mountpoint/profiles|" );
    while (<PROFILES>) {
      # Ensure we have loaded profiles
      # not just a loaded module
      if ( /\// ) {
        $sdStatus = "enabled"; 
        last;
      }
    } 
    close PROFILES;
  } 
  return $sdStatus;
}

sub profileSyntaxCheck {
   my $errlist = [];
   Immunix::SubDomain::checkIncludeSyntax($errlist);
   Immunix::SubDomain::checkProfileSyntax($errlist);
   my @errlist = Immunix::SubDomain::uniq(@$errlist);
   return \@errlist;
}


# Main 
################################################################################

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

  my $result = undef;
  my $donereturn = 0;
  if ( $command && $path && $argument ) {
    if ( $argument eq 'sd-all') {
      my %hResult = '';		# hashed result, duh 
      $hResult{'sd-status'} = getSubdomainStatus();
      $hResult{'sd-notify'} = Immunix::Notify::getNotifyStatus();
      #ycp::ycpReturnHashAsMap( %hResult );
      ycp::Return( %hResult );
      $donereturn = 1;
    } elsif ( $argument eq 'sd-status') {
      $result = getSubdomainStatus();
    } elsif ( $argument eq 'sd-notify') {
      $result = Immunix::Notify::getNotifyStatus();
    } elsif ( $command eq "Read" and $argument eq 'custom-includes') {
      my $cfg = Immunix::SubDomain::read_config("logprof.conf");
      ycp::ycpReturn(\@$cfg->{settings}{custom_includes});
      $donereturn = 1;
    } elsif ( $command eq "Execute" and $argument eq 'profile-syntax-check') {
      $result = profileSyntaxCheck();
      ycp::ycpReturn($result);
      $donereturn = 1;
    } elsif ( $argument eq 'sd-notify-settings') {
      $result = Immunix::Notify::getNotifySettings();
      ycp::Return($result);
      $donereturn = 1;
    }
    ycp::ycpReturnSkalarAsString( $result ) if ( ! $donereturn );
  } else {
    my $ycpCmd = ycpGetCommand() || "";
    my $ycpArg = ycpGetArgType() || "";
    $result = "Unknown instruction $ycpCmd or argument: $ycpArg\n";
    ycp::ycpReturnSkalarAsString( $result );
  }
  print "\n";
}
exit 0;



