Subversion Repositories programming

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
91 irasnyd 1
#!/bin/bash
89 irasnyd 2
 
96 irasnyd 3
############################### CONFIG OPTIONS #################################
89 irasnyd 4
DLDIR="/home/irasnyd/downloads/usenet"              # "global" download dir  ###
5
GLOGDIR="/home/irasnyd/downloads/usenet/logs"       # "global" logdir        ###
91 irasnyd 6
UNEX_LOG="${GLOGDIR}/unextractable"                 # "global" unextractable ###
7
UNREP_LOG="${GLOGDIR}/unrepairable"                 # "global" unrepairable  ###
105 ira 8
NFODIR="/data/NFO"                                  # "global" nfo storage   ###
113 ira 9
SFVDIR="/data/NFO/sfv"                              # "global" sfv storage   ###
89 irasnyd 10
################################################################################
11
 
96 irasnyd 12
################################################################################
13
############################### FUNCTIONS ######################################
91 irasnyd 14
 
89 irasnyd 15
### Clean up temp files ###
16
cleanup_temp() {
91 irasnyd 17
    rm -f parfiles;
18
    rm -f goodfiles;
19
    rm -f completefiles;
20
    rm -f currentset;
21
    rm -f unrepairable;
93 irasnyd 22
    rm -f parjoinfiles;
96 irasnyd 23
}
89 irasnyd 24
 
91 irasnyd 25
### Clean up global logs ###
26
cleanup_logs() {
27
    rm -f "${UNREP_LOG}";
28
    rm -f "${UNEX_LOG}";
96 irasnyd 29
}
30
 
91 irasnyd 31
### Function which prints error logs ###
32
print_errors() {
96 irasnyd 33
   # Print Unrepairable
91 irasnyd 34
   echo
35
   if [ -f "${UNREP_LOG}" ]
36
   then
37
      echo '****************************************'
38
      echo '*** THE FOLLOWING WERE UNREPAIRABLE ****'
39
      echo '****************************************'
40
      echo
41
      cat "${UNREP_LOG}"
42
   else
43
      echo 'No Unrepairable Files!'
44
   fi
89 irasnyd 45
 
91 irasnyd 46
   echo
47
 
96 irasnyd 48
   # Print Unextractable
91 irasnyd 49
   if [ -f "${UNEX_LOG}" ]
50
   then
51
      echo '****************************************'
52
      echo '*** THE FOLLOWING WERE UNEXTRACTABLE ***'
53
      echo '****************************************'
54
      echo
55
      cat "${UNEX_LOG}"
56
   else
57
      echo 'No Unextractable Files!'
58
   fi
96 irasnyd 59
}
91 irasnyd 60
 
61
### delete the currentset ###
62
del_currentset() {
63
    while read CSNAME;
64
        do rm "${CSNAME}"
65
    done < currentset
66
}
67
 
