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