#! /usr/bin/perl
################################################################
###
###				impack
###
###	      Copyright (C) 1997  Internet Message Group
###
###		     This Perl5 library conforms
###		GNU GENERAL PUBLIC LICENSE Version 2.
###
###
### Author:  Internet Message Group <img@mew.org>
### Created: Apr 23, 1997
### Revised: May 06, 1998
###

my $VERSION = "impack version 980506";

$Prog = 'impack';

##
## Require packages
##

use IM::Config;
use IM::Folder;
use IM::File;
use IM::Util;
use integer;
use strict;
use vars qw($Prog $EXPLANATION @OptConfig
	    $opt_src $opt_noharm $opt_verbose $opt_debug $opt_help);

##
## Environments
##

$EXPLANATION = "
$Prog :: Internet Message Pack
$VERSION

usage: $Prog [options] [+folder]
";

@OptConfig =(
    'src;F;;'     => "Set source folder.",
    'noharm;b;;'  => "No packing. Show what will happen.",
    'verbose;b;;' => 'With verbose messages.',
    'debug;d;;'   => "With debug message.",
    'help;b;;'    => "Show this message.",
    );

##
## Profile and option processing
##

init_opt(\@OptConfig);
read_cfg();
read_opt(\@ARGV); # help?
help($EXPLANATION) && exit $EXIT_SUCCESS if $opt_help;

debug_option($opt_debug) if $opt_debug;

##
## Main
##

impack($opt_src);
exit $EXIT_SUCCESS;

##################################################
##
## Work horse
##
sub impack ($) {
    my $folder = shift;
    my $msg = 1;
    my $dst;

    chk_folder_existance($folder);          # not return in case false.

    my @paths = get_impath($folder, 'all');

    print "packing messages in $opt_src ... " unless $opt_noharm;
    flush('STDOUT') unless $opt_noharm;

    foreach (@paths) {
	$dst = $_;
	$dst =~ s|[^/]+$|$msg++|e;
	if ($_ ne $dst){
	    im_rename($_, $dst) || die $@;   # XXX
	}
    }

    print "done\n" unless $opt_noharm;
    touch_folder($folder) unless $opt_noharm;
}

### Local Variables:
### mode: perl
### End:
