#!/usr/bin/perl -w

#
# mkindex
# $Id: mkindex,v 1.9 2003/05/23 16:26:33 johnh Exp $
#
# Copyright (C) 1994-1996,2012  Free Software Foundation, Inc.
# Comments to <johnh@isi.edu>.
#
# This file is under the Gnu Public License.
#

sub usage {
    print STDERR <<END;
usage: $0 <rawindex >index

Converts a raw index from a notes database into an index
suitable for notes-index-mode in emacs.
END
    exit 1;
}

require 5.000;

while (<>) {
    chomp;
    ($date, $subject) = m@/(\d{6})#\* (.*)$@;
    warn("could not find date in rawindex line ``$_''.\n"), next if (!defined($date));
    warn("could not find subject in rawindex line ``$_''.\n"), next if (!defined($subject));
    $lcsubject = lc($subject);
    $subject{$lcsubject} = $subject;
    $list{$lcsubject} = "" if (!defined($list{$lcsubject}));   # for -w
    $list{$lcsubject} .= "$date, ";
};

print "# -*- mode: notes-index -*-\n";
foreach (sort keys %list) {
    # Trim the trailing ", ".
    $list{$_} =~ s/, $//;
    print "$subject{$_}: $list{$_}\n";
};
