#!/bin/bash
#
#  Copyright 2014 fenexomega <github.com/fenexomega>
#  
#  Usage: change-colors [color]
#  Colors: default black blue brown cyan green grey darkred orange red violet yellow 
#
#  
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#  
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#  
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#  MA 02110-1301, USA.
#  
#  

#Var of useful directories
auxtxt='/tmp/aux.txt'
tmpsvg='/tmp/tmp.svg'

inputfolder="places-folders/scalable"
auxfolder="/tmp/new_folders"
outputfolder="../../places/scalable"

#List of Color Hex
#TODO this list needs to be dynamic and user-updated
# gonna think of a easy way to do this.
default='1abc9c'
black='2c3e50'
blue='3498db'
brown='d35400'
cyan='00d9ff'
green='27ae60'
grey='bdc3c7'
darkred='c0392b'
orange='e67e22'
red='e74c3c'
violet='9b59b6'
yellow='f1c40f'

#glyph Colors Hex
gdefault='304050'
gblack='d9d9d9'
gbrown='2c3e50'
gdarkred='E0E0E0'


showHelp(){
	echo Usage: change-colors [color]
	echo -e "Colors: default black blue brown cyan green grey darkred orange red violet yellow \n"
	echo -e "e.g.:\n change-colors red\n"
}


if [ "$1" == "--help" ] || [ "$1" == "-h" ]; then
	showHelp
	exit 0
fi

#If user have gdialog, use it
if [ -f /usr/bin/gdialog ]; then
	dialog='gdialog'
else
	dialog='dialog'
fi

if [ ! -d $inputfolder ] || [ ! -d $outputfolder ]; then
	if [ "$dialog" != "" ]; then
		$dialog --title "ERROR" --msgbox "places-folders not found. Exiting" 20 40
	else
		echo "ERROR places-folders not found. Exiting"
	fi
	exit 0
fi


if [ "$1" == "" ]; then
	$dialog --menu "Choose your folders color" 20 40 10 default '1abc9c' black '2c3e50' blue '3498db' brown 'd35400' cyan '00d9ff' green '27ae60' grey 'bdc3c7' darkred 'c0392b' orange 'e67e22' red 'e74c3c' violet '9b59b6' yellow 'f1c40f' custom '??????' 2> $auxtxt
	test "$?" != "0" && exit 1
	

	Color=$(cat $auxtxt)
	if [[ "$Color" == "custom" ]]; then
		$dialog --inputbox "Type the hexcolor for frontColor" 20 20 2> $auxtxt
		test "$?" != "0" && exit 1
		Color=$(cat $auxtxt)

		$dialog --inputbox "Type the hexcolor for glynphColor" 20 20 2> $auxtxt
		test "$?" != "0" && exit 1
		GColor=$(cat $auxtxt)
	else
		GColor="g"
		GColor+=$Color
		GColor=${!GColor}
		Color=${!Color}
	fi
	test "$GColor" == "" && GColor=$gdefault
else
	Color=${!1}
	if [ "$Color" == "" ]; then
		echo "Error: Invalid Color."
		showHelp
		exit 1
	fi
fi

#Change and copy the scalable images
cd $inputfolder/
mkdir $auxfolder/
for file in * ; do 
	if [[ $file == *.svg* ]] && [ $file != "$tmpsvg" ] ; then
	sed -e "s/$default/$Color/" $file | sed -e "s/$gdefault/$GColor/" > $tmpsvg
	cat $tmpsvg > "$auxfolder/$file" 
	fi
done
rm $tmpsvg 
cp $auxfolder/* ../../$outputfolder
rm -r $auxfolder


#Change and copy the small images
inputfolder="places-folders/small"
outputfolder="../../places/small"

cd ../../
cd $inputfolder/
mkdir $auxfolder/
for file in * ; do 
	if [[ $file == *.svg* ]] && [ $file != "$tmpsvg" ] ; then
	sed -e "s/$default/$Color/" $file | sed -e "s/$gdefault/$GColor/" > $tmpsvg
	cat $tmpsvg > "$auxfolder/$file" 
	fi
done
rm $tmpsvg 
cp $auxfolder/* ../../$outputfolder
rm -r $auxfolder

exit 0
