A shell script for recursively repairing and unrarring usenet binaries

After downloading binaries with pan, I still need to verify their integrity, repair them if necessary with par2repair, then unrar them. This is something that *must* be automated. After failing to find a replacement for the excellent auto unpack program available for Windows users, I decided to use hellanzb for this too. You can invoke hellanzb like this to check a directory and repair the files:

$ hellanzb.py -L -p /path/to/files

But hellanzb (AFAIK) can only be invoked on a single directory, it does not work recursively. Hopefully, that will be fixed in the next version. I sometimes have downloads in more than twenty sub-directories that I want to repair and unpack! So I decided to write a little script, which I will copy/paste here, in case anyone wants to use it as an excellent example of bad shell scripting practices.
The dependency for use is, obviously, hellanzb. Please keep in mind that you are always yourself responsible for the code you execute on your system! I should also mention that, even though it seems to work for me, it is NOT finished, but I thought it might be interesting for someone else in the mean time.
Anyway, here it is:

#!/bin/bash

MAX_ARGS=2

if [ -z $1 ]
then
echo "No arguments provided. Usage: extract.sh [-r] {directory}"
exit 1
fi

if [ "$#" -gt "$MAX_ARGS" ]
then
echo "Usage: extract.sh [-r] {directory}"
echo "The script expects one or two arguments, no more, no less."
exit 1
fi

#normal repair/unrar functionality
if [[ "$#" == "1" && -d $1 ]]
then
/usr/bin/hellanzb.py -L -p $1
exit 0
fi

if [[ $1 -eq "-r" && -d $2 ]]
then
# recursive way
for dir in `find $2 -type d` ;
do
/usr/bin/hellanzb.py -L -p $dir
done
exit 0
fi

Enjoy! And sorry about the formatting, even using code tags does not prevent WordPress from ruining my pretty formatting with indentations.

Leave a comment