#!/usr/bin/env perl

# wbmc_ssh_expireadd, v 1.0 2002/03/05 20:52 hata

require "/opt/nec/mail/wbmc/bin/wbmc_msh_cmd.pl";

$umsconf_file = '/opt/nec/mail/ums.conf';
$ldapsearch_cmd = '/usr/bin/ldapsearch';
$exptime = $ARGV[0];

#  
#   Ķѿꡢݻ֤
#   ldapsearchޥɤǳǼа
#   ǼФФexpireadd ޥɼ¹
#

sub parse_stsvr_list {
    my @stsvr_list = ();
    
  # /opt/nec/mail/ums.conf ɤ߹
  # - host, port, uri, basedn, binddn, bindpw 
  #
    open(UMSCONF_FILE, $umsconf_file);
    while(<UMSCONF_FILE>) {
        $line = $_;
        chomp($line);
        if ($line =~ /^host/) {
            ($para, $host) = split(/=/, $line);
            $host =~ s/"//g;
        }
        if ($line =~ /^port/) {
            ($para, $port) = split(/=/, $line);
            $port =~ s/"//g;
        }
        if ($line =~ /^uri/) {
            ($para, $uri) = split(/=/, $line);
            $uri =~ s/"//g;
        }
        if ($line =~ /^basedn/) {
            ($para, $basedn) = split(/basedn=/, $line);
            $basedn =~ s/"//g;
        }
        if ($line =~ /^binddn/) {
            ($para, $binddn) = split(/binddn=/, $line);
            $binddn =~ s/"//g;
        }
        if ($line =~ /^bindpw/) {
            ($para, $bindpw) = split(/bindpw=/, $line);
            $bindpw =~ s/"//g;
        }
    }

  # ldapseach ޥɤǳǼа
  # - ldapsearch -x -LLL -D "binddn" -w "bindpw" -h "host:port" -b "basedn"
  #                         "objectClass=expresMailServerStsvrConfig"
  #
  #   uid: hogehoge -> hogehoge get
  #
    $ldapsearch_opt = " -x -LLL -D '$binddn' -w $bindpw -H '$uri' -b 'ou=Stsvr Config, $basedn' ";
    $filter = '-u uid';
    $stsvr_search = $ldapsearch_cmd . $ldapsearch_opt . $filter;
    open(SRCH, "$stsvr_search | ");
    while(<SRCH>) {
        $line = $_;
        chomp($line);
        if ($line !~ /^uid: /) {
            next;
        }
        ($uid, $stsvr) = split(/uid: /, $line);
      #  if ($stsvr eq $ENV{'HOSTNAME'}) {   # ۥȤϥå
      #      next;
      #  }
        push(@stsvr_list, $stsvr);
    }
    close(SRCH);
    return @stsvr_list;
}


# Ǽ expireadd ޥɼ¹
#
@stsvr_list = &parse_stsvr_list;
for $stsvr (@stsvr_list) {
    ($hh, $mm) = split(/:/, $exptime);
    if ($hh == 99 && $mm == 99) {
	$cmd = "/opt/nec/mail/mcmd/expireadd -g";
	&wbmc_dc_rsh2($stsvr, $cmd);
	exit 0;
    }
    $cmd = "/opt/nec/mail/mcmd/expireadd $hh $mm";
    &wbmc_dc_rsh($stsvr, $cmd);
}