68
### does everything that needs to be done in the dir passed to it ###
69
rarslave_dir() {
70
    OPERATING_DIR="$1"
71
    ORIG_DIR="$(pwd)"
72
 
96 irasnyd 73
    # move into the working dir
91 irasnyd 74
    cd "${OPERATING_DIR}"
111 ira 75
 
96 irasnyd 76
    # print starting message
91 irasnyd 77
    print_working_message
78
 
96 irasnyd 79
    # Set/Clean up the Environment
91 irasnyd 80
    cleanup_temp
81
 
96 irasnyd 82
    # Repair everything, and sort it into "goodfiles" and "badfiles"
129 ira 83
    # NOTE: Only use this line with =findutils-4.2.23
84
    # find ./ -maxdepth 1 -iregex '.*\.par2$' \! -iregex  '.*\.vol[0-9]\++[0-9]\+\.par2$' -print0 \
85
    #    | xargs -0 -n1 -i rarslave "{}" "parsort"
111 ira 86
 
118 ira 87
    # When using <=findutils-4.2.20 (or thereabouts), use the following. The "+" operator
88
    # has changed in later versions.
129 ira 89
    # NOTE: Behaivior is back to this in >=findutils-4.2.24
90
    find ./ -maxdepth 1 -iregex '.*\.par2$' \! -iregex  '.*\.vol[0-9]+\+[0-9]+\.par2$' -print0 \
91
        | xargs -0 -n1 -i rarslave "{}" "parsort"
118 ira 92
 
96 irasnyd 93
    # Extract files, and delete the set if it extracted correctly
91 irasnyd 94
    sed 's/.par2$//' goodfiles | sed 's/.PAR2$//' > completefiles
89 irasnyd 95
 
96 irasnyd 96
    # for each FILENAME in completefiles, extract them
111 ira 97
    while read FILENAME;
89 irasnyd 98
 
91 irasnyd 99
        # we are out of filenames
100
        if [ -z "${FILENAME}" ]; then break; fi;
89 irasnyd 101
 
91 irasnyd 102
        # get the set of files associated with this par2
104 irasnyd 103
        do ls "${FILENAME}"* | grep -i "rar$\|r..$\|par2$" > currentset;
111 ira 104
 
105
        # remove pars from currentset, then get the first thing that
91 irasnyd 106
        # has "rar" or "r00" at the END only
107
        # store above result in EXFILE (file to extract)
108
        EXFILE="$(grep -vi "par2$" currentset | grep -m1 "rar$\|r00$")";
111 ira 109
 
91 irasnyd 110
        # if there are files to extract
111
        if [ ! -z "${EXFILE}" ]; then
112
            # (extract the file AND remove the current set) OR save the filename to "unextractable"
111 ira 113
            ( rar e -o+ "${EXFILE}" && del_currentset ) || echo "${FILENAME}" >> "${UNEX_LOG}";
91 irasnyd 114
        else
115
            echo "No File to extract, removing currentset";
116
            del_currentset;
117
        fi;
118
    done < completefiles
89 irasnyd 119
 
96 irasnyd 120
    # part for parjoin
93 irasnyd 121
    sed 's/.par2$//' unrepairable | sed 's/.PAR2$//' > parjoinfiles
104 irasnyd 122
 
93 irasnyd 123
    while read FILENAME;
124
 
111 ira 125
        # no more filenames
93 irasnyd 126
        if [ -z "${FILENAME}" ]; then break; fi
127
 
128
        # no appropriate file attached to this par2
129
        if [ ! -f "${FILENAME}".001 ]; then continue; fi;
130
 
95 irasnyd 131
        do rarslave "${FILENAME}" "parjoin";
93 irasnyd 132
 
133
    done < parjoinfiles
111 ira 134
 
96 irasnyd 135
    # Clean up temp files used
91 irasnyd 136
    cleanup_temp
137
 
96 irasnyd 138
    # print finishing message
91 irasnyd 139
    print_finished_message
111 ira 140
 
96 irasnyd 141
    # go back to the starting dir
91 irasnyd 142
    cd "${ORIG_DIR}"
143
}
144
 
96 irasnyd 145
### the "parjoin" command ###
95 irasnyd 146
rarslave_parjoin() {
147
    PJNAME="$1"
148
 
113 ira 149
    lxsplit -j "${PJNAME}".001                  ### Attempt to join the file
150
    par2repair "${PJNAME}".*???2                ### Attempt to repair the joined file
151
 
152
    RETURN_CODE=$?                              ### Get the return code of the repair
153
 
154
    if [ ${RETURN_CODE} -eq 0 ]; then           ### If the repair was successful
155
            rm "${PJNAME}".???*                 ### then remove all the source files
156
    else                                        ### If the repair was not successful
157
            rm "${PJNAME}"                      ### then remove the attempted file
158
    fi
159
 
95 irasnyd 160
}
161
 
113 ira 162
### the "parsort" command. attempts to repair and sort into ###
96 irasnyd 163
### "goodfiles" and "unrepairable" ###
95 irasnyd 164
rarslave_parsort() {
165
    PARNAME="$1"
166
 
167
    par2repair "${PARNAME}" \
96 irasnyd 168
    && echo "${PARNAME}" >> goodfiles \
95 irasnyd 169
    || echo "${PARNAME}" >> unrepairable
170
}
171
 
105 ira 172
### the "nfosort" command. moves all NFO's to ${NFODIR} ###
173
nfosort() {
174
    find "${DLDIR}" -iregex '.*\.nfo$' -exec mv '{}' "${NFODIR}" \;
175
}
176
 
