Blame | Last modification | View Log | RSS feed
#!/bin/bash
################## CONFIG OPTIONS #############################################
DLDIR=`pwd` # "global" download dir ###
GLOGDIR=${DLDIR}/logs # "global" logdir ###
###############################################################################
### Clean up temp files ###
cleanup_temp() {
rm -f parfiles;
rm -f goodfiles;
rm -f completefiles;
rm -f currentset;
};
### Clean up global logs ###
cleanup_logs() {
rm -f $GLOGDIR/badfiles;
rm -f $GLOGDIR/unextractable;
};
### Function which prints error logs ###
print_errors() {
### Print Unrepairable ###
echo
if [ -f $GLOGDIR/badfiles ]
then
echo '****************************************'
echo '*** THE FOLLOWING WERE UNREPAIRABLE ****'
echo '****************************************'
echo
cat $GLOGDIR/badfiles
else
echo 'No Unrepairable Files!'
fi
echo
### Print Unextractable ###
if [ -f $GLOGDIR/unextractable ]
then
echo '****************************************'
echo '*** THE FOLLOWING WERE UNEXTRACTABLE ***'
echo '****************************************'
echo
cat $GLOGDIR/unextractable
else
echo 'No Unextractable Files!'
fi
};
### Store the previous value of IFS (word parser) ###
PREV_IFS=$IFS
NEW_IFS='
'
### Set IFS to the newline ###
### This is ugly as fuck, but it works ###
IFS=$NEW_IFS
### Set/Clean up the Environment ###
CUR_PWD=`pwd`
cd $DLDIR
cleanup_temp
cleanup_logs
### Get a list of subdirs one level down ###
ls -Fb $1 | grep \/ | sed -e 's/\/$//g' > $DLDIR/subdirs
echo "." >> $DLDIR/subdirs
###############################################################################
### The main loop that runs through the parchecking and extraction ###
###############################################################################
for SUBDIR in `cat $DLDIR/subdirs`;
do cd "${DLDIR}/${SUBDIR}/";
### Get all of the bare par2 files (not repair volumes) ###
ls *.{par2,PAR2} | grep -v vol > parfiles;
### Repair everything, and sort it into "goodfiles" and "badfiles" ###
for i in `cat parfiles`; \
do ( par2repair $i && echo $i >> goodfiles ) || echo $i >> $GLOGDIR/badfiles; \
done;
### Get the filenames of the files that verified successfully ###
sed 's/.par2//' goodfiles | sed 's/.PAR2//' > completefiles;
### Extract files, and delete the set if it extracted correctly ###
### the sed goop in the next line makes sure all spaces, [, and ] are backslashed ###
for i in `sed 's/ /\\ /g' completefiles | sed 's/\[/\\\[/g' | sed 's/\]/\\\]/g'`; \
# get a good working set, sans sfv and nfo
do ls ${i}.* | grep -vi sfv | grep -vi nfo | grep -vi jpg | grep -vi mp3 | grep -vi ape | grep -vi flac | grep -vi m3u | grep -vi pls > currentset; \
# remove pars from currentset, then get the first thing that has "rar" or "001" at the END only
# store above result in EXFILE (file to extract)
EXFILE=`grep -vi par2 currentset | grep -m 1 "rar$\|001$"`; \
# (extract the file AND remove the current set) OR save the filename to "unextractable"
( rar e ${EXFILE} && for j in `cat currentset`; do rm $j; done; ) || echo $i >> $GLOGDIR/unextractable; \
done;
cleanup_temp;
done; # end the "subdirs" loop
###############################################################################
### Reset IFS to it's default ###
IFS=$PREV_IFS
### Clean up temp files ###
cleanup_temp
rm -f $DLDIR/subdirs
### Go back to old pwd ###
cd $CUR_PWD
### Print Parting Message ###
echo
echo "All Finished. Good Bye!"