File: recordsadmin.in

package info (click to toggle)
records 1.4.9-3
  • links: PTS
  • area: main
  • in suites: woody
  • size: 736 kB
  • ctags: 497
  • sloc: lisp: 6,196; perl: 664; sh: 315; makefile: 192
file content (841 lines) | stat: -rwxr-xr-x 24,600 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
#! @PERL@ -- -*- perl -*-

### recordsadmin.in
###
### $Id: recordsadmin.in,v 1.14 2001/04/11 18:14:12 ashvin Exp $
###
### Copyright (C) 1996 by Ashvin Goel
###
### This file is under the Gnu Public License.

require 5.000;

use Getopt::Std;
use File::Basename;

# global - should be set by makefile

$homedir="$ENV{HOME}";
$records_srcdirpath="@srcdirpath@";

# handle cygwin windows environment
# probably flaky right now
$cygpath="@CYGPATH@";
if ((($ENV{OSTYPE} eq "cygwin") || ($ENV{OSTYPE} eq "CYGWIN")) &&
    $cygpath ne "") {
    $homedir = &cygwin_translate_path($homedir);
    $records_srcdirpath=&cygwin_translate_path($records_srcdirpath);
}

$records_init_file="${homedir}/.emacs-records";
$records_emacs_init_file="${homedir}/.emacs";

# external global variables read from .emacs-records
# with their documentation
# initialized here to sane defaults 

@records_external_vars = (
    "records_directory", 1, 'Directory under which all records are stored.',
    "records_index_file", 1, 'File name in which records subject index is stored.
Make sure that the file is within the records directory.',
    "records_dindex_file", 1, 'File name in which records date index is stored.
Make sure that the file is within the records directory.',
    "records_directory_structure", 0, 'The directory structure for records files.
Its values can be 
0 => all records are stored in records_directory. 
1 => records are stored by year within records_directory.
2 => records are stored by year and month within records_directory.',
    "records_day_order", 0, 'A records file name is composed of a day, month and year.
This variable determines the order of the day in date. 
For example, day = 0, if you want a records date to be in (day, month, year)
format and day = 2, if you want a records date to be (year, month, day), etc.
Valid values for day are 0, 1 or 2 only.',
    "records_month_order", 0, 'A records file name is composed of a day, month and year.
This variable determines the order of the month in date. 
For example, month = 1, if you want a records date to be in (day, month, year)
format and month = 0, if you want a records date to be (month, day, year), etc.
Valid values for month are 0, 1 or 2 only.',
    "records_year_order", 0, 'A records file name is composed of a day, month and year.
This variable determines the order of the year in date. 
For example, year = 2, if you want a records date to be in (day, month, year)
format and year = 0, if you want a records date to be (year, month, day), etc.
Valid values are 0, 1 or 2 only.',
    "records_year_length", 0, 'The length of a records file year. 
This can be 4 (example 1996) or 2 (96).
Even if it is 2, records will work correctly after the turn of the century.
Valid values are 2 or 4 only.',
);

$records_directory = "${homedir}/records";
$records_index_file = "${records_directory}/index";
$records_dindex_file = "${records_directory}/dindex";
$records_directory_structure = 1;
$records_day_order = 0;
$records_month_order = 1;
$records_year_order = 2;
$records_year_length = 4;

# internal global variables
# initialized in &records_initialize_vars
$records_date_length = 0;
@records_date_order = ();
@records_date = ();
@records_glob_regexp = ();

# other global variables
$records_day_length = 0;
$records_month_length = 0;
%records_raw_index = ();
%records_date_index = ();
$alternate = 1; # alternate set of variables

$records_init_mesg = ";;; AUTOMATICALLY GENERATED: DO NOT ALTER OR DELETE FROM THIS LINE ONWARDS";

$records_more_init_mesg = <<MORE_MESG

;;; Add any emacs customization before this comment.

