# -*-perl-*-

# $Id$

# database interface to the limits tables
# (su_limits, su_lim_active, su_lim_alias)

# $Log$
# Revision 1.2  2003/06/09 14:45:11  thomas
# schema version of database
#
# Revision 1.1  2002/07/08 15:56:33  thomas
# 0.2.3 release
#
# Revision 1.1.1.1  2002/05/21 14:13:28  thomas
# sushi - backup to disk
#

package Sushi::Policy;

use strict;
use Data::Dumper;


###############################################################################

sub addPolicy {
  my %parms = ( @_ );

  foreach ('os', 'dump_policy', 'atom_type', 'file_system', 'est_epoch_size') {
    if (!defined $parms{$_}) {
      $::log->error ("Missing required field $_ for policy add\n");
      return 0;
    }
  }

  my $cmd = "insert into wspolicy (os, dump_policy, atom_type, file_system, est_epoch_size) values (".
    $::sudbh->quote($parms{os}) . ", " .
      $::sudbh->quote($parms{dump_policy}) . ", " .
	$::sudbh->quote($parms{atom_type}) . ", " .
	  $::sudbh->quote($parms{file_system}) .
	    ", $parms{est_epoch_size})";
  $main::log->log (4, "Policy::addPolicy $cmd\n");
  my $rows = $::sudbh->do ($cmd);
  $::sudbh->commit;
  return $rows;

}

###############################################################################
sub delPolicy {
  my %parms = ( @_ );

  if (!defined $parms{os} || !defined $parms{dump_policy}) {
    $main::log->error ("Sorry, os and dump policy required for delete.\n");
    return 0;
  }

  my $cmd = "delete from wspolicy where true ";
  foreach ('os', 'dump_policy', 'atom_type', 'file_system', 'est_epoch_size') {
    defined $parms{$_}
      and $cmd .= "and $_ = " . $::sudbh->quote ($parms{$_}) . " ";
  }
  $main::log->log (4, "Policy::delPolicy $cmd\n");
  my $rows = $::sudbh->do ($cmd);
  $::sudbh->commit;
  return $rows;
}

###############################################################################
# Returns an array of limits in the db that match the passed list.

sub getPolicy {
  my %parms = ( @_ );

  my $list = [];

  my $query = "select * from wspolicy where true ";
  foreach ('os', 'dump_policy', 'atom_type', 'file_system', 'est_epoch_size') {
    defined $parms{$_}
      and $query .= "and $_ = " . $::sudbh->quote ($parms{$_}) . " ";
  }

  $query .= " order by os, dump_policy";
  $main::log->log (4, "Policy::getPolicy $query\n");
  my $sth = $::sudbh->prepare ($query) or return undef;
  $sth->execute or return undef;
  while (defined (my $row = $sth->fetchrow_hashref)) {
    push @$list, $row;
  }
  return $list;
}

###############################################################################

sub displayPolicy {
  my $parms = shift;
  my $opts = shift;
  my $fd = shift;
  my $line;
  my $rows = 0;

  if ($opts->{print_headings}) {
    $line = <<__Eof__

Operating    Dump           Sushi         File        Estimated
System       Policy         Atom Type     System      Epoch Size(K)
-----------  -------------  ------------  ----------  ----------
__Eof__
  ;
    Sushi::Misc::printToFile ($fd, $line);
  }

  foreach my $policy (@{getPolicy (%$parms)}) {
    $line = sprintf "%-11s  %-13s  %-12s  %-10s  %d\n",
      $policy->{os}, $policy->{dump_policy}, $policy->{atom_type}, $policy->{file_system}, $policy->{est_epoch_size}/1024;
    Sushi::Misc::printToFile ($fd, $line);
    $rows++;
  }
  if ($opts->{print_headings}) {
    Sushi::Misc::printToFile ($fd, "\n($rows rows)\n\n");
  }
  return $rows;
}

###############################################################################

1;