113 ira 177
### the "remove extraneous" command. removes all *.1 files (repair leftovers)
178
### as well as moving all sfv's to ${SFVDIR}
179
remove_extraneous() {
180
    find "${DLDIR}" -iregex '^.*\.1$' -exec rm '{}' \;
181
    find "${DLDIR}" -iregex '^.*\.sfv$' -exec mv '{}' "${SFVDIR}" \;
182
}
183
 
96 irasnyd 184
### prints the beginning message ###
91 irasnyd 185
print_working_message() {
97 irasnyd 186
    echo '================================================================================'
91 irasnyd 187
    echo "Working in $(pwd)"
97 irasnyd 188
    echo '================================================================================'
91 irasnyd 189
}
190
 
96 irasnyd 191
### prints the ending message ###
91 irasnyd 192
print_finished_message() {
97 irasnyd 193
    echo '================================================================================'
91 irasnyd 194
    echo "Done in $(pwd)"
97 irasnyd 195
    echo '================================================================================'
91 irasnyd 196
}
97 irasnyd 197
 
198
### print a welcome message ###
199
print_welcome_message() {
200
    echo '================================================================================'
201
    echo '== This is rarslave, a free program designed to assist you in repairing and   =='
202
    echo '== extracting files that you have downloaded from USENET. It will work on any =='
203
    echo '== file set with a par2 and rar. It also works for sets with just par2        =='
204
    echo '== associated. In that case, it repairs then removes the par2 (if successful) =='
205
    echo '==                                                                            =='
206
    echo '== Written By ---- Ira Snyder                                                 =='
207
    echo '== Start Date ---- 06-08-2005 (ported from zsh)                               =='
208
    echo '== Last Revised -- 06-08-2005 (appears not to have major bugs now)            =='
209
    echo '== License ------- GNU General Public License v2                              =='
210
    echo '== License ------- http://www.gnu.org/licenses/gpl.txt                        =='
211
    echo '==                                                                            =='
212
    echo '== Thanks for using this program!                                             =='
213
    echo '================================================================================'
214
    echo
215
}
96 irasnyd 216
############################### END FUNCTIONS ##################################
217
################################################################################
89 irasnyd 218
 
91 irasnyd 219
GLOBAL_ORIG_DIR="$(pwd)"
89 irasnyd 220
 
96 irasnyd 221
# if we got no args, run in $DLDIR
93 irasnyd 222
if [ -z "$@" ]; then
97 irasnyd 223
 
224
    print_welcome_message
225
 
226
    # switch to the dir from the config and set up the environment
93 irasnyd 227
    cd "${DLDIR}"
228
    cleanup_logs
91 irasnyd 229
 
97 irasnyd 230
    # run rarslave recursively on every directory . and below
94 irasnyd 231
    find ./ -type d -print0 | xargs -0 -n1 | xargs -I{} rarslave "{}"
105 ira 232
    nfosort
113 ira 233
    remove_extraneous
94 irasnyd 234
    print_errors
235
 
96 irasnyd 236
    # Print Parting Message
94 irasnyd 237
    echo
238
    echo "All Finished. Good Bye!"
239
 
96 irasnyd 240
# we have some args...
93 irasnyd 241
else
96 irasnyd 242
    # if there is no 2nd arg, run in the directory given by $1
95 irasnyd 243
    if [ -z "$2" ]; then
244
        rarslave_dir "$@"
96 irasnyd 245
 
246
    # there is a second arg...
95 irasnyd 247
    else
96 irasnyd 248
 
249
        # run the built-in "parsort" command if we got it
95 irasnyd 250
        if [ "$2" = "parsort" ]; then
251
            rarslave_parsort "$1"
252
        fi
253
 
96 irasnyd 254
        # run the built-in "parjoin" command if we got it
95 irasnyd 255
        if [ "$2" = "parjoin" ]; then
256
            rarslave_parjoin "$1"
257
        fi
96 irasnyd 258
 
95 irasnyd 259
    fi
93 irasnyd 260
fi
91 irasnyd 261
 
262
cd "${GLOBAL_ORIG_DIR}"