I’ve now started rewriting the original script, since the original was unable to deal with cases where only vol.XX+YY.PAR2 files are available to repair a set of files. For now, this version only repairs all files in one directory. It only unpacks a file, if the filename without suffix has *changed*. This way, it avoids unpacking the same file over and over again. For example, suppose a directory contains the following files:
file1.vol01+00.PAR2
file1.vol01+08.PAR2
file1.vol02+13.PAR2
file2.par2
file2.vol01+77.PAR2
Without any kind of string comparing, newsunpack would unpack file1 three times, and file2 two times! The script I am writing would unpack file1.vol01+00.PAR2, and then skip all the other names starting with file1, and then unpack file2.par2.
Here is what I got so far:
#!/bin/bash
unpack() {
oldfile=$1
ls *[pP][aA][rR]2 2> /dev/null | while read parfile ; do
if [ `echo "$parfile" | grep '..vol[0-9]\+[+-][0-9]\+.[pP][aA][rR]2'` ]
then
newfile=${parfile%.vol*[-+]*.[pP][aA][rR]2}
else
newfile=${parfile%.[pP][aA][rR]2}
fi
if [ $newfile == $oldfile ]
then
continue
else
/usr/local/bin/newsunpack.py $parfile
oldfile=$newfile
fi
done
return 0
}
if [ $# -eq 1 ] && [ -d $1 ]
then
pushd "$1" > /dev/null 2>&1
ls *[pP][aA][rR]2 2> /dev/null | while read parfile ; do
if [[ `echo "$parfile" | grep '..vol[0-9]\+[+-][0-9]\+.[pP][aA][rR]2'` ]]
then
firstfile=${parfile%.vol*[-+]*.[pP][aA][rR]2}
else
firstfile=${parfile%.[pP][aA][rR]2}
fi
/usr/local/bin/newsunpack.py $parfile
unpack $firstfile
popd > /dev/null 2>&1
break
done
exit 0
fi
Notice the break statement near the end. I’m afraid that is because I have not been able to find a way to read just the first par2 file in a directory. So I am using a while loop, and then simply breaking out of it at the end so it does not do any looping at all. It’s not pretty, but it works!
I apologize if it looks like gobbledygook, I am not using those <pre> tags anymore, as I have found that using them makes the code continue all the way off the page, and end up somewhere in China. And I just don’t know of any better way to post code examples in my posts, sorry :(