#!/bin/sh

#
# This script generates a directory and an automake fragment from an SVG
# file. It's useful when you have an SVG and want to pre-render icons of
# various sizes from it, then install them into the users icon theme.
#
# For autopackage users, put the following line in your specfile:
#
#   installIcon share/icons/hicolor
#
# It uses rsvg to render the icons

if [[ "$1" == "" ]] || [[ "$1" == "--help" ]] || [[ "$1" == "-h" ]]; then
    echo "Usage: make-icons myprogram.svg"
    echo
    echo "Generates a subdirectory called icons with an icon theme tree"
    echo "in it. Also outputs some automake code you can copy/paste".
    exit 0
fi

if ! which rsvg >/dev/null; then
    # a part of librsvg
    echo "make-icons: this script needs the rsvg program installed"
    exit 1
fi

if [ ! -e $1 ]; then
    echo "make-icons: the given file does not exist"
    exit 1
fi

mkdir -p icons/scalable
cp $1 icons/scalable

echo 'iconSVGdir = $(datadir)/icons/hicolor/scalable/apps'
echo "iconSVG_DATA = icons/scalable/$1"

for size in 128 64 48 24 16; do
    mkdir -p icons/${size}x${size}
    rsvg --width=$size --height=$size $1 icons/${size}x${size}/${1/.svg/}.png
    echo "icon${size}dir = \$(datadir)/icons/hicolor/${size}x${size}/apps"
    echo "icon${size}_DATA = icons/${size}x${size}/${1/.svg/}.png"
done
