Rev 111 | Blame | Last modification | View Log | RSS feed
#!/bin/bash############################### CONFIG OPTIONS #################################DLDIR="/home/irasnyd/downloads/usenet" # "global" download dir ###GLOGDIR="/home/irasnyd/downloads/usenet/logs" # "global" logdir ###UNEX_LOG="${GLOGDIR}/unextractable" # "global" unextractable ###UNREP_LOG="${GLOGDIR}/unrepairable" # "global" unrepairable ###NFODIR="/data/NFO" # "global" nfo storage ###SFVDIR="/data/NFO/sfv" # "global" sfv storage ################################################################################################################################################################################################## FUNCTIONS ######################################### Clean up temp files ###cleanup_temp() {rm -f parfiles;rm -f goodfiles;rm -f completefiles;rm -f currentset;rm -f unrepairable;rm -f parjoinfiles;}### Clean up global logs ###cleanup_logs() {rm -f "${UNREP_LOG}";rm -f "${UNEX_LOG}";}### Function which prints error logs ###print_errors() {# Print Unrepairableechoif [ -f "${UNREP_LOG}" ]thenecho '****************************************'echo '*** THE FOLLOWING WERE UNREPAIRABLE ****'echo '****************************************'echocat "${UNREP_LOG}"elseecho 'No Unrepairable Files!'fiecho# Print Unextractableif [ -f "${UNEX_LOG}" ]thenecho '****************************************'echo '*** THE FOLLOWING WERE UNEXTRACTABLE ***'echo '****************************************'echocat "${UNEX_LOG}"elseecho 'No Unextractable Files!'fi}### delete the currentset ###del_currentset() {while read CSNAME;do rm "${CSNAME}"done < currentset}### does everything that needs to be done in the dir passed to it ###rarslave_dir() {OPERATING_DIR="$1"ORIG_DIR="$(pwd)"# move into the working dircd "${OPERATING_DIR}"# print starting messageprint_working_message# Set/Clean up the Environmentcleanup_temp# Repair everything, and sort it into "goodfiles" and "badfiles"find ./ -maxdepth 1 -iregex '.*\.par2$' \! -iregex '.*\.vol[0-9]+\+[0-9]+\.par2$' -print0 \| xargs -0 -n1 \| xargs -I{} rarslave "{}" "parsort"# Extract files, and delete the set if it extracted correctlysed 's/.par2$//' goodfiles | sed 's/.PAR2$//' > completefiles# for each FILENAME in completefiles, extract themwhile read FILENAME;# we are out of filenamesif [ -z "${FILENAME}" ]; then break; fi;# get the set of files associated with this par2do ls "${FILENAME}"* | grep -i "rar$\|r..$\|par2$" > currentset;# remove pars from currentset, then get the first thing that# has "rar" or "r00" at the END only# store above result in EXFILE (file to extract)EXFILE="$(grep -vi "par2$" currentset | grep -m1 "rar$\|r00$")";# if there are files to extractif [ ! -z "${EXFILE}" ]; then# (extract the file AND remove the current set) OR save the filename to "unextractable"( rar e -o+ "${EXFILE}" && del_currentset ) || echo "${FILENAME}" >> "${UNEX_LOG}";elseecho "No File to extract, removing currentset";del_currentset;fi;done < completefiles# part for parjoinsed 's/.par2$//' unrepairable | sed 's/.PAR2$//' > parjoinfileswhile read FILENAME;# no more filenamesif [ -z "${FILENAME}" ]; then break; fi# no appropriate file attached to this par2if [ ! -f "${FILENAME}".001 ]; then continue; fi;do rarslave "${FILENAME}" "parjoin";done < parjoinfiles# Clean up temp files usedcleanup_temp# print finishing messageprint_finished_message# go back to the starting dircd "${ORIG_DIR}"}### the "parjoin" command ###rarslave_parjoin() {PJNAME="$1"lxsplit -j "${PJNAME}".001 ### Attempt to join the filepar2repair "${PJNAME}".*???2 ### Attempt to repair the joined fileRETURN_CODE=$? ### Get the return code of the repairif [ ${RETURN_CODE} -eq 0 ]; then ### If the repair was successfulrm "${PJNAME}".???* ### then remove all the source fileselse ### If the repair was not successfulrm "${PJNAME}" ### then remove the attempted filefi}### the "parsort" command. attempts to repair and sort into ###### "goodfiles" and "unrepairable" ###rarslave_parsort() {PARNAME="$1"par2repair "${PARNAME}" \&& echo "${PARNAME}" >> goodfiles \|| echo "${PARNAME}" >> unrepairable}### the "nfosort" command. moves all NFO's to ${NFODIR} ###nfosort() {find "${DLDIR}" -iregex '.*\.nfo$' -exec mv '{}' "${NFODIR}" \;}### the "remove extraneous" command. removes all *.1 files (repair leftovers)### as well as moving all sfv's to ${SFVDIR}remove_extraneous() {find "${DLDIR}" -iregex '^.*\.1$' -exec rm '{}' \;find "${DLDIR}" -iregex '^.*\.sfv$' -exec mv '{}' "${SFVDIR}" \;}### prints the beginning message ###print_working_message() {echo '================================================================================'echo "Working in $(pwd)"echo '================================================================================'}### prints the ending message ###print_finished_message() {echo '================================================================================'echo "Done in $(pwd)"echo '================================================================================'}### print a welcome message ###print_welcome_message() {echo '================================================================================'echo '== This is rarslave, a free program designed to assist you in repairing and =='echo '== extracting files that you have downloaded from USENET. It will work on any =='echo '== file set with a par2 and rar. It also works for sets with just par2 =='echo '== associated. In that case, it repairs then removes the par2 (if successful) =='echo '== =='echo '== Written By ---- Ira Snyder =='echo '== Start Date ---- 06-08-2005 (ported from zsh) =='echo '== Last Revised -- 06-08-2005 (appears not to have major bugs now) =='echo '== License ------- GNU General Public License v2 =='echo '== License ------- http://www.gnu.org/licenses/gpl.txt =='echo '== =='echo '== Thanks for using this program! =='echo '================================================================================'echo}############################### END FUNCTIONS ##################################################################################################################GLOBAL_ORIG_DIR="$(pwd)"# if we got no args, run in $DLDIRif [ -z "$@" ]; thenprint_welcome_message# switch to the dir from the config and set up the environmentcd "${DLDIR}"cleanup_logs# run rarslave recursively on every directory . and belowfind ./ -type d -print0 | xargs -0 -n1 | xargs -I{} rarslave "{}"nfosortremove_extraneousprint_errors# Print Parting Messageechoecho "All Finished. Good Bye!"# we have some args...else# if there is no 2nd arg, run in the directory given by $1if [ -z "$2" ]; thenrarslave_dir "$@"# there is a second arg...else# run the built-in "parsort" command if we got itif [ "$2" = "parsort" ]; thenrarslave_parsort "$1"fi# run the built-in "parjoin" command if we got itif [ "$2" = "parjoin" ]; thenrarslave_parjoin "$1"fifificd "${GLOBAL_ORIG_DIR}"