Kodi Community Forum
bash script: turns roms in a dir to fatsafe zip and .cut - Printable Version

+- Kodi Community Forum (https://forum.kodi.tv)
+-- Forum: Support (https://forum.kodi.tv/forumdisplay.php?fid=33)
+--- Forum: Supplementary Tools for Kodi (https://forum.kodi.tv/forumdisplay.php?fid=116)
+--- Thread: bash script: turns roms in a dir to fatsafe zip and .cut (/showthread.php?tid=74493)



bash script: turns roms in a dir to fatsafe zip and .cut - qe2xbmc - 2010-05-25

Posting this to hopefully help grab some street cred for the dev help I'm about to ask for
-------
Code:
#! /bin/bash
#romtoxbox.sh   v1.1
#run from a directory that CONTAINS folder '/rom', which should contain unzipped roms with proper names).
# the script creates output directories ./zip and ./cut,

##~/code/xbox/romtoxbox.sh -from ./ -rompath 'E:\emu\neogenesis\sgenroms' -metalabel '(Sega)' -shortmetalabel SG
# ^^ would create, as a long example, the shortcut file SGDisneyCollection-CastleIllusion&Qu.cut, the zip file DisneyCollection-CastleIllusion&Qu.zip (oops, the zip should have those two extra chars)
# contents of that .cut file will be:

###begin
#    <shortcut>
#  <path>E:\emu\neogenesis\default.xbe</path>
# <label>Disney Collection - Castle of Illusion & Quack Shot (Sega)</label>
#    <custom>
#        <game>E:\emu\neogenesis\sgenroms\DisneyCollection-CastleIllusion&Qu.zip</game>
#      </custom>
#</shortcut>
###end#

# note that the path to the default.xbe is determined by the -rompath argument.  You can override  by including -emuxbe [E:\..\.xbe] as the LAST arguement

# the error rate is about 1/500, when working with my mostly non-redundant inputs, and even then, it only knocks out the remaining non-unique titles over 40 characters.

#these is my setup:
#~/code/xbox/romtoxbox.sh -from ./ -rompath 'E:\emu\MednafenX\nesroms' -metalabel '(Nintendo)' -shortmetalabel NS
#~/code/xbox/romtoxbox.sh   -rompath 'E:\emu\neogenesis\sgenroms' -metalabel '(Sega 32X)' -shortmetalabel S3
#~/code/xbox/romtoxbox.sh  -rompath 'E:\emu\neogenesis\sgenroms' -metalabel '(Sega)' -shortmetalabel SG
#~/code/xbox/romtoxbox.sh  -rompath 'E:\emu\zSNESx\roms' -metalabel '(SNES)' -shortmetalabel SN
#~/code/xbox/romtoxbox.sh  -rompath 'E:\emu\Surreal64\Roms' -metalabel '(N64S)' -shortmetalabel N6
#~/code/xbox/romtoxbox.sh  -rompath 'E:\emu\bliss-intellivision\intvroms' -metalabel '(Intellivision)' -shortmetalabel IV
#~/code/xbox/romtoxbox.sh  -rompath 'E:\emu\adamx_v3\adamgames' -metalabel '(Colecovision)' -shortmetalabel CO
#~/code/xbox/romtoxbox.sh  -rompath 'E:\emu\atari-z26\a26roms' -metalabel '(Atari 2600)' -shortmetalabel A2
#~/code/xbox/romtoxbox.sh  -rompath 'E:\emu\Surreal64\Roms' -metalabel '(N64)' -shortmetalabel N6
mkdir zip rom cut
#################
# Process Command Line#
from='./'
while [ $# -gt 0 ] #####
    do
    case $1 in
    -from)
        if [ -n "$2" ]; then from="$2"; echo  from set to "$2"; else echo "$2 isn't a directory"; exit; fi; shift 2;;
    -rompath)
        rompath="$2"; echo  rompath set to "$2"; emuxbe=`sed -E -e 's/[\][^\]*$/\\\default.xbe/' <<<"$rompath"`;shift 2;;
    -metalabel)
        metalabel="$2"; echo  metalabel set to "$2"; shift 2;;
    -shortmetalabel)
        shortmetalabel="$2"; echo  metalabel set to "$2"; shift 2;;
    -emuxbe)
        emuxbe="$2"; echo  emuxbe set to "$2"; shift 2;;
    *)
        echo "bad command line? "$1" is a bad argument"; exit;;    
    esac
