#!/usr/bin/perl -w

# reset_order_number -- date-based order numbers for Interchange

# From an idea posted to the interchange-users
# mail list by Leon Harris <leon@quoll.com>

# Place in crontab with 
# 
# 0 0 * * * /path/to/reset_order_number /path/to/catalog_dir

use Fcntl qw/:flock/;
use POSIX qw/strftime/;

my $dir = shift 
	or die "usage: $0 <catalog-directory-or-counter-file>\n";

my $fn;

if (-f $dir) {
	$fn = $dir;
}
else {
	$fn = "$dir/etc/order.number";
}

open ON, "+<$fn"
	or die "open $fn: $!\n";

flock    ON, LOCK_EX	or die "lock $fn: $!\n";
seek     ON, 0, 0       or die "seek $fn: $!\n";
truncate ON, 0          or die "truncate $fn: $!\n";

print    ON  strftime("#COUNTER-1.0\n%Y%m%d0000\n", localtime() );

close    ON             or die "close $fn: $!\n";

