-
Notifications
You must be signed in to change notification settings - Fork 1
/
lib43f
1084 lines (978 loc) · 33.8 KB
/
lib43f
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
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env bash
# find using extended regular expressions
function Efind() {
local path="$1"
shift
local regex="$1"
shift
case "$(uname)" in
Darwin)
find -E "$path" -regex "$regex" "$@"
;;
Linux)
find "$path" -regextype posix-extended -regex "$regex" "$@"
;;
OpenBSD)
find "$path" "$@" | grep -E "$regex"
;;
esac
}
# sed using extended regular expressions
function Esed() {
case "$(uname)" in
Darwin|OpenBSD)
sed -E "$@"
;;
Linux)
sed -r "$@"
;;
esac
}
# calculate dates relative to specified date
# in_format & out_format accept a strftime() format
# in_date is a date in the format specified in in_format
# adjust accepts a `dadd` duration unit ('y', 'mo', 'd', 'h', 'm', or 's')
function relative_date() {
local in_format="$1"
local in_date="$2"
local adjust="$3"
local out_format="$4"
# in_format string must not start with a plus symbol
if [[ "$in_format" =~ ^[+] ]]; then
return 1
fi
# out_format string must start with a plus symbol
if [[ "$out_format" =~ ^[^+] ]]; then
return 1
fi
# dadd doesn't handle zero offsets, so use date
if [[ "$adjust" =~ ^[+-]?0(y|mo|d|h|m|s)$ ]]; then
dconv -f "${out_format#+}" -i "$in_format" "$in_date"
else
dadd -f "${out_format#+}" -i "$in_format" "$in_date" "$adjust"
fi
}
# calculate dates relative to specified date
function relative_to_now() {
local adjust="$1"
local out_format="$2"
# out_format string must start with a plus symbol
if [[ (-z "$out_format") || ("$out_format" =~ ^[^+]) ]]; then
return 1
fi
relative_date "%Y-%m-%d %H:%M:%S" "$(date "+%Y-%m-%d %H:%M:%S")" "$adjust" "$out_format"
}
# calculate number of days in previous month
function days_in_previous_month() {
relative_date "%Y-%m-%d" "$(date "+%Y-%m-01")" "-1d" "+%d"
}
# convert a date format string to a regular expression
function parse_date_format_value() {
local success=true
local in_format="$1"
local in_date="$2"
local parse_sequence="$3"
local match_format="$in_format"
# build a regular expression based on the date format
# replace %Y with four digit year match
if [ "$parse_sequence" = "%Y" ]; then
match_format=$(echo -n "$match_format" | Esed "s/%Y/([0-9]{4})/g")
else
match_format=$(echo -n "$match_format" | Esed "s/%Y/[0-9]{4}/g")
fi
# replace %m with two digit month match
if [ "$parse_sequence" = "%m" ]; then
match_format=$(echo -n "$match_format" | Esed "s/%m/(0[123456789]|1[012])/g")
else
match_format=$(echo -n "$match_format" | Esed "s/%m/(?:0[123456789]|1[012])/g")
fi
# replace %d with two digit day match
if [ "$parse_sequence" = "%d" ]; then
match_format=$(echo -n "$match_format" | Esed "s/%d/(0[123456789]|[12][0-9]|3[01])/g")
else
match_format=$(echo -n "$match_format" | Esed "s/%d/(?:0[123456789]|[12][0-9]|3[01])/g")
fi
# replace %H with two digit hour match
if [ "$parse_sequence" = "%H" ]; then
match_format=$(echo -n "$match_format" | Esed "s/%H/([01][0-9]|2[0123])/g")
else
match_format=$(echo -n "$match_format" | Esed "s/%H/(?:[01][0-9]|2[0123])/g")
fi
# replace %M with two digit minute match
if [ "$parse_sequence" = "%M" ]; then
match_format=$(echo -n "$match_format" | Esed "s/%M/([012345][0-9])/g")
else
match_format=$(echo -n "$match_format" | Esed "s/%M/[012345][0-9]/g")
fi
# replace %S with two digit seconds match
if [ "$parse_sequence" = "%S" ]; then
match_format=$(echo -n "$match_format" | Esed "s/%M/([012345][0-9])/g")
else
match_format=$(echo -n "$match_format" | Esed "s/%M/[012345][0-9]/g")
fi
# replace %% with a single % (this should always be done last)
match_format=$(echo -n "$match_format" | Esed "s/%%/%/g")
# use the regex to parse the sequence out of the actual date (use grep with PCRE as we need to use non-capturing groups)
local parsed_sequence="$(perl -pe "s/${match_format}/\1/g" <<< "$in_date")"
if [ $? -eq 0 ]; then
echo -n "$parsed_sequence"
else
success=false
fi
$success;
}
# convert date strings
function convdatestr() {
local in_format="$1"
local in_date="$2"
local out_format="$3"
# input format string can't be empty
if [ -z "$in_format" ]; then
return 1
fi
case "$(uname)" in
Darwin|OpenBSD)
date -j -f "$in_format" "$in_date" "$out_format"
;;
Linux)
local has_year=false
local has_month=false
local has_day=false
local has_hour=false
local has_minute=false
local has_second=false
local year
local month
local day
local hour
local minute
local second
local tmp_date
# parse out custom date
if [[ "$in_format" =~ "%Y" ]]; then
has_year=true
year="$(parse_date_format_value "$in_format" "$in_date" "%Y")"
fi
if [[ "$in_format" =~ "%m" ]]; then
has_month=true
month="$(parse_date_format_value "$in_format" "$in_date" "%m")"
fi
if [[ "$in_format" =~ "%d" ]]; then
has_day=true
day="$(parse_date_format_value "$in_format" "$in_date" "%d")"
fi
if [[ "$in_format" =~ "%H" ]]; then
has_hour=true
hour="$(parse_date_format_value "$in_format" "$in_date" "%H")"
fi
if [[ "$in_format" =~ "%M" ]]; then
has_minute=true
minute="$(parse_date_format_value "$in_format" "$in_date" "%M")"
fi
if [[ "$in_format" =~ "%S" ]]; then
has_second=true
second="$(parse_date_format_value "$in_format" "$in_date" "%S")"
fi
# output the date into an intermediary format that Linux's date command understands
if $has_year && $has_month && $has_day; then
tmp_date="${year}-${month}-${day}"
fi
if $has_hour && $has_minute; then
tmp_date="${tmp_date} ${hour}:${minute}"
if $has_second; then
tmp_date="${tmp_date}:${second}"
fi
fi
date -d "$tmp_date" "$out_format"
;;
esac
}
# validate a day
function valid_day() {
local success=false
if (( ( 10#$1 >= 1 ) && ( 10#$1 <= 31 ) )); then
success=true
fi
$success
}
# validate a month
function valid_month() {
local success=false
if (( ( 10#$1 >= 1 ) && ( 10#$1 <= 12 ) )); then
success=true
fi
$success
}
# validate a year
function valid_year() {
local success=false
if [[ "$1" =~ ^[0-9]{4}$ ]]; then
success=true
fi
$success
}
# validate a file mode
function valid_mode() {
local success=false
if [[ "$1" =~ ^[0-7]{3,4}$ ]]; then
success=true
fi
$success
}
# load the config file
function load_config() {
local success
local var
local val
local config_file
success=true
config_file="$1"
# does the config file exist?
if [ -n "$config_file" -a -e "$config_file" ]; then
$verbose && echo "Loading config file '${config_file}'..."
while IFS== read var val; do
case "$var" in
\#*)
# do nothing - this would be a comment
;;
repository)
if [ -n "$val" -a -d "$val" ]; then
config_repository="$val"
$verbose && echo " '${var}' set to '${config_repository}'."
else
printf " ERROR! '%s' value in config file is either invalid or not a directory! You may need to run '43f init' to initialize the repository.\n" "$var"
notify "UNKNOWN" "ERROR" "'${var}' value in '${config_file} configuration file is either invalid or is not a directory! You may need to run '43f init' to initialize the repository."
success=false
fi
;;
repository_mode)
if valid_mode "$val"; then
if [ -n "$mode" ]; then
$verbose && echo " '${var}' NOT set to '${val}' because it has been overridden by command line option ('${mode}')."
else
mode="$val"
$verbose && echo " '${var}' set to '${mode}."
fi
else
printf " ERROR! '%s' value in config file is not valid absolute (octal) file mode!\n" "$var"
notify "UNKNOWN" "ERROR" "'${var}' value in '${config_file}' configuration file is not a valid absolute (octal) file mode!"
success=false
fi
;;
notify)
if echo "$val" | grep -q -i -E '^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}$' ; then
config_notify_email="$val"
$verbose && echo " '${var}' set to '${config_notify_email}'."
else
printf " ERROR! '%s' value in config file is not a valid email address!\n" "$var"
notify "UNKNOWN" "ERROR" "'${var}' value in '${config_file}' configuration file is not a valid email address!"
success=false
fi
;;
days)
if valid_day "$val"; then
config_keep_days="$val"
$verbose && echo " '$var' set to '$config_keep_days'."
else
echo " ERROR! '$var' value in config file is out of range (1-31)!"
notify "UNKNOWN" "ERROR" "'${var}' value in '${config_file}' configuration file is out of range (1-31)!"
success=false
fi
;;
months)
if valid_month "$val"; then
config_keep_months="$val"
$verbose && echo " '$var' set to '$config_keep_months'."
else
echo " ERROR! '$var' value in config file is out of range (1-12)!"
notify "UNKNOWN" "ERROR" "'${var}' value in '${config_file}' configuration file is out of range (1-12)!"
success=false
fi
;;
years)
if (( "$val" >= 1 )); then
config_keep_years="$val"
$verbose && echo " '$var' set to '$config_keep_years'."
else
echo " ERROR! '$var' value in config file is out of range (1+)!"
notify "UNKNOWN" "ERROR" "'${var}' value in '${config_file}' configuration file is out of range (1+)!"
success=false
fi
;;
datestamp)
if [[ "$val" =~ ^[YmdHMS%.-]+$ ]]; then
config_date_format="$val"
$verbose && echo " '$var' set to '$config_date_format'."
else
echo " ERROR! '$var' value in config file is out of range!"
notify "UNKNOWN" "ERROR" "'${var}' value in '${config_file}' configuration file is out of range!"
success=false
fi
;;
*)
printf " Warning: Ignored unknown configuration variable '%s'.\n" "$var"
;;
esac
done < "$config_file"
$verbose && echo ' Done.'
else
$verbose && echo "ERROR! Config file '${config_file}' does not exist!"
notify "UNKNOWN" "ERROR" "The '${config_file}' does not exist!"
success=false
fi
$success
}
# determine whether repository exists
function does_repository_exist() {
local success
local repo
success=true
repo="$1"
if [ -z "$repo" -o ! -d "$repo" ]; then
success=false
fi
$success
}
# determine whether repository has been initialized
function is_repository_initialized() {
local success=true
local repo="$1"
# check that year directory exists
if [ ! -d "${repo}/${y}" ]; then
success=false
fi
# check that month directories exist
local month
for (( month=1; month<=12; month++ )); do
local month_dir
printf -v month_dir "%s/%s/m%02i" "$repo" "$y" "$(( 10#$month ))"
if [ ! -d "$month_dir" ]; then
success=false
fi
done
# check that day directories exist
local day
for (( day=1; day<=31; day++ )); do
local day_dir
printf -v day_dir "%s/%s/d%02i" "$repo" "$y" "$(( 10#$day ))"
if [ ! -d "$day_dir" ]; then
success=false
fi
done
$success
}
# convert a date format string to a regular expression
#
# # this and compare_date_stamped_filenames() use `sed`/`grep`, but should use something like http://sprunge.us/LZjS
function date_format_to_regex() {
local success=true
local date_format="$1"
# replace %Y with four digit year match
date_format=$(echo -n "$date_format" | Esed "s/%Y/([0-9]{4})/g")
# replace %m with two digit month match
date_format=$(echo -n "$date_format" | Esed "s/%m/(0[123456789]|1[012])/g")
# replace %d with two digit day match
date_format=$(echo -n "$date_format" | Esed "s/%d/(0[123456789]|[12][0-9]|3[01])/g")
# replace %H with two digit hour match
date_format=$(echo -n "$date_format" | Esed "s/%H/([01][0-9]|2[0123])/g")
# replace %M/%S with two digit minute/second match
date_format=$(echo -n "$date_format" | Esed "s/(%M|%S)/([012345][0-9])/g")
# replace %% with a single % (this should always be done last)
date_format=$(echo -n "$date_format" | Esed "s/%%/%/g")
echo -n "$date_format"
$success;
}
# compare two date stamped filenames (note: datestamp must match
# returns 0 for no match, 1 if string1's timestamp is higher, or 2 if string2's datestamp is higher
#
# this and date_format_to_regex() use `sed`/`grep`, but should use something like http://sprunge.us/LZjS
function compare_date_stamped_filenames() {
local match=0
local file1="$1"
local file2="$2"
local date_regex=$(date_format_to_regex "$config_date_format")
# are the filenames the same length? we have to be dealing with the same date format here!
if [ ${#file1} -eq ${#file2} ]; then
# do our files have a datestamp matching our format?
if [[ "$file1" =~ $date_regex && "$file2" =~ $date_regex ]]; then
# do the filenames match if we strip out those datestamps?
local file1_stripped=$(echo -n "$file1" | Esed "s/${date_regex}//g")
local file2_stripped=$(echo -n "$file2" | Esed "s/${date_regex}//g")
if [ "$file1_stripped" = "$file2_stripped" ]; then
# now we compare the dates, fortunately we use `date` compatible date formats so this is
# straightforward to extract the date from the filename & convert to epoch time
local file1_date=$(echo -n "$file1" | grep -o -E "$date_regex")
local file2_date=$(echo -n "$file2" | grep -o -E "$date_regex")
file1_date="$(convdatestr "$config_date_format" "$file1_date" "+%s")"
file2_date="$(convdatestr "$config_date_format" "$file2_date" "+%s")"
if [ $file1_date -gt $file2_date ]; then
match=1
elif [ $file1_date -lt $file2_date ]; then
match=2
fi
fi
fi
fi
return $match
}
# initialize the repository
function init_repository() {
local success=true
local repo="$1"
local year="$2"
local mode="$3"
# default to this year (if one wasn't specified)
if [ -z "$year" ]; then
year="$y"
# otherwise, validate the year input
elif [[ ! "$year" =~ ^[0-9]{4}$ ]]; then
echo "ERROR! Cannot initialize repository with invalid year '$year'!"
notify "UNKNOWN" "ERROR" "Cannot initialize repository with invalid year '$year'!"
success=false
fi
# validate the file mode
if [ -n "$mode" ] && ! valid_mode "$mode"; then
echo "ERROR! Cannot initialize repository with invalid absolute (octal) file mode '$mode'!"
notify "UNKNOWN" "ERROR" "Cannot initialize repository with invalid absolute (octal) file mode '$mode'!"
success=false
fi
# does the repository exist?
if [ -z "$repo" ]; then
echo 'ERROR! Cannot initialize repository with empty path!'
notify "UNKNOWN" "ERROR" "Cannot initialize repository with empty path!"
success=false
elif $success; then
$verbose && echo -n "Repository directory '$repo' "
if does_repository_exist "$repo"; then
$verbose && echo 'exists.'
else
$verbose && echo -n 'does not exist. '
if [ -n "$mode" ]; then
$verbose && echo -n 'Creating with mode '$mode'... '
if $dry_run || mkdir -p -m "$mode" "$repo"; then
$verbose && echo 'Done.'
else
$verbose && echo 'Error!'
notify "$repo" "ERROR" "An unknown error occurred while attempting to create the '${repo}' repository directory!"
success=false
fi
else
$verbose && echo -n 'Creating... '
if $dry_run || mkdir -p "$repo"; then
$verbose && echo 'Done.'
else
$verbose && echo 'Error!'
notify "$repo" "ERROR" "An unknown error occurred while attempting to create the '${repo}' repository directory!"
success=false
fi
fi
fi
fi
# check for year subdirectory & create if necessary
if $success; then
year_dir="$repo/$year"
$verbose && echo -n "Year Directory '${year_dir}' "
if [ -d "$year_dir" ] ; then
$verbose && echo "exists."
else
$verbose && echo -n "does not exist. "
if [ -n "$mode" ]; then
$verbose && echo -n "Creating with mode '$mode'... "
if $dry_run || mkdir -m "$mode" "$year_dir" ; then
$verbose && echo "Done."
else
$verbose && echo "ERROR!"
notify "$repo" "ERROR" "An unknown error occurred while attempting to create the '${year_dir}' year directory!"
success=false
fi
else
$verbose && echo -n "Creating... "
if $dry_run || mkdir "$year_dir" ; then
$verbose && echo "Done."
else
$verbose && echo "ERROR!"
notify "$repo" "ERROR" "An unknown error occurred while attempting to create the '${year_dir}' year directory!"
success=false
fi
fi
fi
fi
# step through the month directories, checking for them & creating as necessary
if $success; then
local month
for (( month=1; month<=12; month++ )); do
local month_dir
# check for month directory
printf -v month_dir "%s/%s/m%02d" "$repo" "$year" "$(( 10#$month ))"
$verbose && echo -n "Month Directory ${month_dir} "
if [ -d "$month_dir" ]; then
$verbose && echo "exists."
else
# create the month directory
$verbose && echo -n "does not exist. "
if [ -n "$mode" ]; then
$verbose && echo -n "Creating with mode '$mode'... "
if $dry_run || mkdir -m "$mode" "$month_dir"; then
$verbose && echo "Done."
else
$verbose && echo "ERROR!"
notify "$repo" "ERROR" "An unknown error occurred while attempting to create the '${month_dir}' month directory!"
success=false
fi
else
$verbose && echo -n "Creating... "
if $dry_run || mkdir "$month_dir"; then
$verbose && echo "Done."
else
$verbose && echo "ERROR!"
notify "$repo" "ERROR" "An unknown error occurred while attempting to create the '${month_dir}' month directory!"
success=false
fi
fi
fi
done
fi
# step through the day directories, checking for them & creating as necessary
if $success; then
local day
for (( day=1; day<=31; day++ )); do
local day_dir
# check for the day directory
printf -v day_dir "%s/%s/d%02d" "$repo" "$year" "$(( 10#$day ))"
$verbose && echo -n "Day Directory ${day_dir} "
if [ -d "$day_dir" ]; then
$verbose && echo "exists."
else
# create the day directory
$verbose && echo -n "does not exist. "
if [ -n "$mode" ]; then
$verbose && echo -n "Creating with mode '$mode'... "
if $dry_run || mkdir -m "$mode" "$day_dir"; then
$verbose && echo "Done."
else
$verbose && echo "ERROR!"
notify "$repo" "ERROR" "An unknown error occurred while attempting to create the '${day_dir}' day directory!"
success=false
fi
else
$verbose && echo -n "Creating... "
if $dry_run || mkdir "$day_dir"; then
$verbose && echo "Done."
else
$verbose && echo "ERROR!"
notify "$repo" "ERROR" "An unknown error occurred while attempting to create the '${day_dir}' day directory!"
success=false
fi
fi
fi
done
fi
$success
}
function init_repository_symlinks() {
local success
local repo
success=true
repo="$1"
if does_repository_exist "$repo" && is_repository_initialized "$repo"; then
# create symlink for 'today'
local today_dir
local today
today="$d"
today_dir="${repo}/${y}/d${today}"
$verbose && echo -n "Creating symlink for 'today' pointing to '$today_dir'... "
if $dry_run || ln -f -n -s "$today_dir" "${repo}/today"; then
$verbose && echo 'Done.'
else
$verbose && echo 'ERROR!'
notify "$repo" "ERROR" "An unknown error occurred while attempting to create the 'today' symlink pointing at the '${today_dir}' directory!"
success=false
fi
# create symlink for 'yesterday'
local yesterday_dir
local yesterday
local yesterday_year
yesterday="$(relative_to_now -1d +%d)"
yesterday_year="$(relative_to_now -1d +%Y)"
yesterday_dir="${repo}/${yesterday_year}/d${yesterday}"
$verbose && echo -n "Creating symlink for 'yesterday' pointing to '$yesterday_dir'... "
if $dry_run || ln -f -n -s "$yesterday_dir" "${repo}/yesterday"; then
$verbose && echo 'Done.'
else
$verbose && echo 'ERROR!'
notify "$repo" "ERROR" "An unknown error occurred while attempting to create the 'yesterday' symlink pointing at the '${yesterday_dir}' directory!"
success=false
fi
# create symlink for 'sunday' through 'saturday' for the past week (not including today)
local weekday_dir
local weekday_name
local weekday
local weekday_year
local i
for (( i=1; i<=7; i++ )); do
weekday="$(relative_to_now -${i}d +%d)"
weekday_year="$(relative_to_now -${i}d +%Y)"
weekday_dir="${repo}/${weekday_year}/d${weekday}"
weekday_name="$(relative_to_now -${i}d +%A | tr '[:upper:]' '[:lower:]')"
$verbose && echo -n "Creating symlink for '$weekday_name' pointing to '$weekday_dir'... "
if $dry_run || ln -f -n -s "$weekday_dir" "${repo}/${weekday_name}"; then
$verbose && echo 'Done.'
else
$verbose && echo 'ERROR!'
notify "$repo" "ERROR" "An unknown error occurred while attempting to create the '${weekday_name}' symlink pointing at the '${weekday_dir}' directory!"
success=false
fi
done
else
$verbose && echo "ERROR! Repository '${repo}' doesn't exist or isn't initialized!"
notify "$repo" "ERROR" "The '${repo}' repository doesn't exist or isn't initialized!"
success=false
fi
$success
}
function roll_repository_daily_to_monthly() {
local success=true
local repo="$1"
local year="$2"
local day="$3"
local month="$4"
local mtime_today="$5" # also include files modified today? "true"/"false"
if ! valid_year "$year" \
|| ! valid_day "$day" \
|| ! valid_month "$month"; then
return 1
fi
if does_repository_exist "$repo"; then
local src
local dst
local mtime=''
printf -v src "%s/%d/d%02d" "$repo" "$year" "$(( 10#$day ))"
printf -v dst "%s/%d/m%02d" "$repo" "$year" "$(( 10#$month ))"
$verbose && echo -n $'\n'" Moving files & directories from '$src' to '$dst' ("
if $mtime_today; then
$verbose && echo -n "including"
else
$verbose && echo -n "excluding"
mtime='-mtime +0'
fi
$verbose && echo -n " those modified today)... "
# step through all the matching (i.e. with or without a modification date of today) in the src dir and move them to dst
while IFS= read -r line; do
# don't bother processing an empty line or is the src directory itself
if [ -n "$line" ]; then
$verbose && echo -n $'\n'" '$line'... "
if $dry_run || mv "$line" "$dst/"; then
$verbose && echo "Done."
else
$verbose && echo "ERROR!"
notify "$repo" "ERROR" "An unknown error occurred while attempting to move the '${line}' file/directory into the '${dst}' directory!"
success=false
fi
fi
done <<< "$(find "$src" -mindepth 1 -maxdepth 1 $mtime| sort)"
$verbose && echo "Done."
else
$verbose && echo "ERROR! Repository '${repo}' doesn't exist!"
notify "$repo" "ERROR" "The '${repo}' repository doesn't exist!"
success=false
fi
$success
}
function roll_repository_monthly_to_monthly() {
local success=true
local repo="$1"
local year="$2"
local src_month="$3"
local dst_month="$4"
if ! valid_year "$year" \
|| ! valid_month "$src_month" \
|| ! valid_month "$dst_month"; then
return 1
fi
if does_repository_exist "$repo"; then
local src
local dst
printf -v src "%s/%d/m%02d" "$repo" "$year" "$src_month"
printf -v dst "%s/%d/m%02d" "$repo" "$year" "$dst_month"
$verbose && echo -n $'\n'" Moving files & directories from '$src' to '$dst'... "
# step through all the files in the src dir and move them to dst
while IFS= read -r line; do
# don't bother processing an empty line
if [ -n "$line" ]; then
$verbose && echo -n $'\n'" '$line'... "
if $dry_run || mv "$line" "$dst/"; then
$verbose && echo "Done."
else
$verbose && echo "ERROR!"
notify "$repo" "ERROR" "An unknown error occurred while attempting to move the '${line}' file/directory into the '${dst}' directory!"
success=false
fi
fi
done <<< "$(find "$src" -mindepth 1 -maxdepth 1 | sort)"
$verbose && echo "Done."
else
$verbose && echo "ERROR! Repository '${repo}' doesn't exist!"
notify "$repo" "ERROR" "The '${repo}' repository doesn't exist!"
success=false
fi
$success
}
function consolidate_repository_monthly() {
local success=true
local repo="$1"
local year="$2"
local month="$3"
if does_repository_exist "$repo"; then
local dir
printf -v dir "%s/%d/m%02d" "$repo" "$year" "$(( 10#$month ))"
local last_file=''
# step through all the files and directories and look for any with the same name except for a datestamp
while IFS= read -r line; do
# don't bother processing an empty line
if [[ ( -n "$line" ) && ( "$line" != "$dir" ) ]]; then
# if we don't have another file to compare against, we can't do anything
if [ -n "$last_file" ]; then
$verbose && echo -n $'\n'" Comparing '$last_file' with '$line'... "
compare_date_stamped_filenames "$last_file" "$line"
local match=$?
# don't do anthing if the filenames don't match
if [ $match -eq 0 ]; then
$verbose && echo "NO MATCH!"
# if the first file has a newer date stamp, then keep it and remove the second
elif [ $match -eq 1 ]; then
$verbose && echo -n "Keeping the former... "
if $dry_run || rm -r "$line"; then
$verbose && echo "Done."
else
$verbose && echo "ERROR!"
notify "$repo" "ERROR" "An unknown error occurred while attempting to delete the '${line}' file/directory during consolidation of files in the '${dir}' directory!"
success=false
fi
# if the second file has a newer date stamp, then keep it and remove the first
elif [ $match -eq 2 ]; then
$verbose && echo -n "Keeping the latter... "
if $dry_run || rm -r "$last_file"; then
$verbose && echo "Done."
else
$verbose && echo "ERROR!"
notify "$repo" "ERROR" "An unknown error occurred while attempting to delete the '${last_file}' file/directory during consolidation of files in the '${dir}' directory!"
success=false
fi
fi
$verbose && echo -n " Done."$'\n'" "
fi
# store this filename to compare with the next one
last_file="$line"
fi
done <<< "$(find "$dir" -mindepth 1 -maxdepth 1 | sort)"
else
$verbose && echo "ERROR! Repository '${repo}' doesn't exist!"
notify "$repo" "ERROR" "The '${repo}' repository doesn't exist!"
success=false
fi
$success
}
function purge_year_directories() {
local success=true
local repo="$1"
if does_repository_exist "$repo"; then
$verbose && echo
# calculate keep-through year
local keep_through="$(relative_to_now -$(( ${config_keep_years} - 1 ))y +%Y)"
# step through all the year directories that exist and delete any that are older than our "keep through" year
while IFS= read -r line; do
local year="$(basename "$line")"
if [[ "$year" =~ ^[0-9]{4}$ ]] \
&& (( $year < $keep_through )); then
$verbose && echo -n " Deleting $year directory... "
if $dry_run || rm -r "${repo}/${year}"; then
$verbose && echo "Done."
else
$verbose && echo "ERROR!"
notify "$repo" "ERROR" "An unknown error occurred while attempting to delete the '${repo}/${year}' year directory while purging old annual directories!"
success=false
fi
else
$verbose && echo " Preserving $year directory."
fi
done <<< "$(Efind "$repo" '.*/[0-9]{4}' -mindepth 1 -maxdepth 1 | sort -nr)"
$verbose && echo -n " "
else
$verbose && echo "ERROR! Repository '${repo}' doesn't exist!"
notify "$repo" "ERROR" "The '${repo}' repository doesn't exist!"
success=false
fi
$success
}
function roll_repository_directories() {
local success=true
local repo="$1"
if does_repository_exist "$repo"; then
# calculate today & keep-through dates
local today="$d"
local keep_through
local last_months_days="$(days_in_previous_month)"
if (( $config_keep_days > $last_months_days )); then
keep_through="$(relative_to_now -$(( $last_months_days - 1 ))d +%d)"
else
keep_through="$(relative_to_now -$(( $config_keep_days - 1 ))d +%d)"
fi
$verbose && echo "Keeping files through 'd${keep_through}'."
# step through the day subdirectories and "process" those that are outside the range to preserve (starting with yesterday)
local year="$(relative_to_now -1d +%Y)"
local month="$(relative_to_now -1d +%m)"
local day="$(relative_to_now -1d +%d)"
local day_dir=''
local month_dir=''
while (( 10#$day != 10#$today )); do
# build the directory paths
printf -v day_dir "%s/%d/d%02d" "$repo" "$year" "$(( 10#$day ))"
printf -v month_dir "%s/%d/m%02d" "$repo" "$year" "$(( 10#$month ))"
$verbose && echo -n "Processing directory '$day_dir'... "
# are we keeping a range that doesn't wrap (i.e. today is greater than the day through which we're keeping)?
if (( \
( 10#$today >= 10#$keep_through ) \
&& ( 10#$day >= 10#$keep_through ) \
&& ( 10#$day <= 10#$today ) \
)); then
$verbose && echo 'Keeping.'
# or, are we keeping a range that wraps (i.e. the day through which we're keeping is greater than today)?
elif (( \
( 10#$today <= 10#$keep_through ) \
&& ( \
( 10#$day <= 10#$today ) \
|| ( 10#$day >= 10#$keep_through ) \
) \
)); then
$verbose && echo 'Keeping.'
# only process if this is outside the range to keep
else
$verbose && echo -n $'\n'' Processing...'
# move the contents of the day's directory to the appropriate month's directory
if roll_repository_daily_to_monthly "$repo" "$year" "$day" "$month" true; then
$verbose && echo " Done."
else
echo 'ERROR!'
echo "A fatal error occurred while attempting to move the contents of '$day_dir' to '$month_dir'!"
echo 'Exiting.'
notify "$repo" "ERROR" "A fatal error occurred while attempting to move the contents of '${day_dir}' to '${month_dir}'!"
exit 1
fi
fi
# decrement the day & wrap around day, month, and year if we've gone under
day=$(( 10#$day - 1 ))
if (( 10#$day < 1 )); then
day=31
month=$(( 10#$month - 1 ))
if (( 10#$month < 1 )); then
month=12
(( year-- ))
fi
fi
done
# we always process today (but we have to be extra careful since there's the possibility new files have already been stashed in it)
if (( \
( $year == $y ) \
&& ( 10#$month == 10#$m ) \
)); then
month=$(( 10#$month - 1 ));
if (( 10#$month < 1 )); then
month=12
(( year-- ))
fi
fi
printf -v day_dir "%s/%d/d%02d" "$repo" "$year" "$(( 10#$day ))"
printf -v month_dir "%s/%d/m%02d" "$repo" "$year" "$(( 10#$month ))"
$verbose && echo -n "Processing directory '$day_dir'... "$'\n'" Processing (special)... "
# move the contents of the day's directory to the appropriate month's directory
if roll_repository_daily_to_monthly "$repo" "$year" "$day" "$month" false; then
$verbose && echo " Done."
else
echo 'ERROR!'
echo "A fatal error occurred while attempting to move the contents of '$day_dir' to '$month_dir'!"
echo 'Exiting.'
notify "$repo" "ERROR" "A fatal error occurred while attempting to move the contents of '$day_dir' to '$month_dir'!"
exit 1
fi
# calculate month keep-through & year dates
local keep_through_year="$(relative_to_now -$((${config_keep_months} - 1))mo +%Y)"
local keep_through_month="$(relative_to_now -$((${config_keep_months} - 1))mo +%m)"
# step through the month subdirectories and "process" those that are outside the range to preserve (starting with this month)
local year=''