#!/usr/bin/eperl -w
###
#
# Copyright (c) Ensim Corporation 2000, 2001   All Rights Reserved.
#
# This software is furnished under a license and may be used and copied
# only  in  accordance  with  the  terms  of such  license and with the
# inclusion of the above copyright notice. This software or any other
# copies thereof may not be provided or otherwise made available to any
# other person. No title to and ownership of the software is hereby
# transferred.
#
# The information in this software is subject to change without notice
# and  should  not be  construed  as  a commitment by Ensim Corporation.
# Ensim assumes no responsibility for the use or  reliability  of its
# software on equipment which is not supplied by Ensim.
#
# --------------------------------------------------------------------------
# $Id: set_max_size,v 1.10 2003/03/04 23:39:51 amit Exp $
# $Name:  $
# --------------------------------------------------------------------------

push @INC, ($ENV{'OCW_SVCPATH'} || "/usr/lib/opcenter")."/sendmail";

use strict;
require sendmailconf;

use lib ($ENV{OCW_SVCPATH} or "/usr/lib/opcenter") . "/sendmail";
use lib ($ENV{OCW_SVCPATH} or "/usr/lib/opcenter") . "/cmdline_common";

use ERRORS;
use Carper;

my $usage = "Usage: $0 [<messageSize>]";
my($max_msg_size);

$max_msg_size = shift || 0;
die "Invalid value ($max_msg_size)" unless $max_msg_size =~ (/^\d+$/);

# Read the config file
open (CF,$sendmailconf::config{'sendmail_cf'}) or $cerr += $E_UNABLEREADFILE and $cerr += { file => $sendmailconf::config{'sendmail_cf'} } and die "Cannot read $sendmailconf::config{'sendmail_cf'} .\n";
my (@lines)=<CF>;
close (CF);

# First we want to get rid of all instances of MaxMessageSize
@lines = grep { ! /^O MaxMessageSize=(\d+)/ } @lines;

# And then add the new MaxMessageSize (if needed)
push @lines, "O MaxMessageSize=$max_msg_size" if $max_msg_size > 0;

# Write the new file
open (CF,">$sendmailconf::config{'sendmail_cf'}") or $cerr += $E_UNABLEWRITEFILE and $cerr += { file => $sendmailconf::config{'sendmail_cf'} } and die "Cannot write to $sendmailconf::config{'sendmail_cf'} .\n";
print CF @lines;
close(CF);

# Restart sendmail and quit
system("$sendmailconf::config{'sendmail_command'} restart >/dev/null 2>/dev/null");
exit 0;
