
###########################################################################
#
#  create_add_command  <list> <password> [sub_file]
#
#  Create an add command that maximizes the size of batch requests
#  without overrunning the built in line length limitation.
#  from a list of subscribers.  The list of the subscribers should be
#  of the following form:
#               address Firstname Lastname
#
###########################################################################


###########################################################################
#  Process command line args
###########################################################################
die "Format: create_add_command <list> <password> [sub_file]\n"
     unless( @ARGV==2 || @ARGV==3 );

$list = shift(@ARGV);
$password = shift(@ARGV);
$sub_file = shift(@ARGV);


# read from standard input if no file is specified
if($sub_file eq "")
{
     $SUBFILE = STDIN;
}
else
{
     die "Can't open $sub_file\n" unless open(SFILE,"$sub_file");
     $SUBFILE = SFILE;
}


$base = "quiet add $list $password &\n";
$maxlen = 1022;



###########################################################################
#  Create the command, & print to standard output.
###########################################################################

$command = $base;

while( <$SUBFILE> )
{
     chop($_);
     $subline = "\{$_\}&\n";

     #
     # We have enough room, so add another "subscribe"
     #
     if( (length($subline)+length($command)) < $maxlen )
     {
         $command .= $subline;
     }

     #
     # No room left.  Print out this command, and reset $command
     #
     else
     {
         print $command;
         $command = $base . $subline;
     }
}


#
#  Make sure the last subscribers get added
#
if($command ne $base)
{
     print $command;
}



close(SFILE);

