#!/bin/sh
#
#  Generate puppy-svg-bundle.gz from scalable Puppy Icon Theme archive.
#  The gzip'ed bundle is suitable for Tcl array set, i.e. made up of
#  proper list elements with the icon name (path name w/o .svg) and
#  the SVG data for rendering.
#
#  Tools required: gzip, perl, tclsh, unzip, wget

test -f master.zip || wget -c \
    https://github.com/puppylinux-woof-CE/puppy_icon_theme/archive/master.zip

test -d puppy_icon_theme-master || \
    unzip -q master.zip

test -z "$TCLSH" && TCLSH=tclsh

DIR=puppy_icon_theme-master/icons

# Fix problematic svg files...
perl -pi -e 's@height="500px.* 100 100"@height="100" width="100"@g' \
    $DIR/application-x-cd-image.svg
perl -pi -e 's@viewBox=".*100 100"@height="100" width="100"@g' \
    $DIR/add.svg $DIR/application-kernel.svg $DIR/application-pet.svg \
    $DIR/application-rtf.svg $DIR/application-x-compressed-tar.svg \
    $DIR/application-x-deb.svg $DIR/application-x-delta.svg \
    $DIR/application-x-executable.svg $DIR/application-x-rpm.svg \
    $DIR/application-x-squashfs-image.svg $DIR/apply.svg
perl -pi -e 's@ viewBox=".*100 100"@@g' $DIR/vlc.svg

for file in $(cd $DIR ; find . -name '*.svg') ; do
    FILE=$(echo $file | cut -c 3-)
    SVG=$DIR/$FILE
    export FILE SVG
    $TCLSH <<'EOF'
set f [open $env(SVG) r]
set n [file rootname $env(FILE)]
puts [list $n]
puts [list [read $f]]
close $f
exit 0
EOF
done | gzip -9 > puppy-svg-bundle.gz
