-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathideal.sh
executable file
·312 lines (259 loc) · 6.24 KB
/
ideal.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
#!/bin/bash
# NAME: ideal.sh
# AUTHOR: SKRUPLER
# LiCENSE: GPLV2
# DATE: 2017-01-26
# pretty colors
FC='\033[31;1m'
PC='\033[32;1m'
BC='\033[34;1m'
CEND='\033[0m'
source lib/helper.sh
menu(){
target=""
__move=""
__write=false
verbose=false
while getopts ":t:r:m:wivh" opt; do
case $opt in
t)
target="$OPTARG"
;;
r)
rlstype="$OPTARG"
;;
m)
__move="$OPTARG"
;;
w)
if $OPT;then
__write=true
printf '%s\n' "Write mode: $__write"
else
__write=false
fi
;;
i)
if ! [[ $OPT ]];then
__interactive=true
elif [[ $OPT ]];then
__interactive=false
fi
;;
h)
helpmsg
exit 1
;;
v)
verbose=true
printf "Verbose mode: $verbose\n"
;;
\?)
printf "Invalid option. -$OPTARG\n" >&2
exit 1
;;
:)
printf "Option -$OPTARG requires an argument.\n" >&2
exit 1
;;
*)
helpmsg
exit 1
;;
esac
done
}
helpmsg(){
# just a simple help screen
# not valid anymore anyways
echo -e "\tideal.sh\n"
echo -e "\tUsage:"
echo -e "\t$0 -t /path/to/target -v\n"
echo -e "\t-m" "\t" "Directory to move broken releases into."
echo -e "\t-w" "\t" "Writable mode, default doesnt touch anything."
echo -e "\t-i" "\t" "When flag is passed interactive mode will be disabled."
echo -e "\t-v" "\t" "Toggles verbose output aka also printing successful."
echo -e "\t-r" "\t" "Specify type of release. ie: mp3 or movie."
echo -e "\t-h" "\t" "Prints this message.\n"
}
make_list_of_failed(){
# writes all failed to failed.log
SCRIPT=$(realpath -s $0)
SCRIPTPATH=$(dirname $SCRIPT)
LOGPATH="$SCRIPTPATH/failed.log"
#echo "Making a failed list."
if ! [ -z $1 ];then
if ! [ -d $SCRIPTPATH ] && [ -e $LOGPATH ];then
touch $SCRIPTPATH/failed.log
elif [ -d $SCRIPTPATH ] && [ -s $LOGPATH ];then
rm $LOGPATH
touch $LOGPATH
fi
printf '%s\n' "--------------------------------[ CORRUPT ]--------------------------------"
for BROKEN in ${failed[@]};do
if [ -d $BROKEN ];then
echo -en "$BROKEN" "\n" >> $LOGPATH
echo -ne "BROKEN: $BROKEN\n"
else
echo "Not a directory. Skipped."
fi
done
printf '%s\n' "-------------------------------[ INCOMPLETE ]------------------------------"
for INCOMPLETE in ${incomplete[@]};do
if [ -d $INCOMPLETE ];then
echo -en "$INCOMPLETE" "\n" >> $LOGPATH
echo -ne "INCOMPLETE: $INCOMPLETE\n"
else
echo "Not a directory. Skipped."
fi
done
fi
}
draw_bar() {
# $1 work_done
# $2 work_total
# $3 window_columns
x=$(($3-2))
if [ $1 -eq $2 ];then
total_rls=$((${4}+${2}))
printf "\r[%*s" "${x}" | tr ' ' '#' #'▇'
printf "%*s]"
printf "${SCOLOR}${total_rls} releases in directory $target.${CEND}\n"
printf "${SCOLOR}${4} where incomplete and excluded from scan.${CEND}\n"
printf "${SCOLOR}Operation completed ${1} scans.${CEND}\n"
printf "${FCOLOR}${fint} broken releases.${CEND}\n"
printf "${SCOLOR}${sint} intact releases.${CEND}\n"
else
i=$((${1}*${x}/${2})) # work_done * window_columns / work_total
j=$((${x}-i)) # window_column - (work_done*window_columns / work_total)
printf "\r[%*s" "${i}" | tr ' ' '#' # '▇'
printf "%*s]\r" "${j}"
fi
}
listfiles() {
if find $1 -maxdepth 1 -type f -name "*.sfv";then
continue
else
printf "No SFV found."
fi
}
create_temp_dir(){
# takes $1 param as output directory
# creates two sub dirs within that.
printf '%s\n' "$1"
if [[ ! -d "$1" ]];then
mkdir -p "$1"
fi
if [[ ! -z "$1" ]] && [[ -d $1 ]];then
output="$1"
else
printf '%s\n' "Something wrong with output directory. Quitting."
exit 1
fi
if [[ ! -z $2 ]];then
new="$output/$2"
else
printf '%s\n' "Seems to be missing an argument. Quitting."
exit 1
fi
if [[ ! -d "$new" ]];then
printf '%s\n' "$new does not exist, creating it."
mkdir -p "$new"
fi
}
move_broken(){
OLDIFS=$IFS
IFS=$(echo -en "\n\b")
#mv_opt="-r"
#if [[ "$3" == true ]];then
# mv_opt="-ri"
#elif [[ "$3" == false ]];then
# mv_opt="-r"
#fi
# for all the corrupt releases
if [[ ${#failed[@]} == 0 ]];then
printf '%s\n' "broken rls: ${#failed[@]}"
else
for crpt in "${failed[@]}"; do
if [[ -d "${crpt}" ]];then
printf '%s\n' "Moved $crpt to $__move/broken/"
mv "$crpt" "$__move/broken/"
else
printf '%s\n' "Failed to remove: $crpt"
printf '%s\n' "Did the folder move?"
fi
done
fi
# for all the incomplete releases
if [[ ${#incomplete[@]} == 0 ]];then
printf '%s\n' "incomplete rls: ${#incomplete[@]}"
else
for inco in "${incomplete[@]}"; do
if [[ -d "${inco}" ]];then
printf '%s%s\n' "Moved $inco to $__move/incomplete"
mv "$inco" "$__move/incomplete/"
else
printf '%s\n' "Failed to remove: $inco"
printf '%s\n' "Did the folder move?"
fi
done
fi
IFS=${OLDIFS}
}
runnable(){
OLDIFS=$IFS
IFS=$(echo -en "\n\b")
if [[ -d $1 ]];then
# gets all the magic going, scans directories, creates list of sfv
# then runs it thru cksfv.
sint=0 # success increment
fint=0 # failed increment
make_lists $1 $2
work_done=0
broken=false
el=$(tput el) # this fixes the fucking return carriage
for katalog in "${pass_forward[@]}";do
for fil in $(listfiles $katalog);do
if [[ $fil == *.sfv ]];then # dubbelkontroll SO WHAT?
broken=false
if cksfv -g $fil -q &> /dev/null;then
# array of successful items
success[sint]=$katalog
sint=$((sint+1))
work_done=$((work_done+1))
if [[ $verbose == true ]];then
printf '%s %s%s\n' "[ SUCCESS ]" "$katalog" "$el"
fi
else
# array of failed items
failed[fint]=$katalog
fint=$((fint+1))
work_done=$((work_done+1))
broken=true
printf '%s %s%s\n' "[ FAILED ]" "$katalog" "$el"
fi
fi
done
cols=$(tput cols)
draw_bar ${work_done} ${#pass_forward[@]} ${cols} ${#failed[@]}
done
make_list_of_failed ${pass_forward[@]} ${failed[@]}
else
helpmsg
fi
IFS=${OLDIFS}
}
menu "$@"
runnable "$target" "$rlstype"
if [[ ! -z "$__move" ]] && [[ "$__write" == true ]];then
printf '%s\n' "BEFORE $__move"
if [[ ! ${#broken[@]} == 0 ]];then
create_temp_dir "$__move" "broken"
fi
if [[ ! ${#incomplete[@]} == 0 ]];then
create_temp_dir "$__move" "incomplete"
fi
move_broken "${failed[@]}" "${incomplete[@]}" "$__move"
else
printf '%s\n' "Requirements not met."
fi