-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjrnltimestampfix
executable file
·108 lines (87 loc) · 2.65 KB
/
jrnltimestampfix
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
#!/bin/bash
# This script comes with ABSOLUTELY NO WARRANTY, use at own risk
# Copyright (C) 2017 Osiris Alejandro Gómez <[email protected]>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
DEFAULT_JRNL_DIR="$HOME/.jrnl"
DEFAULT_JRNL_LOCAL_DIR="$PWD/.jrnl"
die ()
{
printf "%s" "$RED[ERROR] $1 $NORMAL" && exit 1
}
if tput setaf 1 &> /dev/null
then
NORMAL=$(tput sgr0);
BRIGHT=$(tput bold);
BLUE=$(tput setaf 33);
GREEN=$(tput setaf 46);
RED=$(tput setaf 196);
WHITE=$(tput setaf 231);
YELLOW=$(tput setaf 11);
else
NORMAL="\e[0m";
BRIGHT="\e[1m";
BLUE="\e[1;34m";
GREEN="\e[1;32m";
RED="\e[1;31m";
WHITE="\e[1;37m";
YELLOW="\e[1;33m";
fi;
TMP=$(mktemp)
date '+%Y %m %d %H %M %S' > "$TMP"
while read -r YY mm dd HH MM SS
do
YEAR="$YY"
TODAY="$YY-$mm-$dd"
NOW="$HH:$MM"
MONTH="$mm"
done < "$TMP"
if [[ -z "$JRNL_DIR" ]]
then
if [[ ! -d "$DEFAULT_JRNL_LOCAL_DIR" ]]
then
JRNL_DIR="$DEFAULT_JRNL_DIR"
else
JRNL_DIR="$DEFAULT_JRNL_LOCAL_DIR"
fi
fi
##JRNL_DIR="$HOME/.jrnl"
DIR="$JRNL_DIR/$YEAR/$YEAR-$MONTH/$TODAY"
[[ ! -z "$1" ]] && DIR="$1"
[[ ! -d $(realpath "$DIR") ]] && die "invalid directory: $DIR"
find "$DIR" -follow -type f -iname '*.txt' | while read F
do
D=$(dirname "$F")
FILE=$(basename "$F")
SS=$(echo "$FILE" | egrep -o "[0-9]+" | cut -c 5-6)
[[ -z "$SS" ]] && SS='00'
[[ "$SS" = "0" ]] && SS='00'
HEAD=$(head -1 "$F")
HEAD_DATE=$(echo "$HEAD" | cut -c -10 | egrep -o "[0-9]{4}-[0-9]{2}-[0-9]{2}")
HEAD_TIME=$(echo "$HEAD" | cut -c 12-16 | egrep -o "[0-9]{2}:[0-9]{2}")
HM=$(echo "$HEAD_TIME" | tr -d ':')
YY=$(echo "$HEAD_DATE" | cut -d '-' -f1)
MM=$(echo "$HEAD_DATE" | cut -d '-' -f2)
[[ -z "$HEAD_DATE" ]] && die "invalid DATE: $HEAD_DATE in: $F"
[[ -z "$HM" ]] && die "invalid HOUR: $HM in: $F"
[[ -z "$YY" ]] && die "invalid YEAR: $YY in: $F"
[[ -z "$MM" ]] && die "invalid MONTH: $MM in: $F"
NEW_DIR="$JRNL_DIR/$YY/$YY-$MM/$HEAD_DATE"
NEW_FILE=$(printf "%6s.txt" "$HM$SS")
if [[ "$D" = "$NEW_DIR" ]]
then
[[ "$FILE" = "$NEW_FILE" ]] && continue
fi
mv -vi "$F" "$NEW_DIR/$NEW_FILE"
done