Tuesday 21 January 2014

Automating PSP ISO compression under Linux

I've recently started to test PPSSPP, a very promising PSP emulator that is available on a variety of platforms.

I had a bunch of ISO images lying on my disk, and wanted to compress them as CSO to spare toom on my SD cards. I stumbled upon pspshrink which is a nice command-line PSP ISO compressor for Linux. Building it on my desktop was a trivial task.

Next I wrote the following Bash script to automate compression:


#!/bin/bash

EXEC_PATH="`pwd`" 
echo -e "Executing in $EXEC_PATH"

ISO_FOLDER=iso
CSO_FOLDER=cso
COMPRESS="$EXEC_PATH/pspshrink-1.1.2/pspshrink 9"

while read r;
do
   DIR="`dirname "$r"`"
   pushd "$DIR"

   ROM="`basename "$DIR"`"
   
   # US rom?
   US=`expr match "$ROM" '.*_US'`
   if [ "$US" -gt 0 ]; then 
       ROM="`expr substr "$ROM" 1 "$US"`"
   else
     # EU rom?
     EU=`expr match "$ROM" '.*_EU'`
     if [ "$EU" -gt 0 ]; then 
         ROM="`expr substr "$ROM" 1 "$EU"`"
     fi
   fi

   echo -e "\n########## Start $ROM"

   unrar e -o+ "$r"

   rename -v 's/\.ISO/.iso/' *.ISO

   ISO="`find -name "*.iso"`"
   CSO=""$EXEC_PATH"/"$CSO_FOLDER"/"$ROM".cso"
   if [ -f "$CSO" ]; then
       echo -e "Skip existing "$CSO""
   else
       $COMPRESS "$ISO" "$CSO"
   fi
 
   rm -vf *.r*

   popd

   echo -e "\n########## End $ROM"

done < <(find $EXEC_PATH/$ISO_FOLDER -name "*.rar" | sort)


Worked like a charm!

And finally a small script, recycling the same base loop, that checks for missing ROMs (the compress script handles only RAR archives).

#!/bin/bash

EXEC_PATH="`pwd`" 
echo -e "Executing in $EXEC_PATH"

ISO_FOLDER=iso
CSO_FOLDER=cso

while read d;
do
   ROM="`basename "$d"`"
   
   # US rom?
   US=`expr match "$ROM" '.*_US'`
   if [ "$US" -gt 0 ]; then 
       ROM="`expr substr "$ROM" 1 "$US"`"
   else
     # EU rom?
     EU=`expr match "$ROM" '.*_EU'`
     if [ "$EU" -gt 0 ]; then 
         ROM="`expr substr "$ROM" 1 "$EU"`"
     fi
   fi

   CSO=""$EXEC_PATH"/"$CSO_FOLDER"/"$ROM".cso"
   if [ -f "$CSO" ]; then
       echo -e "[OK] "$CSO""
   else
       echo -e "[KO] "$CSO""
   fi

done < <(ls -ld $EXEC_PATH/$ISO_FOLDER/* | sort)