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"
118 ira 83
    find ./ -maxdepth 1 -iregex '.*\.par2$' \! -iregex  '.*\.vol[0-9]\++[0-9]\+\.par2$' -print0 \
84
        | xargs -0 -n1 -i rarslave "{}" "parsort"
111 ira 85
 
118 ira 86
    # When using <=findutils-4.2.20 (or thereabouts), use the following. The "+" operator
87
    # has changed in later versions.
88
    #find ./ -maxdepth 1 -iregex '.*\.par2$' \! -iregex  '.*\.vol[0-9]+\+[0-9]+\.par2$' -print0 \
89
    #    | xargs -0 -n1 -i rarslave "{}" "parsort"
90
 
96 irasnyd 91
    # Extract files, and delete the set if it extracted correctly
91 irasnyd 92
    sed 's/.par2$//' goodfiles | sed 's/.PAR2$//' > completefiles
89 irasnyd 93
 
96 irasnyd 94
    # for each FILENAME in completefiles, extract them
111 ira 95
    while read FILENAME;
89 irasnyd 96
 
91 irasnyd 97
        # we are out of filenames
98
        if [ -z "${FILENAME}" ]; then break; fi;
89 irasnyd 99
 
91 irasnyd 100
        # get the set of files associated with this par2
104 irasnyd 101
        do ls "${FILENAME}"* | grep -i "rar$\|r..$\|par2$" > currentset;
111 ira 102
 
103
        # remove pars from currentset, then get the first thing that
91 irasnyd 104
        # has "rar" or "r00" at the END only
105
        # store above result in EXFILE (file to extract)
106
        EXFILE="$(grep -vi "par2$" currentset | grep -m1 "rar$\|r00$")";
111 ira 107
 
91 irasnyd 108
        # if there are files to extract
109
        if [ ! -z "${EXFILE}" ]; then
110
            # (extract the file AND remove the current set) OR save the filename to "unextractable"
111 ira 111
            ( rar e -o+ "${EXFILE}" && del_currentset ) || echo "${FILENAME}" >> "${UNEX_LOG}";
91 irasnyd 112
        else
113
            echo "No File to extract, removing currentset";
114
            del_currentset;
115
        fi;
116
    done < completefiles
89 irasnyd 117
 
96 irasnyd 118
    # part for parjoin
93 irasnyd 119
    sed 's/.par2$//' unrepairable | sed 's/.PAR2$//' > parjoinfiles
104 irasnyd 120
 
93 irasnyd 121
    while read FILENAME;
122
 
111 ira 123
        # no more filenames
93 irasnyd 124
        if [ -z "${FILENAME}" ]; then break; fi
125
 
126
        # no appropriate file attached to this par2
127
        if [ ! -f "${FILENAME}".001 ]; then continue; fi;
128
 
95 irasnyd 129
        do rarslave "${FILENAME}" "parjoin";
93 irasnyd 130
 
131
    done < parjoinfiles
111 ira 132
 
96 irasnyd 133
    # Clean up temp files used
91 irasnyd 134
    cleanup_temp
135
 
96 irasnyd 136
    # print finishing message
91 irasnyd 137
    print_finished_message
111 ira 138
 
96 irasnyd 139
    # go back to the starting dir
91 irasnyd 140
    cd "${ORIG_DIR}"
141
}
142
 
96 irasnyd 143
### the "parjoin" command ###
95 irasnyd 144
rarslave_parjoin() {
145
    PJNAME="$1"
146
 
113 ira 147
    lxsplit -j "${PJNAME}".001                  ### Attempt to join the file
148
    par2repair "${PJNAME}".*???2                ### Attempt to repair the joined file
149
 
150
    RETURN_CODE=$?                              ### Get the return code of the repair
151
 
152
    if [ ${RETURN_CODE} -eq 0 ]; then           ### If the repair was successful
153
            rm "${PJNAME}".???*                 ### then remove all the source files
154
    else                                        ### If the repair was not successful
155
            rm "${PJNAME}"                      ### then remove the attempted file
156
    fi
157
 
95 irasnyd 158
}
159
 
113 ira 160
### the "parsort" command. attempts to repair and sort into ###
96 irasnyd 161
### "goodfiles" and "unrepairable" ###
95 irasnyd 162
rarslave_parsort() {
163
    PARNAME="$1"
164
 
165
    par2repair "${PARNAME}" \
96 irasnyd 166
    && echo "${PARNAME}" >> goodfiles \
95 irasnyd 167
    || echo "${PARNAME}" >> unrepairable
168
}
169
 
