#!/bin/bash

function call_inkscape {
    # $1: width = height
    # $2: source file (*.svg)
    # $3: target file (*.png)
    echo -n "$1x$1 "
    inkscape -D -w $1 -h $1 $2 --export-png=$1x$1/$3 >/dev/null 2>&1
    if [ ! $? -eq 0 ]; then
        echo -n "ERROR "
    fi
}

function convert {
    # $1: source file
    echo -n "generating icons for $1... "
    NAME=`echo $1 | sed -e 's/svg$/png/'`

    if [[ $1 =~ Act.*\.svg ]]; then
        call_inkscape 16 $1 $NAME
    fi

    call_inkscape 32 $1 $NAME
    call_inkscape 48 $1 $NAME

    echo ""
}

which inkscape >/dev/null 2>&1
if [ ! $? -eq 0 ]; then
    echo "this script requires inkscape, aborting..."
fi

if [ "$1" == "" ]; then
    echo "generating ALL icons..."
    for i in *.svg; do
        convert "$i"
    done
else
    if [ -f $1 ]; then
        convert "$1"
    else
        echo "$1 does not exist or is no regular file, aborting..."
    fi
fi

