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