#!/usr/bin/bash
#
#    This file is a part of Clarity vector icon theme for gtk
#    Copyright (c) 2010 Jakub Jankiewicz <http://jcubic.pl> 
#
#    This icons are free: 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 3 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, see <http://www.gnu.org/licenses/>.
#

RULES_FILE=src/compound_icons_rules
ERROR=false
echo "Generating Makefile"

{
    echo "# This is generated Makefile from Clarity Vector Icon Theme for GTK"
    echo "#"
    echo "# Copyright (c) 2010-2012 Jakub Jankiewicz <http://jcubic.pl>"
    echo "# Licensed under GNU GPL Version 3 license <http://www.gnu.org/copyleft/gpl.html>"
    echo "#"
    echo "# This file was automaticly generated by configure script"
    echo "# Don't modified it, if you want to do some changes, do this"
    echo "# in configure script"
    echo "#"
    echo
    echo "ALL: canus"
    echo
    echo 'scalable:'
    echo -e "\t@echo unpack symlinks"
    echo -e "\ttar xzf scalable.tar.gz"
    echo
    echo 'static-files:'
    echo -e "\t@bash -c \"find static -type f | sed -e 's/[^\/]*\/\(.*\)/echo copy scalable\/\1;cp & scalable\/\1/' | bash\""
    #defined themes rules
    for i in src/template_*.svg; do
        echo
        name=`echo $i | sed -e 's/.*template_\([^\.]*\).*/\1/'`
        echo -n "Process template $name ... " > /dev/stderr
        echo ${name}: scalable static-files gen_$name elements _16x16
        echo
        echo gen_${name}: 
        echo -e "\t@echo 'Building icons for theme $name ... '"
        echo -e "\t@bash -c 'for i in \`find src -mindepth 2 -name \"*.svg\" -type f\`; do \\"
        echo -e "SHAPE=\`grep \"path.*d=\" \$\$i | sed -e \"s/.*d=\\\"\([^\\\"]*\)\\\".*/\1/\";\`;\\"
        echo -e "TITLE=\`grep \"<title>\" \$\$i | sed -e \"s/.*<title>\([^<]*\)<\/title>/\1/\"\`;\\"
        echo -e "sed -e \"s/{{PATH}}/\$\${SHAPE}/\" -e \"s/{{TITLE}}/\$\${TITLE}/\" $i > \`echo \$\$i | sed -e \"s/src/scalable/\"\`;\\"
        echo -e "echo building \`echo \$\$i | sed -e \"s/src/scalable/\"\`;\\"
        echo "done;'"
        echo -e "[\x1b[32m\x1b[1mOK\x1b[0m]" > /dev/stderr
    done
    echo
    echo "elements:"
    echo -e "\t@echo -n 'building additional icons ... '"

    IFS=$'\n'
    line=0
    echo -n "Process compound icons rules ... " > /dev/stderr
    for i in $(grep -v '^#' $RULES_FILE | tr -d '\r' | grep -v '^ *$'); do
        if $(echo $i | grep -e ".*\.svg *=.*\+.*" > /dev/null); then
            desc=$(echo $i | sed -e 's/\([^= ]*\) *=.*/\1/')
            source=$(echo $i | sed -e 's/[^=]*= *\([^ ]*\) *\+.*/\1/')
            element=$(echo $i | sed -e 's/.*+ *\(.*\) *$/\1/')
            if [ ! -e src/$element ]; then
                {
                    echo -e "[\x1b[31m\x1b[1mFAIL\x1b[0m]"
                    echo "ERROR in $RULES_FILE"
                    echo "file src/$element don't exists"
                    echo "line: $i"
                } > /dev/stderr
                exit;
            else
                echo -e "\t@sed -e \"s/<\\/svg>/    <path \`grep 'id=\\\"shape\\\" d=' src/$element | sed -e 's/.*\\(d=[^\\/]*\)\\/.*/\\1/'\` \\/\\>\\\\n\\<\\/svg\\>/\" scalable/$source > scalable/$desc;"
                echo -e "\t@echo \"building scalable/$desc\""
            fi
        else
            {
                echo -e "[\x1b[31m\x1b[1mFAIL\x1b[0m]"
                echo "ERROR in $RULES_FILE"
                echo "line: $i"
                
            } > /dev/stderr
            exit
        fi
        (( line++ ))
    done
    echo -e "[\x1b[32m\x1b[1mOK\x1b[0m]" > /dev/stderr
    echo -n "Generating distribution rules ... " > /dev/stderr
    cd src/distributor-logos;
    for i in *; do
        echo -e "$i:" | sed -e 's/\.svg//'
        echo -e "\tln -sf ../distributor-logos/$i scalable/places/start-here.svg"
        echo -e "\tln -sf ../distributor-logos/$i scalable/places/gnome-main-menu.svg"
        echo -e "\tln -sf ../distributor-logos/$i scalable/places/distributor-logo.svg"
        echo -e "\tln -sf ../distributor-logos/${i/svg/png} 16x16/places/start-here.png"
        echo -e "\tln -sf ../distributor-logos/${i/svg/png} 16x16/places/gnome-main-menu.png"
        echo -e "\tln -sf ../distributor-logos/${i/svg/png} 16x16/places/distributor-logo.png"
        echo
    done
        echo -ne "[\x1b[32m\x1b[1mOK\x1b[0m]\nGenerating simple test ... " > /dev/stderr
    echo
    echo '_16x16:'
    echo -e "\t@tar xzf 16x16.tar.gz"
    echo -e "\t@find scalable/ -type f | sed -e 's/scalable\(.*\)svg/echo building 16x16\1png; rsvg-convert -w 16 -h 16 & > 16x16\1png/' | bash"
    echo
    echo 'symlinks:'
    echo -e "\t@test -d scalable || ( echo \"folder scalable doesn't exists\"; false )"
    echo -e "\t@tar czvf scalable.tar.gz scalable"
    echo -e "\t@find scalable/ -type d | sed -e 's/scalable\(.*\)/test -d \"16x16\1\" || mkdir 16x16\1/' | bash"
    echo -e "\t@find scalable/ -type l | xargs file | sed -e \"s/scalable\(.*\)svg:.*\\\`\(.*\)svg'/ln -sf \2png 16x16\1png/\" | bash"
    echo -e "\t@test -d 16x16 || ( echo \"folder 16x16 doesn't exists\"; false )"
    echo -e "\t@tar czvf 16x16.tar.gz 16x16"
    echo
    echo "clean:"
    echo -e "\t@echo cleaning..."
    echo -e "\t@test -d scalable && rm -r scalable || true"
    echo -e "\t@test -d 16x16 && rm -r 16x16 || true"
    echo -e "\t@bash -c 'for i in \`find . -name \"*-stamp\"\`; do rm \$\$i; done'"
    #test rule
    echo
    echo "install:"
    echo -e "\tmkdir debian/clarity-icon-theme/usr/share/icons/Clarity"
    for i in configure index.theme Makefile README scalable.tar.gz 16x16.tar.gz change-theme; do
      echo -e "\tcp $i debian/clarity-icon-theme/usr/share/icons/Clarity"
    done
    for name in static src scalable 16x16; do
      echo -e "\tcp -r $name debian/clarity-icon-theme/usr/share/icons/Clarity"
    done
    echo
    echo "source:"
    echo -e "\tdebuild -S"
    echo
    echo "deb:"
    echo -e "\tdpkg-buildpackage -rfakeroot"
    echo -e "[\x1b[32m\x1b[1mOK\x1b[0m]" > /dev/stderr

} > Makefile

echo -e "\nMakefile generated, now you can run 'make' to create your icons\n"



