#!/bin/sh
#This is makeiconfolder.sh version 0.0 by Dan Jensen (admin at leinir dot dk)
#
#Requirements:
#
#  Before you do anything else, make sure you have these programs:
#  Sodipodi (0.32 or later prefferably)
#  convert (comes with ImageMagik)
#
#What does it do?
#
#  It takes the following directory structure: (+ = directory, - = file)
#
# +(iconsetname or something else)
#  ++scalable
#    ++actions
#    | +-icon.svg
#    ++apps
#    | +-icon.svg
#    ++devices
#    | +-icon.svg
#    ++filesystems
#    | +-icon.svg
#    ++mimetypes
#      +-icon.svg
#
#  Any .svg files in that structure (and only there (plus subfolders)) are:
#  1. Converted into png files (960 by 960) using Sodipodi
#  2. Resize that png file to 16x16, 22x22, 32x32, 48x48, 64x64 and 128x128
#  3. Place the converted files in a directory structure as above, but with
#     'scalable' replaced with the resolution of the new .png
#  4. When the script is done, you only need to supply a index.desktop file
#     yourself and pack it in .tar.gz/.tar.bz2 and it's ready for distribution.
#
#  WARNING! This script takes a LONG time to run (due to Sodipodi, mainly)
#
#Installation:
#
#  Stuff it into wherever you want to run it from (I've got mine in 
#
#Usage:
#
#  Just run it (i.e. makeiconfolder.sh) (see above in What does it do? to
#  see, well, what it does)
#
#  
echo 'Making icons (every icon may take some time)'
echo '------------------------------------------------------------------ v 0.0'
echo ''

#Initialise array containing section names
sections=(actions apps devices filesystems mimetypes)

#Quickly traverse directories to get number of icons (for status when rendering bellow)
numoficons=0
for one in ${sections[@]};
  do {
    for two in $(find ./scalable/$one/ -name '*.svg' -printf '%P\n'); 
      do {
        let numoficons=$numoficons+1
      };
    done
  };
done

iconnum=0
for section in ${sections[@]}; do {
	#Make folders, just in case they're not already there
	mkdir -p $PWD/16x16/$section
	mkdir -p $PWD/22x22/$section
	mkdir -p $PWD/32x32/$section
	mkdir -p $PWD/48x48/$section
	mkdir -p $PWD/64x64/$section
	mkdir -p $PWD/128x128/$section
	for file in $(find ./scalable/$section/ -name '*.svg' -printf '%P\n'); do {
		#The file name minus .svg (for convert)
		IconName=${file%%.svg}
		#The relative filename for the original icon
		OriginalIcon='./scalable/'$section'/'$IconName'.svg'
		#Make sure we know which number this icon is
		let iconnum=$iconnum+1
		#Tell user what we're doing
		echo 'Making section '$section' icon '$IconName' (icon number '$iconnum' of '$numoficons')'
		#Render icon using Sodipodi to 960 by 960 PNG format temporary picture
		inkscape --without-gui --file=$OriginalIcon --export-png=$PWD/temp.png --export-width=960 --export-height=960 > /dev/null
		#Resize icons using ImageMagik's brilliant little convert program, directly into where it's supposed to be
		convert $PWD/temp.png -resize 16x16 -antialias $PWD/16x16/$section/$IconName.png > /dev/null
		convert $PWD/temp.png -resize 22x22 -antialias $PWD/22x22/$section/$IconName.png > /dev/null
		convert $PWD/temp.png -resize 32x32 -antialias $PWD/32x32/$section/$IconName.png > /dev/null
		convert $PWD/temp.png -resize 48x48 -antialias $PWD/48x48/$section/$IconName.png > /dev/null
		convert $PWD/temp.png -resize 64x64 -antialias $PWD/64x64/$section/$IconName.png > /dev/null
		convert $PWD/temp.png -resize 128x128 -antialias $PWD/128x128/$section/$IconName.png > /dev/null
		#Delete temporary picture
		rm -f $PWD/temp.png > /dev/null
	}; done
}; done
echo ''
echo Done making $thing icons. Enjoy!
