Subversion Repositories programming

Rev

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 Unrepairable
   echo
   if [ -f "${UNREP_LOG}" ]
   then
      echo '****************************************'
      echo '*** THE FOLLOWING WERE UNREPAIRABLE ****'
      echo '****************************************'
      echo
      cat "${UNREP_LOG}"
   else
      echo 'No Unrepairable Files!'
   fi

   echo

   # Print Unextractable
   if [ -f "${UNEX_LOG}" ]
   then
      echo '****************************************'
      echo '*** THE FOLLOWING WERE UNEXTRACTABLE ***'
      echo '****************************************'
      echo
      cat "${UNEX_LOG}"
   else
      echo '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 dir
    cd "${OPERATING_DIR}"

    # print starting message
    print_working_message

    # Set/Clean up the Environment
    cleanup_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 correctly
    sed 's/.par2$//' goodfiles | sed 's/.PAR2$//' > completefiles

    # for each FILENAME in completefiles, extract them
    while read FILENAME;

        # we are out of filenames
        if [ -z "${FILENAME}" ]; then break; fi;

        # get the set of files associated with this par2
        do 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 extract
        if [ ! -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}";
        else
            echo "No File to extract, removing currentset";
            del_currentset;
        fi;
    done < completefiles

    # part for parjoin
    sed 's/.par2$//' unrepairable | sed 's/.PAR2$//' > parjoinfiles

    while read FILENAME;

        # no more filenames
        if [ -z "${FILENAME}" ]; then break; fi

        # no appropriate file attached to this par2
        if [ ! -f "${FILENAME}".001 ]; then continue; fi;

        do rarslave "${FILENAME}" "parjoin";

    done < parjoinfiles

    # Clean up temp files used
    cleanup_temp

    # print finishing message
    print_finished_message

    # go back to the starting dir
    cd "${ORIG_DIR}"
}

### the "parjoin" command ###
rarslave_parjoin() {
    PJNAME="$1"

    lxsplit -j "${PJNAME}".001                  ### Attempt to join the file
    par2repair "${PJNAME}".*???2                ### Attempt to repair the joined file

    RETURN_CODE=$?                              ### Get the return code of the repair

    if [ ${RETURN_CODE} -eq 0 ]; then           ### If the repair was successful
            rm "${PJNAME}".???*                 ### then remove all the source files
    else                                        ### If the repair was not successful
            rm "${PJNAME}"                      ### then remove the attempted file
    fi
                
}

### 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 $DLDIR
if [ -z "$@" ]; then

    print_welcome_message

    # switch to the dir from the config and set up the environment
    cd "${DLDIR}"
    cleanup_logs

    # run rarslave recursively on every directory . and below
    find ./ -type d -print0 | xargs -0 -n1 | xargs -I{} rarslave "{}"
    nfosort
    remove_extraneous
    print_errors

    # Print Parting Message
    echo
    echo "All Finished. Good Bye!"

# we have some args...
else
    # if there is no 2nd arg, run in the directory given by $1
    if [ -z "$2" ]; then
        rarslave_dir "$@"

    # there is a second arg...
    else

        # run the built-in "parsort" command if we got it
        if [ "$2" = "parsort" ]; then
            rarslave_parsort "$1"
        fi

        # run the built-in "parjoin" command if we got it
        if [ "$2" = "parjoin" ]; then
            rarslave_parjoin "$1"
        fi

    fi
fi

cd "${GLOBAL_ORIG_DIR}"