89 |
irasnyd |
1 |
#!/bin/bash
|
|
|
2 |
|
|
|
3 |
################## CONFIG OPTIONS #############################################
|
|
|
4 |
DLDIR="/home/irasnyd/downloads/usenet" # "global" download dir ###
|
|
|
5 |
GLOGDIR=${DLDIR}/logs # "global" logdir ###
|
|
|
6 |
###############################################################################
|
|
|
7 |
|
|
|
8 |
### Clean up temp files ###
|
|
|
9 |
cleanup_temp() {
|
|
|
10 |
rm -f parfiles;
|
|
|
11 |
rm -f goodfiles;
|
|
|
12 |
rm -f completefiles;
|
|
|
13 |
rm -f currentset;
|
|
|
14 |
};
|
|
|
15 |
|
|
|
16 |
### Clean up global logs ###
|
|
|
17 |
cleanup_logs() {
|
|
|
18 |
rm -f $GLOGDIR/badfiles;
|
|
|
19 |
rm -f $GLOGDIR/unextractable;
|
|
|
20 |
};
|
|
|
21 |
|
|
|
22 |
### Function which prints error logs ###
|
|
|
23 |
print_errors() {
|
|
|
24 |
### Print Unrepairable ###
|
|
|
25 |
echo
|
|
|
26 |
if [ -f $GLOGDIR/badfiles ]
|
|
|
27 |
then
|
|
|
28 |
echo '****************************************'
|
|
|
29 |
echo '*** THE FOLLOWING WERE UNREPAIRABLE ****'
|
|
|
30 |
echo '****************************************'
|
|
|
31 |
echo
|
|
|
32 |
cat $GLOGDIR/badfiles
|
|
|
33 |
else
|
|
|
34 |
echo 'No Unrepairable Files!'
|
|
|
35 |
fi
|
|
|
36 |
echo
|
|
|
37 |
|
|
|
38 |
### Print Unextractable ###
|
|
|
39 |
if [ -f $GLOGDIR/unextractable ]
|
|
|
40 |
then
|
|
|
41 |
echo '****************************************'
|
|
|
42 |
echo '*** THE FOLLOWING WERE UNEXTRACTABLE ***'
|
|
|
43 |
echo '****************************************'
|
|
|
44 |
echo
|
|
|
45 |
cat $GLOGDIR/unextractable
|
|
|
46 |
else
|
|
|
47 |
echo 'No Unextractable Files!'
|
|
|
48 |
fi
|
|
|
49 |
};
|
|
|
50 |
|
|
|
51 |
### Store the previous value of IFS (word parser) ###
|
|
|
52 |
PREV_IFS=$IFS
|
|
|
53 |
NEW_IFS='
|
|
|
54 |
'
|
|
|
55 |
### Set IFS to the newline ###
|
|
|
56 |
### This is ugly as fuck, but it works ###
|
|
|
57 |
IFS=$NEW_IFS
|
|
|
58 |
|
|
|
59 |
### Set/Clean up the Environment ###
|
|
|
60 |
CUR_PWD=`pwd`
|
|
|
61 |
cd $DLDIR
|
|
|
62 |
cleanup_temp
|
|
|
63 |
cleanup_logs
|
|
|
64 |
|
|
|
65 |
### Get a list of subdirs one level down ###
|
|
|
66 |
ls -Fb $1 | grep \/ | sed -e 's/\/$//g' > $GLOGDIR/subdirs
|
|
|
67 |
echo "." >> $GLOGDIR/subdirs
|
|
|
68 |
|
|
|
69 |
|
|
|
70 |
###############################################################################
|
|
|
71 |
### The main loop that runs through the parchecking and extraction ###
|
|
|
72 |
###############################################################################
|
|
|
73 |
for SUBDIR in `cat $GLOGDIR/subdirs`;
|
|
|
74 |
do cd "${DLDIR}/${SUBDIR}/";
|
|
|
75 |
|
|
|
76 |
### Get all of the bare par2 files (not repair volumes) ###
|
|
|
77 |
ls *.{par2,PAR2} | grep -v vol > parfiles;
|
|
|
78 |
|
|
|
79 |
### Repair everything, and sort it into "goodfiles" and "badfiles" ###
|
|
|
80 |
for i in `cat parfiles`; \
|
|
|
81 |
do ( par2repair $i && echo $i >> goodfiles ) || echo $i >> $GLOGDIR/badfiles; \
|
|
|
82 |
done;
|
|
|
83 |
|
|
|
84 |
### Get the filenames of the files that verified successfully ###
|
|
|
85 |
sed 's/.par2//' goodfiles | sed 's/.PAR2//' > completefiles;
|
|
|
86 |
|
|
|
87 |
### Extract files, and delete the set if it extracted correctly ###
|
|
|
88 |
### the sed goop in the next line makes sure all spaces, [, and ] are backslashed ###
|
|
|
89 |
for i in `sed 's/ /\\ /g' completefiles | sed 's/\[/\\\[/g' | sed 's/\]/\\\]/g'`; \
|
|
|
90 |
# get a good working set, sans sfv and nfo
|
|
|
91 |
do ls ${i}.* | grep -vi nfo | grep -vi jpg > currentset; \
|
|
|
92 |
|
|
|
93 |
# remove pars from currentset, then get the first thing that has "rar" or "001" at the END only
|
|
|
94 |
# store above result in EXFILE (file to extract)
|
|
|
95 |
EXFILE=`grep -vi par2 currentset | grep -m 1 "rar$\|001$"`; \
|
|
|
96 |
|
|
|
97 |
# (extract the file AND remove the current set) OR save the filename to "unextractable"
|
|
|
98 |
( rar e ${EXFILE} && for j in `cat currentset`; do rm $j; done; ) || echo $i >> $GLOGDIR/unextractable; \
|
|
|
99 |
done;
|
|
|
100 |
cleanup_temp;
|
|
|
101 |
done; # end the "subdirs" loop
|
|
|
102 |
###############################################################################
|
|
|
103 |
|
|
|
104 |
### Reset IFS to it's default ###
|
|
|
105 |
IFS=$PREV_IFS
|
|
|
106 |
|
|
|
107 |
### Clean up temp files ###
|
|
|
108 |
cleanup_temp
|
|
|
109 |
rm -f $GLOGDIR/subdirs
|
|
|
110 |
|
|
|
111 |
### Go back to old pwd ###
|
|
|
112 |
cd $CUR_PWD
|
|
|
113 |
|
|
|
114 |
### Print Parting Message ###
|
|
|
115 |
echo
|
|
|
116 |
echo "All Finished. Good Bye!"
|