105 ira 170
### the "nfosort" command. moves all NFO's to ${NFODIR} ###
171
nfosort() {
172
    find "${DLDIR}" -iregex '.*\.nfo$' -exec mv '{}' "${NFODIR}" \;
173
}
174
 
113 ira 175
### the "remove extraneous" command. removes all *.1 files (repair leftovers)
176
### as well as moving all sfv's to ${SFVDIR}
177
remove_extraneous() {
178
    find "${DLDIR}" -iregex '^.*\.1$' -exec rm '{}' \;
179
    find "${DLDIR}" -iregex '^.*\.sfv$' -exec mv '{}' "${SFVDIR}" \;
180
}
181
 
96 irasnyd 182
### prints the beginning message ###
91 irasnyd 183
print_working_message() {
97 irasnyd 184
    echo '================================================================================'
91 irasnyd 185
    echo "Working in $(pwd)"
97 irasnyd 186
    echo '================================================================================'
91 irasnyd 187
}
188
 
96 irasnyd 189
### prints the ending message ###
91 irasnyd 190
print_finished_message() {
97 irasnyd 191
    echo '================================================================================'
91 irasnyd 192
    echo "Done in $(pwd)"
97 irasnyd 193
    echo '================================================================================'
91 irasnyd 194
}
97 irasnyd 195
 
196
### print a welcome message ###
197
print_welcome_message() {
198
    echo '================================================================================'
199
    echo '== This is rarslave, a free program designed to assist you in repairing and   =='
200
    echo '== extracting files that you have downloaded from USENET. It will work on any =='
201
    echo '== file set with a par2 and rar. It also works for sets with just par2        =='
202
    echo '== associated. In that case, it repairs then removes the par2 (if successful) =='
203
    echo '==                                                                            =='
204
    echo '== Written By ---- Ira Snyder                                                 =='
205
    echo '== Start Date ---- 06-08-2005 (ported from zsh)                               =='
206
    echo '== Last Revised -- 06-08-2005 (appears not to have major bugs now)            =='
207
    echo '== License ------- GNU General Public License v2                              =='
208
    echo '== License ------- http://www.gnu.org/licenses/gpl.txt                        =='
209
    echo '==                                                                            =='
210
    echo '== Thanks for using this program!                                             =='
211
    echo '================================================================================'
212
    echo
213
}
96 irasnyd 214
############################### END FUNCTIONS ##################################
215
################################################################################
89 irasnyd 216
 
91 irasnyd 217
GLOBAL_ORIG_DIR="$(pwd)"
89 irasnyd 218
 
96 irasnyd 219
# if we got no args, run in $DLDIR
93 irasnyd 220
if [ -z "$@" ]; then
97 irasnyd 221
 
222
    print_welcome_message
223
 
224
    # switch to the dir from the config and set up the environment
93 irasnyd 225
    cd "${DLDIR}"
226
    cleanup_logs
91 irasnyd 227
 
97 irasnyd 228
    # run rarslave recursively on every directory . and below
94 irasnyd 229
    find ./ -type d -print0 | xargs -0 -n1 | xargs -I{} rarslave "{}"
105 ira 230
    nfosort
113 ira 231
    remove_extraneous
94 irasnyd 232
    print_errors
233
 
96 irasnyd 234
    # Print Parting Message
94 irasnyd 235
    echo
236
    echo "All Finished. Good Bye!"
237
 
96 irasnyd 238
# we have some args...
93 irasnyd 239
else
96 irasnyd 240
    # if there is no 2nd arg, run in the directory given by $1
95 irasnyd 241
    if [ -z "$2" ]; then
242
        rarslave_dir "$@"
96 irasnyd 243
 
244
    # there is a second arg...
95 irasnyd 245
    else
96 irasnyd 246
 
247
        # run the built-in "parsort" command if we got it
95 irasnyd 248
        if [ "$2" = "parsort" ]; then
249
            rarslave_parsort "$1"
250
        fi
251
 
96 irasnyd 252
        # run the built-in "parjoin" command if we got it
95 irasnyd 253
        if [ "$2" = "parjoin" ]; then
254
            rarslave_parjoin "$1"
255
        fi
96 irasnyd 256
 
95 irasnyd 257
    fi
93 irasnyd 258
fi
91 irasnyd 259
 
260
cd "${GLOBAL_ORIG_DIR}"