-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathorganize_chd.zsh
executable file
·177 lines (150 loc) · 5.22 KB
/
organize_chd.zsh
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
#!/usr/bin/env zsh
# organize_chd
# Copyright (C) 2022 Kreeblah
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
function do_compress_chd {
if ! type "chdman"; then
echo "chdman must be in your path to create CHD files"
exit 1
fi
unset num_cpus
num_cpus=$([ $(uname) = 'Linux' ] && nproc || sysctl -n hw.ncpu)
if [[ -z "${num_cpus}" ]]; then
echo "Unable to determine number of CPUs"
exit 1
fi
for i in "${disc_directory[2]}"/**/*.cue;
do
tmpcue="${i:r}"
if [[ ! -f "${tmpcue}.chd" ]]; then
echo "Processing: ${tmpcue}.cue"
chdman createcd -np ${num_cpus} -i "${tmpcue}.cue" -o "${tmpcue}.chd"
if [[ $? -ne 0 ]]; then
if [[ -f "${tmpcue}.chd" ]]; then
rm -f "${tmpcue}.chd"
fi
echo "Error processing: ${tmpcue}.cue"
exit 1
elif [[ ! -f "${tmpcue}.chd" ]]; then
echo "Error creating: ${tmpcue}.chd"
exit 1
fi
else
echo "Found: ${tmpcue}.chd"
echo "Skipping: ${tmpcue}.cue"
fi
done;
}
function do_delete_source_image {
echo "Removing .cue files"
find "${disc_directory[2]}" -name "*.cue" -type f -delete
echo "Removing .bin files"
find "${disc_directory[2]}" -name "*.bin" -type f -delete
echo "Removing empty directories"
find "${disc_directory[2]}" -empty -type d -delete
}
function do_organize_single_region {
find_command_args=("${disc_directory[2]}" -type d \()
existing_find_str=0
console_region_dir="${disc_directory[2]}/${@[$#]}"
for i in "${@:1:#-1}";
do
if [[ ${existing_find_str} -ne 0 ]]; then
find_command_args+=(-or)
else
existing_find_str=1
fi
find_command_args+=(-name "*(${i}*")
done;
find_command_args+=(\))
if [[ ! -d "${console_region_dir}" ]]; then
echo "Creating: ${console_region_dir}"
mkdir -p "${console_region_dir}"
fi
find "${find_command_args[@]}" | while read found_regioned_dir;
do
if [[ "${found_regioned_dir}" != "${console_region_dir}/${found_regioned_dir:t}" ]]; then
echo "Moving ${found_regioned_dir} to ${console_region_dir}"
mv "${found_regioned_dir}" "${console_region_dir}"
else
echo "CHD directory already organized by region: ${found_regioned_dir}"
fi
done;
}
function do_organize_regions {
do_organize_single_region "Canada" "Latin America" "USA" "World" "NTSC-U"
do_organize_single_region "Japan" "NTSC-J"
do_organize_single_region "Asia" "Korea" "NTSC-A"
do_organize_single_region "Australia" "Austria" "Belgium" "Denmark" "Europe" "Finland" "France" "Germany" "Italy" "Netherlands" "Norway" "Poland" "Portugal" "Russia" "Spain" "Sweden" "Switzerland" "Turkey" "UK" "PAL"
do_organize_single_region "Brazil" "PAL-M"
}
function do_merge_disc_numbers {
find -E "${disc_directory[2]}" -regex '.* \(Disc [0-9]+\).*\.chd' | while read found_chd;
do
folder_to_move_to=$(echo "${found_chd:h}" | sed -E -e 's/(\/[^\/]*)( \(Disc [0-9]+\))([^\/]*[\/]?$)/\1\3/')
if [[ "${found_chd}" != "${folder_to_move_to}/${found_chd:t}" ]]; then
echo "Found CHD for disc merging: ${found_chd}"
echo "Directory to move to for disc merging: ${folder_to_move_to}"
if [[ ! -d "${folder_to_move_to}" ]]; then
mkdir -p "${folder_to_move_to}"
echo "Created directory: ${folder_to_move_to}"
fi
mv "${found_chd}" "${folder_to_move_to}/."
echo "Moved CHD ${found_chd} to ${folder_to_move_to}"
folder_to_remove=$(echo "${found_chd}" | sed -E -e 's/(.*)\/.*\.chd/\1/')
find "${folder_to_remove}" -empty -type d | while read empty_dir;
do
echo "Removing empty directory: ${empty_dir}"
rmdir "${empty_dir}"
done;
else
echo "CHD already merged for disc numbers: ${found_chd}"
fi
done;
}
zmodload zsh/zutil
autoload is-at-least
if ! is-at-least 5.8 ${ZSH_VERSION}; then
echo "zsh 5.8 is required for option parsing in this script, but zsh version ${ZSH_VERSION} was used"
exit 1
fi
unset disc_directory
unset compress_chd
unset organize_regions
unset merge_disc_numbers
unset delete_source_image
zparseopts -D -E -F - d:=disc_directory -disc-directory:=disc_directory c=compress_chd -compress-chd=compress_chd r=organize_regions -organize-regions=organize_regions n=merge_disc_numbers -merge-disc-numbers=merge_disc_numbers s=delete_source_image -delete-source-image=delete_source_image || exit 1
end_opts=$@[(i)(--|-)]
set -- "${@[0,end_opts-1]}" "${@[end_opts+1,-1]}"
if [[ -z ${disc_directory} ]]; then
echo "Must specify disc directory"
exit 1
fi
if [[ ! -d "${disc_directory[2]}" ]]; then
echo "Cannot find directory: ${disc_directory[2]}"
exit 1
fi
if [[ ! -z ${compress_chd} ]]; then
do_compress_chd
fi
if [[ ! -z ${organize_regions} ]]; then
do_organize_regions
fi
if [[ ! -z ${merge_disc_numbers} ]]; then
do_merge_disc_numbers
fi
if [[ ! -z ${delete_source_image} ]]; then
do_delete_source_image
fi