-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwhatsapp-date.sh
executable file
·145 lines (120 loc) · 2.69 KB
/
whatsapp-date.sh
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
#!/usr/bin/env bash
set -euo pipefail
# Allows running in debug mode by setting the TRACE environment variable.
# e.g. <TRACE=1 ./yt-album.sh>
if [[ "${TRACE-0}" == "1" ]]; then set -o xtrace; fi
GREEN=$(tput setaf 2)
YELLOW=$(tput setaf 3)
RED=$(tput setaf 1)
RESET=$(tput sgr0)
DRY_RUN=
log_succ() {
printf "${GREEN}%s${RESET}\n" "${*}"
}
log_warn() {
printf "${YELLOW}%s${RESET}\n" "${*}" 1>&2
}
log_err() {
printf "${RED}%s${RESET}\n" "${*}" 1>&2
}
usage() {
printf \
'Usage: whatsapp-date.sh <directory>
Options:
-h, --help
Show this help message and exit.
--dry-run
Run without modifying images.
--no-color
Disable colored output.
Examples:
./whatsapp-date.sh ./images/
# Debug mode
TRACE=1 ./whatsapp-date.sh ./images/
'
}
die() {
log_err "${*}"
usage
exit 1
}
while :; do
case ${1-} in
# Two hyphens ends the options parsing
--)
shift
break
;;
-h | --help | help | "")
usage
exit
;;
--dry-run)
DRY_RUN=1
;;
--no-color)
GREEN=""
YELLOW=""
RED=""
RESET=""
;;
# Anything remaining that starts with a dash triggers a fatal error
-?*)
die "The command line option is unknown: $1"
;;
# Anything remaining is treated as content not a parseable option
*)
break
;;
esac
shift
done
[[ ! -d $1 ]] && echo "Directory does not exist!" && usage && exit 1
dir="$1"
min_date=$(date -d "2000-01-01 00:00:00" --rfc-3339=s)
max_date=$(date -d "2099-12-31 23:59:59" --rfc-3339=s)
success=0
total=0
for name in "$dir/"*; do
total=$((total + 1))
filename="$(basename "$name")"
# Check expected filename format.
# See <https://regex101.com/r/SWfL4C/1>
if [[ ! $filename =~ ^IMG-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]-WA[0-9][0-9][0-9][0-9]\.(jpg|jpeg|JPG|JPEG)$ ]]; then
log_warn "$filename is invalid! skipping ..."
continue
fi
# Check if date is valid.
if ! date="$(date -d "${filename:4:8}" --rfc-3339=s 2>/dev/null)"; then
log_warn "$filename is an invalid date! skipping ..."
continue
fi
# Sanity check 1.
if [[ $date < $min_date ]]; then
log_warn "$filename is before 2000-01-01! skipping ..."
continue
fi
# Sanity check 2.
if [[ $date > $max_date ]]; then
log_warn "$filename is after 2099-12-31! skipping ..."
continue
fi
new_date=${filename:4:8}
if [[ -z $DRY_RUN ]]; then
# Set access and modified date to parsed date.
touch -amt "${new_date}0900" "$name"
else
log_succ "$filename would be set to $new_date"
fi
success=$((success + 1))
done
msg="Changed $success of $total files."
if [[ $success -eq $total ]]; then
log_succ "$msg"
elif [[ $success -le 0 ]]; then
log_err "$msg"
exit 1
else
log_warn "$msg"
fi
exit 0