#!/bin/sh
#This is makesvgdistro.sh version 0.0 by Dan Jensen (admin at leinir dot dk)
#
#Requirements:
#
#  Before you do anything else, make sure you have this program:
#  Sodipodi (0.32 or later prefferably)
#
#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
#  converted into png files (16 by 16 pixels) using Sodipodi directly into
#  a directory structure as above, but with 'scalable' replaced with the
#  resolution of the new .png
#
#  WARNING! This script takes a LONG time to run (due to Sodipodi, mainly)
#
#Usage:
#
#  Just run it (i.e. sh makesvgdistro.sh) (see above in What does it do? to
#  see, well, what it does)
#
#  
echo 'Making distribution (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

#Copy the contents of the animation folder (containing the throbber)
cp --recursive animation/* .


iconnum=0
for section in ${sections[@]}; do {
	#Make the folder for the current section
	mkdir -p $PWD/16x16/$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', SVG)'
		#Make sure SVG file is pure SVG
		#(export the possibly InkScape or SodiPodi tainted svg file to pure svg)
		inkscape --without-gui --file=$OriginalIcon --export-plain-svg=$OriginalIcon > /dev/null

		#Tell user what we're doing
		echo 'Making section '$section' icon '$IconName' (icon number '$iconnum' of '$numoficons', PNG)'
		#Render icon using inkscape to 16 by 16 PNG format picture
		inkscape --without-gui --file=$OriginalIcon --export-png=$PWD/16x16/$section/$IconName.png --export-width=16 --export-height=16 > /dev/null
	}; done
}; done
echo ''
echo Done making $numoficons icons. Enjoy!
