#!/bin/bash

# A script to convert the Deluge svg icons to png.

DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
data_dir="$DIR/../ui/data"
zopfli_iter="--iterations=100"
zopflipng_bin="zopflipng --filters=01234mepb --lossy_8bit --lossy_transparent -y"

# Create deluge png icon pack for all sizes.
for size in 16 22 24 32 36 48 64 72 96 128 192 256 512; do
    mkdir -p $data_dir/icons/hicolor/${size}x${size}/apps
    in_svg=$data_dir/pixmaps/deluge.svg
    out_png=$data_dir/icons/hicolor/${size}x${size}/apps/deluge.png
    rsvg-convert -w ${size} -h ${size} -o $out_png $in_svg
    if [ "$size" -gt 128 ]; then
        zopfli_iter=""
    fi
    echo $zopflipng_bin $zopfli_iter
    eval $zopflipng_bin $zopfli_iter $out_png $out_png
done

# Create deluge-panel png for systray.
for size in 16 22 24; do
    in_png=$data_dir/icons/hicolor/${size}x${size}/apps/deluge.png
    out_png=$data_dir/icons/hicolor/${size}x${size}/apps/deluge-panel.png
    cp $in_png $out_png
done

# Create deluge.ico icon from pngs.
for size in 16 32 48 64 128 256; do
    ico_infiles+="$data_dir/icons/hicolor/${size}x${size}/apps/deluge.png "
done
convert $ico_infiles $data_dir/pixmaps/deluge.ico

# Copy of deluge.svg to icon theme pack.
mkdir -p $data_dir/icons/hicolor/scalable/apps/
cp $data_dir/pixmaps/deluge.svg $data_dir/icons/hicolor/scalable/apps/deluge.svg

# Create 48px deluge.png.
cp $data_dir/icons/hicolor/48x48/apps/deluge.png $data_dir/pixmaps/deluge.png

# Create 16px png from deluge and status svgs.
for file in $data_dir/pixmaps/*.svg; do
    out_png=${file%.*}16.png
    rsvg-convert -w 16 -h 16 -o $out_png $file
    eval $zopflipng_bin $out_png $out_png
done

# Copy 16px deluge and status pngs to webui icons folder.
for icon in $data_dir/pixmaps/*16.png; do
    iconname=$(basename $icon)
    cp $icon $data_dir/../web/icons/${iconname::-6}.png
done
rm $data_dir/../web/icons/tracker*.png

for size in 32 192 512; do
    in_png=$data_dir/icons/hicolor/${size}x${size}/apps/deluge.png
    out_png=$data_dir/../web/icons/deluge-${size}.png
    cp $in_png $out_png

# Create apple and android touch icons with background colour.
apple_icon=$data_dir/../web/icons/deluge-apple-180.png
rsvg-convert -w 180 -h 180 -b '#599EEE' -o $apple_icon $data_dir/pixmaps/deluge.svg
eval $zopflipng_bin $apple_icon $apple_icon

# Create favicon.ico icon from pngs.
for size in 16 32 48; do
    web_ico_infiles+="$data_dir/icons/hicolor/${size}x${size}/apps/deluge.png "
done
convert $web_ico_infiles $data_dir/../web/icons/favicon.ico
