-
-
Notifications
You must be signed in to change notification settings - Fork 31
/
has_files.sh
executable file
·200 lines (189 loc) · 8.12 KB
/
has_files.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
#! /bin/bash
set -e
set -o pipefail
rom_path=$1
rom_type=$2
full_scan=$3
# Set user folder
if [ -d '/data' ]; then
user_folder='/data'
else
user_folder='frontend/user'
fi
check_files=$(find "${user_folder}${rom_path}" -maxdepth 1 -not -type d -and -not -name '.*' -exec basename {} \; | sort)
# Clear out old hashes
if [ "${full_scan}" = "true" ]; then
if [ -d "${user_folder}/hashes/${rom_path}" ]; then
rm -Rf "${user_folder}/hashes/${rom_path}"
fi
fi
# Clean up for bad scans
rm -Rf "${user_folder}/hashes/${rom_path}/tmp/"
# Process zip file hashes
process_zip () {
mkdir -p "${user_folder}/hashes/${rom_path}/tmp"
echo "unzipping ${file}"
unzip -j -q "${user_folder}${rom_path}/${file}" -d "${user_folder}/hashes/${rom_path}/tmp"
rm "${user_folder}/hashes/${rom_path}/tmp/"*.{txt,nfo,xml,readme,README} &> /dev/null || :
echo "hashing ${file}"
firstfile=( "${user_folder}/hashes/${rom_path}/tmp/"* )
sum=$(sha1sum "$firstfile" | awk '{print $1;exit}')
rm -R "${user_folder}/hashes/${rom_path}/tmp/"
printf ${sum^^} > "${user_folder}/hashes/${rom_path}/${file}.sha1"
}
# Process 7zip file hashes
process_7z () {
mkdir -p "${user_folder}/hashes/${rom_path}/tmp"
echo "unzipping ${file}"
7z e "${user_folder}${rom_path}/${file}" -o"${user_folder}/hashes/${rom_path}/tmp"
rm "${user_folder}/hashes/${rom_path}/tmp/"*.{txt,nfo,xml,readme,README} &> /dev/null || :
find "${user_folder}/hashes/${rom_path}/tmp/" -empty -type d -delete
echo "hashing ${file}"
firstfile=( "${user_folder}/hashes/${rom_path}/tmp/"* )
sum=$(sha1sum "$firstfile" | awk '{print $1;exit}')
rm -R "${user_folder}/hashes/${rom_path}/tmp/"
printf ${sum^^} > "${user_folder}/hashes/${rom_path}/${file}.sha1"
}
# Just hash the file
just_hash () {
mkdir -p "${user_folder}/hashes/${rom_path}"
echo "hashing ${file}"
sum=$(sha1sum "${user_folder}/${rom_path}/${file}" | awk '{print $1;exit}')
printf ${sum^^} > "${user_folder}/hashes/${rom_path}/${file}.sha1"
}
# For NES roms strip headers to get raw sha1
process_nes () {
if [ $file_type == 'zip' ]; then
mkdir -p "${user_folder}/hashes/${rom_path}/tmp"
echo "unzipping ${file}"
unzip -j -q "${user_folder}${rom_path}/${file}" -d "${user_folder}/hashes/${rom_path}/tmp"
rm "${user_folder}/hashes/${rom_path}/tmp/"*.{txt,nfo,xml,readme,README} &> /dev/null || :
file_to_sha="${user_folder}/hashes/${rom_path}/tmp/"*
elif [ $file_type == 'x-7z-compressed' ]; then
echo "unzipping ${file}"
mkdir -p "${user_folder}/hashes/${rom_path}/tmp"
echo "unzipping ${file}"
7z x "${user_folder}${rom_path}/${file}" -o"${user_folder}/hashes/${rom_path}/tmp"
rm "${user_folder}/hashes/${rom_path}/tmp/"*.{txt,nfo,xml,readme,README} &> /dev/null || :
file_to_sha="${user_folder}/hashes/${rom_path}/tmp/"*
else
file_to_sha="${user_folder}/${rom_path}/${file}"
fi
echo "hashing ${file}"
sum=$(NES20Tool -operation rominfo -rom-file ${file_to_sha} |awk '/ROM SHA1/ {print $3;exit}' || sha1sum ${file_to_sha} | awk '{print $1;exit}')
wait
printf ${sum^^} > "${user_folder}/hashes/${rom_path}/${file}.sha1"
if [ -d "${user_folder}/hashes/${rom_path}/tmp" ]; then
rm -R "${user_folder}/hashes/${rom_path}/tmp"
fi
}
# Use the file name for a key for arcade games and pceCD
process_name () {
printf "${file%.*}" > "${user_folder}/hashes/${rom_path}/${file}.sha1"
}
process_chd () {
echo "processing ${file}"
mkdir -p "${user_folder}/hashes/${rom_path}/tmp"
chdman extractcd -i "${user_folder}${rom_path}/${file}" -o "${user_folder}/hashes/${rom_path}/tmp/FILE.cue"
# Check if file has a track 2
if grep -q "TRACK 02" "${user_folder}/hashes/${rom_path}/tmp/FILE.cue"; then
echo "${file} is multi track need to split"
mkdir -p "${user_folder}/hashes/${rom_path}/tmp/split"
NOSPLIT=false
binmerge -s "${user_folder}/hashes/${rom_path}/tmp/FILE.cue" FILE -o "${user_folder}/hashes/${rom_path}/tmp/split" || NOSPLIT=true
if [ "${NOSPLIT}" == "false" ]; then
echo "hashing ${file} (Track 1)"
if [ -f "${user_folder}/hashes/${rom_path}/tmp/split/FILE (Track 1).bin" ]; then
sum=$(sha1sum "${user_folder}/hashes/${rom_path}/tmp/split/FILE (Track 1).bin" | awk '{print $1;exit}')
elif [ -f "${user_folder}/hashes/${rom_path}/tmp/split/FILE (Track 01).bin" ];then
sum=$(sha1sum "${user_folder}/hashes/${rom_path}/tmp/split/FILE (Track 01).bin" | awk '{print $1;exit}')
fi
else
echo "splitting failed hashing full bin ${file}"
sum=$(sha1sum "${user_folder}/hashes/${rom_path}/tmp/FILE.cue" | awk '{print $1;exit}')
fi
elif grep -q "TRACK 01" "${user_folder}/hashes/${rom_path}/tmp/FILE.cue"; then
echo "hashing ${file}"
sum=$(sha1sum "${user_folder}/hashes/${rom_path}/tmp/FILE.bin" | awk '{print $1;exit}')
fi
printf ${sum^^} > "${user_folder}/hashes/${rom_path}/${file}.sha1"
if [ -d "${user_folder}/hashes/${rom_path}/tmp" ]; then
rm -R "${user_folder}/hashes/${rom_path}/tmp"
fi
}
process_bin () {
echo "processing ${file}"
# Make sure we have a cue file
cuefile=$(echo "${file}" | sed 's/.bin$/.cue/')
if [ ! -f "${user_folder}/${rom_path}/${cuefile}" ]; then
echo "No cue found"
return 0
fi
# Check if file has a track 2
if grep -q "TRACK 02" "${user_folder}/${rom_path}/${cuefile}"; then
echo "${file} is multi track need to split"
mkdir -p "${user_folder}/${rom_path}/tmp/"
NOSPLIT=false
binmerge -s "${user_folder}/${rom_path}/${cuefile}" FILE -o "${user_folder}/${rom_path}/tmp/" || NOSPLIT=true
if [ "${NOSPLIT}" == "false" ]; then
echo "hashing ${file} (Track 1)"
if [ -f "${user_folder}/${rom_path}/tmp/FILE (Track 1).bin" ]; then
sum=$(sha1sum "${user_folder}/${rom_path}/tmp/FILE (Track 1).bin" | awk '{print $1;exit}')
elif [ -f "${user_folder}/${rom_path}/tmp/FILE (Track 01).bin" ];then
sum=$(sha1sum "${user_folder}/${rom_path}/tmp/FILE (Track 01).bin" | awk '{print $1;exit}')
fi
else
echo "splitting failed hashing full bin ${file}"
sum=$(sha1sum "${user_folder}/${rom_path}/${file}" | awk '{print $1;exit}')
fi
elif grep -q "TRACK 01" "${user_folder}/${rom_path}/${cuefile}"; then
echo "hashing ${file}"
sum=$(sha1sum "${user_folder}/${rom_path}/${file}" | awk '{print $1;exit}')
fi
printf ${sum^^} > "${user_folder}/hashes/${rom_path}/${file}.sha1"
if [ -d "${user_folder}/${rom_path}/tmp" ]; then
rm -R "${user_folder}/${rom_path}/tmp"
fi
}
# Process special zip file hashes
process_zip_by_name () {
mkdir -p "${user_folder}/hashes/${rom_path}/tmp"
echo "unzipping ${file}"
unzip -j -q "${user_folder}${rom_path}/${file}" -d "${user_folder}/hashes/${rom_path}/tmp"
echo "hashing ${file}"
sum=$(sha1sum "${user_folder}/hashes/${rom_path}/tmp/${file%.*}."* | awk '{print $1;exit}')
rm -R "${user_folder}/hashes/${rom_path}/tmp/"
printf ${sum^^} > "${user_folder}/hashes/${rom_path}/${file}.sha1"
}
IFS=$'\n'
mkdir -p "${user_folder}/hashes/${rom_path}"
for file in $check_files; do
if [ ! -f "${user_folder}/hashes/${rom_path}/${file}.sha1" ]; then
file_extension="${file##*.}"
if [ "${file_extension,,}" = 'multiwad' ]; then
process_zip_by_name
elif [ $rom_type == 'arcade' ] || [ $rom_type == 'segaSaturn' ] && [[ "${file_extension,,}" != @(img|cue|ccd|disk*|sub) ]]; then
process_name
elif [ "${file_extension,,}" = 'chd' ] && [ $rom_type == 'pce' ]; then
process_name
elif [ "${file_extension,,}" = 'chd' ] || ([ $rom_type == 'psx' ] && [[ "${file_extension,,}" == "disk1" ]]); then
process_chd
elif [ "${file_extension,,}" = 'bin' ] && [ $rom_type != '3do' ]; then
process_bin
elif [[ "${file_extension,,}" = @(img|cue|ccd|disk*|sub) ]]; then
echo "Filetype ${file_extension} not supported"
elif [ $rom_type == 'nes' ]; then
file_type=$(file -b --mime-type "${user_folder}${rom_path}/${file}" | awk -F'/' '{print $2}')
process_nes
else
file_type=$(file -b --mime-type "${user_folder}${rom_path}/${file}" | awk -F'/' '{print $2}')
if [ $file_type == 'zip' ]; then
process_zip
elif [ $file_type == 'x-7z-compressed' ]; then
process_7z
else
just_hash
fi
fi
fi
done