Code:
#!/bin/bash
# Translate argument 1 to usable name
inpath=$1
# Save where you are right now.
oldpwd=$(pwd)
# Check if the directory in argument 1 exists.
if [ -d $inpath ]; then
cd $inpath;
mkdir -p original;
# Move the original files away, this will generate a warning since the "original" directory will be "moved" in this also.
mv * $inpath/original &> /dev/null;
mkdir -p $inpath/repacked;
# Figure out which the first rar archive is, part01 or r00 versions doesn't matter that much.
relname=$(ls $inpath/original/*.rar|head -n1);
# Unpack it.
unrar e -inul $relname $inpath/repacked;
# If it fails, then exit
if [ $? -eq 0 ]; then
# Remove all the crap that we don't need and recreate it using our own standard.
relname=$(basename $relname | sed -e "s/\(?:\.part01|.rar$\)//g");
# Our own standard (this is very possibly the same as one of the above, but since there's more then 1 option it's better to recreate it)
outfile="$inpath/repacked/$relname.rar";
cd $inpath/repacked;
# Pack the files into 200Mb files (-v200000k is for 200Mb noted in Kb)
rar a -m0 -r -v200000k $outfile *;
# If it succeds we move the files around to the right place
if [ $? -eq 0 ]; then
outpath="$inpath/repacked";
mv $outpath/* $inpath;
# Delete the temporary "repacked" directory
rm -rf $outpath;
else
echo "Failed to compress files... exiting...";
fi
else
echo "Failed to extract files ($relname).. exiting";
fi
fi
# Return to where you started when executing the script.
cd $oldpwd
I'm not sure I understand what it is you want.
The original files are never changed, they're just moved to make room for the new files in the repacked catalog... which was supposed to be moved to the original location.
Anyway, the error was that a bracket (]) was against the 0 in "if [ $? -eq 0]"... bash doesn't like that.
What should happen now is the following:
1) Enter the path you put in when starting the script.
2) creates a "temporary" original directory
3) moves the files from the "root" to the original directory
4) creates a temporary directory named "repacked"
5) unrar's the content from the "original" directory to the "repacked" directory
6) if that went okey, then it figures out the name of the rar archive and repacks it in 200Mb files
7) moves the newly created files in the "repacked" directory into the "root" directory (../ that is)
If you want to remove the original files then you can always add a "rm -rf original" at the bottom (above the "cd $oldpwd") and it'll be the last the script does.
Best regards
Fredrik Eriksson