;commented by tv\@debian.org - this shouldn't be necessary on Debian.
;(let ((f (expand-file-name "$records_srcdirpath")))
;  (if (not (member f load-path))
;      (setq load-path (cons f load-path))))

(autoload 'records-mode "records" "records mode" t)
(autoload 'records-goto-today "records" "Go to today's records" t)
(autoload 'records-underline-line "records" "underine a title" t)
(autoload 'records-index-mode "records-index" "records index mode" t)

(setq auto-mode-alist
      (cons (cons "[0-9][0-9][0-9][0-9][0-9][0-9].?.?\$" 'records-mode)
	    auto-mode-alist))

;;; The following part of the file is also read by perl.
MORE_MESG
;

#
# Start the bang bang '
#
 
if (!getopts('hdvicn')) {
    &usage();
}

# testing
# &records_initialize_vars(1);
# &records_query_vars($alternate);
# &records_initialize_vars(0, $alternate);
# &records_update_links("/home/asgoel/records/96/961209", $alternate);
# exit(0);

&usage() if ($opt_h);
$debug = 1 if ($opt_d);
$verbose = 1 if ($opt_v);

if ($opt_i) {
    &records_install_init_file();
}

if ($opt_c) {
    &records_convert_format();
}

if (!$opt_n) {
   print "Indexing all records...\n";
   &records_recreate_indexes();
}

print "If you already have an emacs running with records mode loaded, run
M-x records-initialize (C-c C-z) in a record buffer\n";

exit(0);

sub usage {
    print STDERR "Usage: $0 [-options ...]

This program will recreate the records indexes and modifed records files
to reflect the new indexes. With more options, it will also
install records defaults, convert date and directory formats, etc.

where options are:
	[-h]	this usage message
	[-i]	install records defaults and then reindexes records
	[-c]	converts one date and directory format to another and reindex
	[-n]	do not reindex records (useful with -i or -c)
	[-d]	run in debug mode
	[-v]	run in verbose mode
";
    exit(1);
}

# make directory hierarchy
sub mkdirhier {
    local($dir, $mode) = @_;
    local($pdir) = dirname($dir);

    if (!(-d $pdir)) {
	&mkdirhier($pdir, $mode);
    }
    mkdir($dir, $mode) ||
	die "Couldn't make directory $dir: $!\n";
}

sub records_write_file {
    local($file, *records_file) = @_;

    print "$file is modified.\n" if ($verbose);
    # rewrite the new file
    if (!rename("$file", "$file~")) {
	warn("Could not rename $file: $!\n");
	return;
    }
    if (!open(OUT, ">$file")) {
	warn("Could not open $file for writing: $!\n");
	# get back old file
	rename("$file~", "$file");
	return;
    }
    print OUT @records_file;
    close (OUT);
}

# returns 1 if the init file has the generated stuff in it or else 0
sub records_read_init_file {
    local($var);
    local($init_msg_seen) = 0;

    if (!open(IN, "< $records_init_file")) {
        return 0;
    }
    while(<IN>) {
	if (!$init_msg_seen) {
	    next unless /$records_init_mesg/;
	    $init_msg_seen = 1;
	}
	next if /^\s*$/;
	next if /^\s*;/;
	if (/\s*\(\s*setq\s+([A-Za-z-]+)\s+([0-9]+)\s*\)/) {
	    # numeric quantity found
	    $var = $1;
	    $val = $2;
	    $var =~ s/-/_/g;
	    ${$var} = $val;
	    next;
        }
	if (/\s*\(\s*setq\s+([A-Za-z-]+)\s+\"(.*)\"\s*\)/) {
	    # string quantity found
	    $var = $1;
	    $val = $2;
	    $var =~ s/-/_/g;
	    ${$var} = $val;
	    next;
	}
    }
    close(IN);
    return $init_msg_seen;
}

# will take alternate set of variables
sub records_write_init_file {
    local($init, $val) = @_;
    local($len) = $#records_external_vars; # length of the array
    local($i, $var, $value, $str);


    # if init file exists 
    # we have to remove the generated stuff from it.
    if ($init) {
	if (!rename("$records_init_file", "$records_init_file~")) {
	    warn("Could not rename $records_init_file: $!\n");
	    return;
	}
	if (!open(IN, "< $records_init_file~")) {
	    die "Couldn't open $records_init_file~: $!\n";
	}
    }
    if (!open(AP, ">> $records_init_file")) {
	die "Couldn't write to $records_init_file: $!\n";
    }
    if ($init) {
	# get stuff from $records_init_file~
	while(<IN>) {
	    if (!/$records_init_mesg/) {
		print AP;
		next;
	    }
	    last;
	}
	close(IN);	
    }
    print AP $records_init_mesg;
    print AP $records_more_init_mesg;
    for ($i = 0; $i < $len; $i += 3) {
	$var = $records_external_vars[$i];
	$str = $records_external_vars[$i+1];
	$value = ${$var . $val};
	$var =~ s/_/-/g;
	if ($str) {
	    print AP "(setq ", $var, " ", "\"",  $value, "\"", ")\n";
        } else {
	    print AP "(setq ", $var, " ", $value, ")\n";
        }
    }
    close(AP);
}

# will take alternate set of variables
# returns 1 if the validate is unsuccessful or else 0
sub records_validate_vars {
    local($val) = @_;
    local($ret) = 0;

    local(*records_directory_structure_a)
	= \${"records_directory_structure_a" . $val};
    local(*records_day_order_a) = \${"records_day_order" . $val};
    local(*records_month_order_a) = \${"records_month_order" . $val};
    local(*records_year_order_a) = \${"records_year_order" . $val};
    local(*records_year_length_a) = \${"records_year_length" . $val};

    if ($records_directory_structure_a != 0 && 
	$records_directory_structure_a != 1 &&
	$records_directory_structure_a != 2) {
	print "records_directory_structure: should have a value of 0, 1 or 2.\n";
	$ret = 1;
    }
    if ($records_day_order_a != 0 &&
	$records_day_order_a != 1 &&
	$records_day_order_a != 2) {
	print "records_day_order: should have a value of 0, 1 or 2.\n";
	$ret = 1;
    }
    if ($records_month_order_a != 0 &&
	$records_month_order_a != 1 &&
	$records_month_order_a != 2) {
	print "records_month_order: should have a value of 0, 1 or 2.\n";
	$ret = 1;
    }
    if ($records_year_order_a != 0 && 
	$records_year_order_a != 1 &&
	$records_year_order_a != 2) {
	print "records_year_order: should have a value of 0, 1 or 2.\n";
	$ret = 1;
    }
    if (($records_day_order_a == $records_month_order_a) ||
	($records_month_order_a == $records_year_order_a) ||
	($records_year_order_a == $records_day_order_a)) {
	print "records_{day|month|year}_order: should have different values.\n";
	$ret = 1;
    }
    if (!($records_year_length_a == 2 || $records_year_length_a == 4)) {
	print "records_year_length: should have a value of 2 or 4.\n";
    }
    return $ret;
}

# will take alternate set of variables
sub records_initialize_vars {
    local($read_init, $val) = @_;
    local($i, $dmy, $dmylen);
    local($dir_regexp, $file_regexp);

    local(*records_day_length_a) = \${"records_day_length" . $val};
    local(*records_month_length_a) = \${"records_month_length" . $val};
    local(*records_year_length_a) = \${"records_year_length" . $val};

    local(*records_day_order_a) = \${"records_day_order" . $val};
    local(*records_month_order_a) = \${"records_month_order" . $val};
    local(*records_year_order_a) = \${"records_year_order" . $val};

    local(*records_date_length_a) = \${"records_date_length" . $val};
    local(*records_date_order_a) = \@{"records_date_order" . $val};
    local(*records_date_a) = \@{"records_date" . $val};

    local(*records_directory_structure_a)
	= \${"records_directory_structure" . $val};
    local(*records_glob_regexp_a) = \@{"records_glob_regexp" . $val};

    if ($read_init) {
	&records_read_init_file();
    }

    $records_day_length_a = 2;
    $records_month_length_a = 2;
    $records_date_length_a = 0;
    @records_date_a = (['year', 0, 0], ['month', 0, 0], ['day', 0, 0]); 

    # initialize records_date_order
    $records_date_order_a[$records_day_order_a] = 'day';
    $records_date_order_a[$records_month_order_a] = 'month';
    $records_date_order_a[$records_year_order_a] = 'year';
    # initialize records_date
    foreach $dmy (@records_date_order_a) {
	for ($i = 0; $i < 3; $i++) {
	    if ($records_date_a[$i][0] eq $dmy) {
		$dmylen = ${"records_" . $dmy . "_length_a"};
		$records_date_a[$i][1] = $records_date_length_a;
		$records_date_a[$i][2] = $dmylen;
		$records_date_length_a += $dmylen;
	    }
	}
    }

    # initialize records_glob_regexp 
    # directory structure + date length considered
    if ($records_directory_structure_a == 0) {
	$dir_regexp = '';
    } elsif ($records_directory_structure_a == 1) {
	for ($i = 0; $i < $records_year_length_a; $i++) {
	    $dir_regexp .= "[0-9]";
	}
	$dir_regexp .= "/";	
    } elsif ($records_directory_structure_a == 2) {
	for ($i = 0; $i < $records_year_length_a; $i++) {
	    $dir_regexp .= "[0-9]";
	}
	$dir_regexp .= "/[0-9][0-9]/";	
    }
    for ($i = 0; $i < $records_date_length_a; $i++) {
	$file_regexp .= "[0-9]";
    }
    @records_glob_regexp_a = ($dir_regexp, $file_regexp);
}

# will take alternate set of variables
# query the user for values
sub records_query_vars {
    local($val) = @_;

    local($len) = $#records_external_vars; # length of the array
    local($i);
    local($var, $doc, $value);
    local($do_validate) = 1;

    while ($do_validate) {
	for ($i = 0; $i < $len; $i += 3) {
	    $var = $records_external_vars[$i];
	    $doc = $records_external_vars[$i+2];
	    if ($val) {
		${$var . $val} = ${$var};
	    }
	    print $doc, "\n";
	    print $var, " (", ${$var}, "): "; 
            chop($value = <>);
	    if ($value ne "") {
		${$var . $val} = $value;
	    }
	    print "\n";
	}
	# validate input
	$do_validate = &records_validate_vars($val);
	if ($do_validate) {
	    print "You have input bad values. Try again.\n\n";
	}
    }
}

sub records_find_records {
    local(@files);
    local($i);
    local($dir_regexp);
    local($file_regexp);
    local($regexp);

    chdir $records_directory ||
	die "Can't change directory to $records_directory: $!\n";
    @files = glob($records_glob_regexp[0] . $records_glob_regexp[1]);
    return @files;
}

sub records_normalize_date {
    local($date) = @_;
    local($i);
    local(@date);
    
    for ($i = 0; $i < 3; $i++) {
	$date[$i] = substr($date, $records_date[$i][1], $records_date[$i][2]);
    }
    if (length($date[0]) == 2) {
	# normalize year
        local($year) = substr($date[0], 0, 2);
	if ($date[0] > 90) {
	    substr($date[0], 0, 2) = "19";
	} else {
	    substr($date[0], 0, 2) = "20";
	}
	substr($date[0], 2, 4) = $year;
    }
    # year month day
    return $date[0] . $date[1] . $date[2];
}

# will take alternate set of variables
sub records_denormalize_date {
    local($ndate, $val) = @_;
    local(*records_date_length_a) = \${"records_date_length" . $val};
    local(*records_date_a) = \@{"records_date" . $val};

    local($date) = sprintf("%${records_date_length_a}s", " ");
    if ($records_date_a[0][2] == 2) {
	# denormalize year
	substr($date, $records_date_a[0][1], $records_date_a[0][2]) = 
	    substr($ndate, 2, 2);
    } else {
	substr($date, $records_date_a[0][1], $records_date_a[0][2]) = 
	    substr($ndate, 0, 4);
    }
    substr($date, $records_date_a[1][1], $records_date_a[1][2])
	= substr($ndate, 4, 2);
    substr($date, $records_date_a[2][1], $records_date_a[2][2])
	= substr($ndate, 6, 2);
    return $date;
}

# will take alternate set of variables
# absolute =  0 => relative path from a records file
#	   =  1 => full absolute path
#	   = -1 => relative path from the records directory
sub records_directory_path {
    local($date, $absolute, $val) = @_;
    local(*records_date_a) = \@{"records_date" . $val};
    local(*records_directory_a) = \${"records_directory" . $val};
    local(*records_directory_structure_a) = 
	\${"records_directory_structure" . $val};

    if ($records_directory_structure_a == 0) {
	return ($absolute >= 1) ? $records_directory_a : '';
    } elsif ($records_directory_structure_a == 1) {
	return (($absolute >= 1) ? $records_directory_a . "/" : 
		($absolute <= -1) ? '' : "../") .
		    substr($date, $records_date_a[0][1], $records_date_a[0][2]); 
    } elsif ($records_directory_structure_a == 2) {
	return (($absolute >= 1) ? $records_directory_a . "/" : 
		($absolute <= -1) ? '' : "../../") .
		    substr($date, $records_date_a[0][1], $records_date_a[0][2]) .
			"/" . substr($date, $records_date_a[1][1], 
				     $records_date_a[1][2]); 
    } else {
	die "records_directory_path: bad value\n";
    }
}

sub records_update_file {
    local($file) = @_;
    local($date) = basename($file);
    local($subject);
    local($file_modified);
    local(@records_file);

    ## variables in the finite state machine
    local($regexp, $a_link_regexp, $link_regexp);
    local($a_link_found, $link_found);
    local($subject_found, $full_subject_found);
    local($tag, $tag_generated);
    local($state) = 1;

    # TODO: finite state machine diagram.

    if (!open(IN, "< $file")) {
        warn("$0: cannot open $file.  Skipped.\n");
        return;
    }

    while ($state) {
	if ($state == 1) {
	    if (!($_ = <IN>)) {
		# eof detected
		$state = 0;
		next;
	    }
	    chop;
	    if (!$a_link_found) {
		$state = 2;
	    } else {
		$state = 3;
	    }
	    next;
	} elsif ($state == 2) {
	    # look for subject
	    if (!$subject_found) {
		if (/^\* (.*)\s*$/) {
		    # partial subject was found
		    # get potential subject name
		    $subject = $1;
		    $subject_found = 1;
		}
		$state = 4;
		next;
	    }
	    if (!$full_subject_found) {
		$regexp = "^-{" . sprintf("%d", length($subject) + 2) . "}";
		if (/$regexp/) {
		    $full_subject_found = 1;
		} else {
		    # subject is bogus
		    $subject_found = 0;
		}
		$state = 4;
		next;
	    }
	    # found a records subject
	    $state = 3;
	    next;
	} elsif ($state == 3) {
	    if (!$tag_generated) {
		$tag = records_add_record_to_rawindex($subject, $date);
		$tag_generated = 1;
	    }
	    # check loose link match
	    $a_link_regexp = "^(link|prev|next): <.*>";
	    if (/$a_link_regexp/) {
		$a_link_found = 1;
		$link_regexp = "^link: <.*\/" . $date . "#" . $tag . "\\* " . 
		    $subject . ">";
		if (/$link_regexp/) {
		    $link_found = 1;
		    $state = 4;
		} else {
		    $file_modified = 1 unless ($file_modified);
		    $state = 1;
		}
		next;
	    } else {
		if (!$link_found) {
		    $file_modified = 1 unless ($file_modified);
		    push(@records_file, "link: <" . &records_directory_path($date)
			 . "/" . $date . "#" . $tag . "* " . $subject . ">\n");
		}
		$a_link_found = 0;
		$link_found = 0;
		$subject_found = 0;
		$full_subject_found = 0;
		$tag_generated = 0;
		$state = 2;
		next;
	    }
	} elsif ($state == 4) {
	    push(@records_file, $_ . "\n");
	    $state = 1;
	    next;
	}
    }
    close (IN);
    if ($file_modified) {
	&records_write_file($file, *records_file);
    } else {
	print "$file unchanged.\n" if ($verbose);
    }
}

# TODO: finish writing it
# will take alternate set of variables
# after format conversion, update the links in file. 
sub records_update_links {
    local($file, $val) = @_;
    local(@records_file, $file_modified);
    local($newdate, $newdir);

    local($link_regexp) = "<(.*)" . $records_glob_regexp[0] . "("
	. $records_glob_regexp[1] . ")(#[0-9]*\\* .*)>";

    if (!open(IN, "< $file")) {
        warn("$0: cannot open $file.  Skipped.\n");
        return;
    }
    while (<IN>) {
	if (!/$link_regexp/) {
	    push(@records_file, $_);
	} else {
	    $file_modified = 1 unless ($file_modified);
	    $newdate = &records_denormalize_date(&records_normalize_date($2),
					       $val);
	    $newdir = &records_directory_path($newdate, 0, $val);
	    # TODO: broken
	    push(@records_file, $` . "<" . $newdir . "/" . $newdate 
		 . $3 . ">" . $');
	}
    }
    close(IN);
    if ($file_modified) {
	&records_write_file($file, *records_file);
    } else {
	print "$file unchanged.\n" if ($verbose);
    }
}

sub records_add_record_to_rawindex {
    local($subject, $date) = @_;
    local($ndate) = &records_normalize_date($date);
    local($key) = $subject . $ndate;

    # add the date and count of number of subjects to the date index
    if (!exists($records_date_index{$ndate})) {
	$records_date_index{$ndate} = 1;
    } else {
	$records_date_index{$ndate}++;
    }
    if (!exists($records_raw_index{$key})) {
	$records_raw_index{$key} = 0;
	return '';
    } else {
	return sprintf("%d", ++$records_raw_index{$key});
    }
}
    
sub records_make_index {
    local($subject_ndate);
    local($subject, $old_subject);
    local($ndate, $date);
    local($tags, $i);
 
    if ( -T "$records_index_file" ) {
	rename("$records_index_file", "$records_index_file~") ||
	    die("Could not rename $records_index_file: $!\n");
    }
    open(INDEX, ">$records_index_file") || 
	die("Could not open $records_index_file for writing: $!\n");
    # index start up 
    print INDEX "-*- records-index -*- ";
    foreach $subject_ndate (sort keys(%records_raw_index)) {
	$subject = substr($subject_ndate, 0, -8);
	$ndate = substr($subject_ndate, -8);
	$date = &records_denormalize_date($ndate);
	$tags = $records_raw_index{$subject_ndate};
	if ($subject ne $old_subject) {
	    # new subject. write it out
	    print INDEX "\n", $subject, ": ";
	    $old_subject = $subject;
	}
	# write out the first tag
	print INDEX $date . " ";
	# write out other tags if they exist
	for ($i = 1; $i <= $tags; $i++) {
	    print INDEX $date . "#" . sprintf("%d", $i) . " ";
	}
    }
    print INDEX "\n";
    close(INDEX);
}

sub records_make_dindex {
    local($date, $ndate);

    if ( -T "$records_dindex_file" ) {
	rename("$records_dindex_file", "$records_dindex_file~") ||
	    die("Could not rename $records_dindex_file: $!\n");
    }
    open(DINDEX, ">$records_dindex_file") || 
	die("Could not open $records_dindex_file for writing: $!\n");
    # print DINDEX " "; 
    foreach $ndate (sort keys (%records_date_index)) {
	$date = &records_denormalize_date($ndate);
	print DINDEX $date, "#", $records_date_index{$ndate}, " ";
    }
    print DINDEX "\n"; # add a newline at the end of file to make emacs happy
    close (DINDEX);
}

sub records_install_init_file {
    local($init) = &records_read_init_file();
    local($val);
    if ($init) {
	print "You have a valid $records_init_file.
I will use its values to initialize your records.
Unless you know what you are doing, do not change these values.
If you wish to change the date or directory format, use 
$0 -c\n\n";
	print "Do you still want to re-initialize your init file (y or n) ";
	chop($val= <>);
	if ($val eq "n") {
            # write out the init file
            &records_write_init_file($init);
	    return;
	}
    }

    print "The default values of the variables are shown in parenthesis.
If you want the default value, press return.\n\n";

    # query the user for values
    &records_query_vars();
    # write out the init file
    &records_write_init_file($init);
    # Add stuff to emacs init file
    open(EIT, ">>$records_emacs_init_file") ||
	die "Could not open $records_emacs_init_file for writing: $!\n";
    print EIT "(load \"$records_init_file\")\n";
    close EIT;
    &mkdirhier($records_directory, 0700) unless ( -d $records_directory);

}

sub records_convert_format {
    local($file, @records_files, $newfile, $newdir);
    local($date, $ndate, $newdate);
    
    if (!&records_read_init_file()) {
	print "Records init file has not been initialized.\n";
	print "Run $0 -i to initialize the init file before
attempting to convert records formats.\n\n";
	exit(1);
    }
    # original set of variables has been read and initialized
    &records_initialize_vars(1);
    # read the new set of vars.
    print "You must input the new values of the records variables.\n\n";
    &records_query_vars($alternate);
    &records_initialize_vars(0, $alternate);
    @records_files = &records_find_records();
    foreach $file (@records_files) {
	$date = basename($file);
	$ndate = &records_normalize_date($date);
	$newdate = &records_denormalize_date($ndate, $alternate);
	$newdir = &records_directory_path($newdate, 1, $alternate);
	$newfile =  $newdir . "/" . $newdate;
	if ($file eq $newfile) {
	    die "The old and new variable settings are the same.
We will not go any further!!\n";
	}
	&mkdirhier($newdir, 0700) unless ( -d $newdir);
	rename($file, $newfile) ||
	    warn "Couldn't rename $file to $newfile: $!\n";
	&records_update_links($newfile, $alternate);
    }
    &records_write_init_file(1, $alternate);
}

sub records_recreate_indexes {
    local(@records_files);

    &records_initialize_vars(1);
    @records_files = &records_find_records();

    foreach (@records_files) {
	&records_update_file($_);
    }
    &records_make_index();
    &records_make_dindex();
}

# This function might have to be changed for different emacs flavors
# (native,cygwin) (emacs, xemacs).
sub cygwin_translate_path {
    local ($path) = @_;
    local ($npath);

    # convert path to windows path
    $npath=`$cygpath -w $path`;
    chop ($npath);
    #print "path is $npath\n";

    # Fix cygwin bug where even if $path has no trailing slash, $npath may 
    # have it (when $path is a windows drive)
    if (!($path =~ m#.*\\$#) && ($npath =~ m#.*\\$#)) {
          chop ($npath);
    }
    $path = $npath;

    # convert backslashes to slashes for emacs
    $path =~ s#\\#/#g;
    #print "translated path is $npath\n";
    return $path;
}