views:

358

answers:

2

Im completely new to Bash scripting but I've been told, with little help, to create a file that compresses textures into PVR format only if the file has been modified since the last time the script was run. Heres the code I have so far:


# variables
TEXTURE_TOOL=/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/texturetool
INPUT_DIR="/Data/Mobile"
OUTPUT_DIR="/Data/iPhone"
IMAGE_GREP="\(.bmp\|.png\)"
OTHER_GREP="\.b3d$"

echo "Starting backup of directory $INPUT_DIR to directory $OUTPUT_DIR"

# cycle through the input directory for images we can compress
echo "Compressing textures!"
for i in $( ls -a "$INPUT_DIR" | grep $IMAGE_GREP  ); 
     do
        if test "$OUTPUT_DIR/$i.pvr" -nt "$INPUT_DIR/$i"; then
     # check to see output's status
     echo "Compressing file $i!"

     # compress and store in output directory
     $TEXTURE_TOOL -m -e PVRTC --bits-per-pixel-2 -o "$OUTPUT_DIR/$i.pvr" -f PVR "$INPUT_DIR/$i"
        fi
     done

# cycle through the input directory for models we can export
echo "Moving models!"
for i in $( ls -a "$INPUT_DIR" | grep $OTHER_GREP  ); 
     do
     # check to see output's status
     echo "Moving model file $i!"

     # cp to output directory
     cp "$INPUT_DIR/$i" "$OUTPUT_DIR/$i"
     done

Using one of the questions here I tried to do the timestamp checking but I it isnt working and I'm pretty sure its because I dont fully understand the code.

Can anyone suggest what Im doing wrong

Thanks, Michael A

+3  A: 
test "$OUTPUT_DIR/$i.pvr" -nt "$INPUT_DIR/$i";

Checks to see if $OUTPUTDIR/$i.pvr is newer than $INPUTDIR/$i, I think you want to do it the other way around.

test "$INPUT_DIR/$i" -nt "$OUTPUT_DIR/$i.pvr";

To check to see fi the input is newer than the output.

David
+4  A: 

Doing things only if some files are newer than expected is best achieved with make. This is a little more complex to learn but very efficient.

mouviciel
+1. It might be more complex to learn *properly*, but for a relatively simple task like the OP's, it could probably be solved pretty quickly by just playing around with existing makefiles. So to the OP, don't be put off by the warning of complexity; it is indeed worth the effort.
Mark Rushakoff