-
Notifications
You must be signed in to change notification settings - Fork 4
/
iphoto-cleanup.sh
executable file
·267 lines (223 loc) · 5.02 KB
/
iphoto-cleanup.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
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
#!/usr/bin/env bash
__DEBUG=1
source include arrays filesize
function decho
{
(( __DEBUG )) && echo "$@"
}
# See: http://fvue.nl/wiki/Bash:_Passing_variables_by_reference
# Usage: local "$1" && upvar $1 "value(s)"
upvar() {
if unset -v "$1"; then # Unset & validate varname
if (( $# == 2 )); then
eval $1=\"\$2\" # Return single value
else
eval $1=\(\"\${@:2}\"\) # Return array
fi
fi
}
# array_shift <array_var_name> into <var_name>
function array_shift
{
(( $# > 1 )) && if [[ $2 != "into" ]]
then
echo "Invalid arguments passed to $FUNCNAME: $@" >&2
return 1
fi
local __array_var_name=$1
local __var_name=$3
local __first=
local __rest=
e="$( declare -p "$__array_var_name" )";
e=${e#*=}
e=${e#\'}
e=${e%\'}
eval "declare E=$e"
(( ${#E[@]} < 1 )) && return 1
set -- "${E[@]}"
# echo "Positional parameters are 1:$1 2:$2 3:$3 4:$4 etc..."
test -n "$__var_name" && local "$__var_name" && upvar $__var_name "$1"
shift
A=( "$@" )
eval $__array_var_name='("${A[@]}")'
# local "$__array_var_name" && upvar $__array_var_name "$@"
# upvar didn't work so well when the array got down to a single member (tried to set it to a flat variable)
#
# # alias get_array_by_ref='e="$( declare -p ${1} )"; eval "declare -A E=${e#*=}"'
# # KEYS=( "${!E[@]}" )
}
unset EXPLODED
declare -a EXPLODED
function explode
{
local c=$#
(( c < 2 )) &&
{
echo function "$0" is missing parameters
return 1
}
local delimiter="$1"
local string="$2"
local limit=${3-99}
local tmp_delim=$'\x07'
local delin=${string//$delimiter/$tmp_delim}
local oldifs="$IFS"
IFS="$tmp_delim"
EXPLODED=($delin)
IFS="$oldifs"
}
function basename
{
local __rv="${1##*/}"
local "$3" && upvar $3 "$__rv"
}
function dirname
{
local __rv="${1%/*}"
local "$3" && upvar $3 "$__rv"
}
function extension
{
local __rv="${1##*.}"
local "$3" && upvar $3 "$__rv"
}
function noextension
{
local __rv="${1%.*}"
local "$3" && upvar $3 "$__rv"
}
function is_jpeg_ext
{
local __rv=1
shopt -s nocasematch
[[ $1 == jpg ]] && __rv=0
shopt -u nocasematch
return $__rv
}
function process
{
local __sfn=$1
local __tfn="Cleaned/$2"
local __tdir=
dirname "$__tfn" into __tdir
[ ! -d "$__tdir" ] && mkdir -p "$__tdir"
local __ssize=
local __tsize=0
filesize "$__sfn" into __ssize
[ -e "$__tfn" ] && filesize "$__tfn" into __tsize
echo "ssize: $__ssize tsize: $__tsize"
(( __ssize > __tsize )) && cp "$__sfn" "$__tfn"
}
cat files.iphoto.txt |
while read -r fn
do
# decho $fn
dirname "$fn" into dn
basename "$fn" into bn
extension "$bn" into ext
noextension "$bn" into noext
# decho $dn/$bn "($ext)"
[[ $dirname =~ .*AppleDouble.* ]] && continue
[[ $fn =~ DS_Store ]] && continue
# [[ $fn == $__target ]] && continue
# declare -p BASH_REMATCH
# ./Xmas 2005/FEB252E4781948F18479E7567CB803E5.jpg: JPEG image data, EXIF standard
test -e "$fn" || continue
test -s "$fn" ||
{
decho "Empty: $fn";
continue
}
__target="$dn/$noext.$ext"
filetype="$( file "$fn" )"
filetype="${filetype#*: }"
case "$filetype" in
("Composite Document File V2"*)
ft=thm
decho Thumbs.db: "$fn"
;;
"Apple binary property list" )
ft=plist
;;
*JPEG* )
ft=jpg
explode "/" "$fn"
# declare -p EXPLODED
# JPEG (514319): ./iPhoto Library Recovered Photos/AEA5346FACD24CC88CA5B9AD49784D60
# JPEG (1565908): ./iPhoto Library/Modified/2003/pandita/IMG_0802.JPG
# JPEG (52604): ./iPhoto Library/Data.noindex/2000/Bitch/DSCF0574.jpg
# while array_shift EXPLODED into __subdir
# do
# echo "shifted: $__subdir"
# done
# continue
__basepath=
array_shift EXPLODED
case "${EXPLODED[0]}" in
"iPhoto Library Recovered Photos" )
array_shift EXPLODED
__basepath="Recovered"
;;
"iPhoto Library" )
array_shift EXPLODED
;;
* )
echo "Unknown first subdir: ${EXPLODED[0]}"
echo
continue
;;
esac
# array_shift EXPLODED into __subdir
# declare -p EXPLODED
while true
do
case "${EXPLODED[0]}" in
"Modified" )
array_shift EXPLODED into __subdir
;;
"Data.noindex" )
array_shift EXPLODED into __subdir
;;
"Originals" )
array_shift EXPLODED
;;
20?? )
array_shift EXPLODED
;;
* )
echo "Unknown second subdir: '${EXPLODED[0]}'"
declare -p EXPLODED
echo
if (( ${#EXPLODED[@]} == 1 ))
then
__fp="${__basepath}/${EXPLODED[*]}"
elif (( ${#EXPLODED[@]} == 2 ))
then
__fp="${EXPLODED[0]}/${EXPLODED[1]}"
else
echo "Path too long: ${EXPLODED[@]}"
break
fi
process "$fn" "$__fp"
break
;;
esac
done
continue
false &&
if [ -e "$__target" ]
then
diff "$fn" "$__target" ||
{
echo "Diff error: \"$fn\" and \"$__target\""
continue
}
fi
decho JPEG "($size)": "$fn"
;;
* )
echo "UNKNOWN ($filetype): $__target"
ft=
esac
done
# check for short arg within long arg