done

sourcedir="$from/rom"
iter=0
cutdir="./cut"
zipdir="./zip"
fatfilter='
-e s/"//g
-e s/\*//g
-e s/\///g
-e s/://g
-e s/[\>]//g
-e s/[\<]//g
-e s/[?]//g
-e s/\+//g
-e s/,//g
-e s/\.//g
-e s/\;//g
-e s/\=//g

-e s/[\]//g
-e s/[|]//g
-e s/""//
-e s///
'
fatfilter=`sed -e s/$^// <<<"$fatfilter"`
x="`ls $sourcedir -1 | grep \\....\$`"
qlog()
{
((qlogc++))
#if [ "beverbose" ]; then
echo -e "-$qlogc- $@ -qlog-"
#fi
#qlogv="$qlogv""\n-q- $@ -q-"
}

qlog "qlog is defined `date`"

while [ `echo "$x" | wc -c` -gt 1 ]; do
read qvar <<<"$x" # pull
x=`sed 1d <<<"$x"` #pop
qlog "$qvar" qvar
cleanname=`sed -E -e 's/\[[^]]*\]//g' -e 's/\([^\)]*\)//g' -e 's/\....$//' -e 's/[ ]*\$//'  -e 's/  / /' <<<"$qvar"`
qlog "$cleanname" cleanname
#fatname=sed` <<<"$cleanname"`
#qlog "$fatname"
fatname=`sed $fatfilter <<<"$cleanname"`
qlog "$fatname" fatname1
fatsafe=`sed s/\'//g <<<"$fatname"`

qlog "$fatsafe" work

#while [ "`echo "$i" | wc -c`" -gt 36 ]; do

while [ `wc -c <<<"$fatsafe"` -gt 36 ]; do
((iter++))
case $iter in

1)
    fatsafe=`sed -e 's/ [Tt]he //' -e 's/ [Oo]f / /' -e 's/ [aA] / /' <<<"$fatsafe"`;
    qlog "$fatsafe" 1
    ;;
2)
    fatsafe=`sed -e 's/ //g' <<<"$fatsafe"`
    qlog "REACHED 2"
    qlog "$fatsafe" 2
    ;;
3)
    qlog "REACHED 3"
    fatsafe=${fatsafe:0:34}    
    qlog "$fatsafe" 3
    ;;

*) qlog ERROR "$fatsafe"; exit;;
    

esac
done
iter=0
#fatsafe names:
zipname="$fatsafe"
cutname="$fatsafe"

# i=`sed -e 's/ \$//' -e 's/ \$//' -e 's/ \$//' <<<"$i"` #lose the space

# echo final fatsafe is "$i" cleanname "$cleanname"
#<thumb>E:\emu\\$qvar\icon.png</thumb>



# prevent overwriting; foo1.bar foo2.bar..

qi="";
#echo "$cutdir"/"$shortmetalabel""$fatsafe""$qi".cut
while [ -r "$cutdir/$shortmetalabel$fatsafe$qi.cut" ];  
do echo "EXISTED: $qi";
qi=$((qi+1)); done;
cutfile="$cutdir/$shortmetalabel$fatsafe".cut
fatsafe="$cutname$qi"
qi="";
########///
xml="
    <shortcut>
   <path>$emuxbe</path>
   <label>$cleanname $metalabel</label>
      <custom>
         <game>$rompath\\$fatsafe.zip</game>
      </custom>
</shortcut>"
########///

echo "$xml" > "$cutfile"
#echo "$xml" > "$cutdir"/"$shortmetalabel""$cutname".cut
zipfile=
zip "$zipdir"/"$fatsafe"  "$sourcedir"/"$qvar"


echo cutfile "$cutfile"
echo zipfile "$zipfile"

#    echo "$part1" > "$qvar".cut
    #echo "$qvar" qvar2
    done
date