#!/usr/bin/perl
#
# compress-majordomo-archive.pl - Compress old majordomo archives
#
# Version 0.1 - first public release
#
# Copyright 1996 by Richard Lunson Bullington III.
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
# You may contact the author at rbulling@obscure.org, if you have
# questions about this program. The current version and supporting
# documentation to this program can be found at:
#
#       http://www.obscure.org/slow-net/majordomo-tips/index.html
#
# Call this from cron(1) on a monthly basis to compress old Majordomo archives
# using a crontab line similar to this:
#
#  # This command compresses last month's majordomo archives at 1AM on
#  # the first day of every month
#  0 1 1 * * /usr/local/etc/compress-majordomo-archives.pl
#

##########################################################################
######                   Configurable Constants                  #########
##########################################################################

# Turn debug on (set it to non-zero) to get a play-by-play 
# trace of this process.

$debug = 1;

# @lists contains the names of all the lists that need to be compressed.

@lists = ("javascript");

# $base_archive_dir contains the name of the directory that has the
# mailing list archive subdirectories within it. This script assumes
# that each list has its own directory, named the same as the list,
# under this base directory.

$base_archive_dir = "/usr/local/mail/archive";

# $gzip contains the full path to gzip in the filesystem.

$gzip = "/bin/gzip";


########################################################################
############              End of Constants                   ###########
########################################################################

$progname = $0;
$progname =~ /.*\/(.+)$/;
$progname = $1;

$debug && print "$progname: Compressing archives under $base_archive_dir\n";

($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);

for $list (@lists) {


	# Perl localtime has 0 as January, 1 as February (zero-based month
	# numbers), and Majordomo uses 01 as January, 02 as February, so
	# the localtime-derived month figure is good as a majordomo-oriented
	# last month figure. 
	$last_month = $mon;
	$effective_year = $year;

	if ($last_month < 1) {
		$last_month = 12;

		# Handle century transition
		if ($effective_year == 0) {
			$effective_year = 99;
		}
	}

	$archive_name = sprintf( "%s.%02u%02u", $list, $effective_year, $last_month, );
	$archive_dir = "$base_archive_dir/$list";

	$debug && print "$progname: Compressing $archive_name\n";

	# This command creates a compressed archive file, but does not delete
	# the original archive.
	system("$gzip -c --best $archive_dir/$archive_name > $archive_dir/$archive_name.gz");

	# Enable this version if you wish to only keep the compressed files, and
	# delete the original archive file.
	#system("$gzip --best $archive_dir/$archive_name");

	if ($? != 0) {
		$error = $? >> 8;
		print "$progname: Error $? compressing $archive_dir/$archive_name\n";
	}

}

