-
Notifications
You must be signed in to change notification settings - Fork 4
/
cleanup.sh
executable file
·151 lines (130 loc) · 2.91 KB
/
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
#!/usr/bin/env bash
__DEBUG=1
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
}
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
}
find . -type f |
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)"
if [[ $dirname =~ ".*AppleDouble.*" ]]
then
continue
fi
# >f+++++++++ kodak/tmp/101B05924B20465E89910DC6ABD62B4D(1)
# declare -ar BASH_REMATCH='([0]="1CD89973FBA542C8A6D5F208EFDF44A1.jpg" [1]="1CD89973FBA542C8A6D5F208EFDF44A1" [2]=".jpg")'
# Check for MD5 (unnamed) files
if [[ "$bn" =~ "([A-F0-9]{32})(.*)" ]]
then
__target="$dn/${BASH_REMATCH[1]}.jpg"
[[ $fn == $__target ]] && continue
# declare -p BASH_REMATCH
filetype="$( file "$fn" )"
# ./Xmas 2005/FEB252E4781948F18479E7567CB803E5.jpg: JPEG image data, EXIF standard
filetype="${filetype##*: }"
case "$filetype" in
*JPEG* )
ft=jpg
if [ -e "$__target" ]
then
diff "$fn" "$__target" ||
{
echo "Diff error: \"$fn\" and \"$__target\""
continue
}
fi
decho mv -f "$fn" "$dn/${BASH_REMATCH[1]}.jpg"
mv -f "$fn" "$dn/${BASH_REMATCH[1]}.jpg"
;;
*CDF* )
ft=db
decho mv -f "$fn" "$dn/Thumbs.db"
mv -f "$fn" "$dn/Thumbs.db"
;;
* )
ft=
esac
elif [[ "$bn" =~ "(.*)(\([0-9]\))(.*)" ]]
then
# declare -p BASH_REMATCH
# declare -ar BASH_REMATCH='([0]="IMG_1869(1).JPG" [1]="IMG_1869" [2]="(1)" [3]=".JPG")'
# declare -ar BASH_REMATCH='([0]="MVI_2119(1).THM" [1]="MVI_2119" [2]="(1)" [3]=".THM")'
__target="$dn/${BASH_REMATCH[1]}${BASH_REMATCH[3]}"
if [ -e "$__target" ]
then
diff "$fn" "$__target" ||
{
echo "Diff error: \"$fn\" and \"$__target\""
continue
}
fi
decho mv -f "$fn" "$__target"
mv -f "$fn" "$__target"
fi
if [[ $ext == "zip" ]]
then
pushd .
decho cd "$dn"
cd "$dn" || exit 1
decho mkdir -p "$noext"
mkdir -p "$noext"
decho cd "$noext"
cd "$noext" || exit 1
decho unzip -n "../$bn"
unzip -n "../$bn"
popd
fi
shopt -s nocasematch
if [[ $ext == "thm" ]]
then
decho mv -f "$fn" "$fn.jpg"
mv -f "$fn" "$fn.jpg"
fi
shopt -u nocasematch
done
# check for short arg within long arg