#!/bin/bash
#grep -o -P width=\".*?\" /home/dan/git/lollipop/Lollipop-Green/actions/scalable/application-exit.svg | sed -re 's/width="(.*?)"/\1/g'
_green() {
	tput setaf 2 ;
}

_white() {
	tput setaf 7 ;
}

_bold() {
	if [[ $1 != "" ]] ; then
		$1 ;
	fi ;
	tput bold ;
}

_tset() {
	tput sgr 0 ;
}

_gecho() {
	_bold _green
	for line ; do
		echo "${line}" ;
	done ;
	_tset ;
}

_wecho() {
	_bold _white
	for line ; do
		echo "${line}" ;
	done ;
	_tset ;
}

_copyDir() {
	if [ -e "$2" ] ; then
		rm -R "$2" ;
	fi ;
	cp -R "$1" "$2" ;
	
	cd "$2" ;
	if [ -e "bak" ] ; then
		rm -R "bak" ;
	fi ;
	cd .. ;
}

_scaleDir() {
	svgs=($(ls *.svg | xargs)) >/dev/null ;
	for svg in "${svgs[@]}" ; do
		_filterSyms "${svg}" ;
	done ;
}

_filterSyms() {
	target="$(readlink -q "${1}")" ;
	if [[ "${target}" == "" ]] ; then
		_gecho "${1}" ;
		_inkscapeSmall "${1}" ;
		_scaleSvg "${1}" ;
	fi ;
}

_inkscapeSmall() {
	_edit 'inkscape:window-width=.*' 'inkscape:window-width="527"' "$1"
	_edit 'inkscape:window-height=.*' 'inkscape:window-height="395"' "$1"
	_edit 'inkscape:zoom=.*' 'inkscape:zoom="0.01"' "$1"
	_edit 'inkscape:cx=.*' 'inkscape:cx="-2218.003443723141"' "$1"
	_edit 'inkscape:cy=.*' 'inkscape:cy="6432.795576772503"' "$1"
}

_edit() {
	sed -i "s/$1/$2/" "$3" ;
}

_scaleSvg() {
	inkscape --verb=FitCanvasToDrawing --verb=FileVacuum --verb=FileSave --verb=FileQuit "${1}" ;
}

root="${PWD}" ;
dirs=(actions animations apps categories devices emblems mimetypes places status stock/io) ;
for d in "${dirs[@]}" ; do
	_wecho "$d" ;
	cd "$d" ;
	_copyDir "scalable" "32" ;
	cd "32" ;
	_scaleDir ;
	cd "${root}" && cd "$d" ;
	_copyDir "32" "22"
	cd "${root}" ;
done ;

