MHonArc Users,

The following forwarded message contains a program written by a
user for creating/managing archives by a specified time period.




On Thu, 15 Oct 1998, Earl Hood wrote:

> I received the following message via private mail.  It includes
> a wrapper program to MHonArc to manage monthly-based archives
> in manner similar to the various Procmail recipies posted to
> the list.  Since the author states it is okay to pass along, I
> I shall in-case anyone may have interest in it.


I've written another wrapper which allows smaller or longer periods than a
month. The period is selected on the command line. (I use it for version
2.2. I haven't upgraded yet.)

Feel free to distribute my wrapper in any way.


Thank you for a good program.


Yours
Mats

-----------------------------------------------------------------
Mats Dufberg                                  [EMAIL PROTECTED]


#!/bin/perl

# usage: <script name> --path:<path> --turn:<value> [--subdir:<value>] 
[--donothing:<value>] [--debug:<value>]

# 1. --path: the absolute path to the specific archives base, under which the mail is 
#    archived in a subdirectory, 
# 2. --turn: the number of days/months/year that the each subdirectory should be used 
#    before a new one is created.
# 3. --subdir: optionally if subdirectories are created at year or month levels
# 4. --do-not: don't start Mhonarc, just print out the archive path


##  Specify a package to protect names from MHonArc, which uses package main for most 
stuff

package mhonarcwrapper;

## Settings
$rcname = '.mhonarcrc'; # There must be a readble Mhonarc resource file as 
<path>/$rcname
@validturnvalues = qw(0 1d 2d 3d 5d 10d 15d 1m 2m 3m 4m 6m 1y);
@validsubdirvalues = qw(year month yearmonth);
$lockdelay = 60; # Override the default value of 3 seconds between locking trials
$locktries = 20; # Override the default value of 10 tries

## --turn
# The following values of are supported:

# Val  Meaning         Subdirectory name  Examples
#  0   Never turn      No subdirectory
#  1d  Turn every day  YYYY-MM-DD         1998-05-01
#  3d  Every 3 days    YYYY-MM-DD--DD     1998-05-01--03,
#                                         1998-05-28--31
#  2d  Every 2 days    YYYY-MM-DD--DD     1998-05-01--02,
#                                         1998-05-31
#  5d  Every 5 days    YYYY-MM-DD--DD     1998-05-01--05,
#                                         1998-05-26--31
# 10d  Every 10 days   YYYY-MM-DD--DD     1998-05-01--10,
#                                         1998-05-21--31
# 15d  Every 15 days   YYYY-MM-DD--DD     1998-05-01--15,
#                                         1998-05-16--31
# 1m   Every month     YYYY-MM            1998-05
# 1y   Every year      YYYY               1998
#
# Note that the last period of a month is constructed to include the
# last days of that month without knowing how many days it has.


## --subdir
# The archive directories are grouped into directories by year or month.
# The following values are supported:
# Value         Example that show the effect
# (default)     <path>/1998-05-01
# year          <path>/1998/1998-05-01
# month         <path>/1998-05/1998-05-01
# yearmonth     <path>/1998/05/1998-05-01


## Read switches
foreach (@ARGV) {
    if ($_ =~ /--path:(.+)/) {
        $path = $1; $path =~ s!/$!!; # Remove any trailing "/"
        next;
    };
    if ($_ =~ /--turn:(.+)/) {
        $turn = $1;
        next;
    };
    if ($_ =~ /--subdir:(.+)/) {
        $subdir = $1;
        next;
    };
    if ($_ =~ /--donothing:(.+)/) {
        $timeisnow = $1 if $1=~/^\d+$/;
        $donothing = 1;
        next;
    };
    if ($_ =~ /--debug:(.+)/) { # Hast to be 1 or greater to be meaningfull
        $debug = $1;
        next;
    };
    die qq(Unkonwn switch, "$_");
};


## Check switch values
# Check so that "path" is a valid path
die qq("--path" has to be a writable directory.) unless (-d $path and -w $path);

# Check so that there is a readable resource file
die qq(Resource file is missing or not readable.) 
    unless (-f "$path/$rcname" and -r "$path/$rcname");

# Check so that "turn" has a legal value
die qq("--turn" has an illegal value.) unless grep /^$turn$/, @validturnvalues;

# Check so that "subdir" has a legal value
die qq("--subdir" has an illegal value.) unless grep /^$subdir$/, @validsubdirvalues 
or $subdir eq '';


## Get current date and length of month
$timeisnow = time unless $timeisnow;
($day,$mon,$year) = (localtime($timeisnow))[3,4,5];
for (;;) { # Find last day of month
    ($dayp,$monp) = (localtime($timeisnow+$i*24*60*60))[3,4]; # Increase $i days
    last if $monp != $mon; # Break at turn of month
    $lastdayofmonth = $dayp;
    $i++;
};
$mon++; # Adjust month value
$year += 1900; # Adjust year value


## Calculate the end of the subdirectory name (--turn)
if ($turn eq '0') { # No turning
    $endpath = '';
} elsif ($turn =~ /^(\d+)d$/) { # Daybased
    $interval = $1;
    $periodstart = (int $day/$interval) * $interval + 1;
    $periodstart -= $interval if $day % $interval == 0; # Lower end

    $periodend = (int $day/$interval) * $interval; 
    $periodend += $interval if $day % $interval > 0; # Upper end
    
    $periodend = $lastdayofmonth if $periodend > $lastdayofmonth; # Adjust to last day 
of month
    
    # If interval is 5 or longer we don't let a single trailing day make a period
    if ($periodstart == $periodend and $interval >= 5) {
        $periodstart -= $interval; # Move down lower end
    } elsif ($lastdayofmonth - $periodend == 1 and $interval >= 5) {
        $periodend = $lastdayofmonth; # Move up upper end
    }

    # Make all cods bidigital, after which a string comparison must be done
    $day = "0$day" if $day < 10;
    $mon = "0$mon" if $mon < 10;
    $periodstart = "0$periodstart" if $periodstart < 10;
    $periodend = "0$periodend" if $periodend < 10;

    # This will be the end part of the path
    $endpath = "$year-$mon-$periodstart--$periodend";
    $endpath = "$year-$mon-$periodstart" if $periodstart eq $periodend; # One day 
period

} elsif ($turn =~ /^(\d+)m$/) { # Monthbased
    $interval = $1;
    $periodstart = (int $mon/$interval) * $interval + 1; # Lower end
    $periodstart -= $interval if $mon % $interval == 0;
    $periodend =  (int $mon/$interval) * $interval; # Upper end
    $periodend += $interval if $mon % $interval > 0;

    # Make all cods bidigital, after which a string comparison must be done
    $day = "0$day" if $day < 10;
    $mon = "0$mon" if $mon < 10;
    $periodstart = "0$periodstart" if $periodstart < 10;
    $periodend = "0$periodend" if $periodend < 10;

    # This will be the end part of the path
    $endpath = "$year-$periodstart--$periodend";
    $endpath = "$year-$periodstart" if $periodstart eq $periodend; # One month period

} elsif ($turn =~ /^1y$/) { # Yearly
    $endpath = $year;

} else {
    die qq(Error in interpretaion of "--turn".);
};


## Calculate the optional middle part of the subdirectory name (--subdir)
if ($subdir eq 'year') {
    $yearpath = $year;
    $monthpath = '';
} elsif ($subdir eq 'month') {
    $yearpath = '';
    $monthpath = "$year-$mon";
} elsif ($subdir eq 'yearmonth') {
    $yearpath = $year;
    $monthpath = $mon;
};


## Make the savepath
$savepath = $path;
$savepath .= "/$yearpath" if $yearpath;
$savepath .= "/$monthpath" if $monthpath;
$savepath .= "/$endpath" if $endpath;

if ($donothing) { # Don't archive, just show
    print "$savepath\n";
    exit; 
};

# Do the directories exist? Else try to create them
#$umask = sprintf "%o", umask;
$mode = 0777 - umask;
if ($yearpath and $monthpath) {
    @temp = ("$path/$yearpath", "$path/$yearpath/$monthpath");
    foreach (@temp) {
        mkdir ($_, $mode); 
        warn qq(Cannot create "$_": $!) if $? == 0 and $debug;
    };
} elsif ($yearpath) {
    @temp = ("$path/$yearpath");
    foreach (@temp) {
        mkdir ($_, $mode);
        warn qq(Cannot create "$_": $!) if $? == 0 and $debug;
    };
} elsif ($monthpath) {
    @temp = ("$path/$monthpath");
    foreach (@temp) {
        mkdir ($_, $mode);
        warn qq(Cannot create "$_": $!) if $? == 0 and $debug;
    };
};    
mkdir ($savepath, $mode);
warn qq(Cannot create "$savepath": $!) if $? == 0 and $debug;



# Check so that "savepath" is a valid path
die qq(The subdirectories have to be writable.) unless (-d $savepath and -w 
$savepath);


##      Edit to point to installed mhonarc.
$MHonArc = '/usr/local/bin/mhonarc';
die qq(Cannot find "mhonarc" or it is not executable) unless -x $MHonArc;


## We exit here if debugging, that is we don't start Mhonarc
exit if $debug;

##      Define ARGV (ARGV is same across all packages).
##      Edit options as required/desired.

@ARGV = ("-add",
         "-lockdelay", "$lockdelay",
         "-locktries", "$locktries",
         "-quiet",
         "-rcfile", "$path/$rcname",
         "-outdir", "$savepath");

##      Just require mhonarc, this prevents the overhead of a
##      fork/exec.  We reset the namespace to main just in-case.

package main;
require $mhonarcwrapper::MHonArc;



Reply via email to