#/bin/bash

#       Copyright 2011 Mithat Konar
#
#       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 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, write to the Free Software
#       Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#       MA 02110-1301, USA.

# Make an icon theme that is a hybrid of other icon themes.
#
# Designed to work with GNOME-colors; can be adapted to work with others.
# No spaces allowed in file names or paths!
#
# 2011/06/11 17:38:44

###
### Constants
###

# Specify the location and name of directory for generated theme:
DESTINATION_DIR=`pwd`
GENERATED_ICON_SET_DIRNAME=gnome-colors-hybrid

# Specify the location and name of source icons and theme used as fallback:
SOURCE_DIR=/usr/share/icons
DEFAULT_ICON_SET=gnome-brave

# Set the following as desired for the index.theme file:
NAME="GNOME-Hybrid"
COMMENT="Colorized Gnome Icons"
INHERITS="gnome-colors-common"

# Specify overridden icon theme for each icon
declare -A ICONLIST=(
    #~ [actions/add-folder-to-archive]=gnome-brave
    #~ [actions/document-open]=gnome-brave
    [actions/edit-select-all]=gnome-human
    #~ [actions/folder-copy]=gnome-brave
    #~ [actions/folder-move]=gnome-brave
    #~ [actions/folder-new]=gnome-brave
    [actions/go-bottom]=gnome-wise
    [actions/go-down]=gnome-wise
    [actions/go-first]=gnome-wise
    [actions/go-jump]=gnome-wise
    [actions/go-last]=gnome-wise
    [actions/go-next]=gnome-wise
    [actions/go-previous]=gnome-wise
    [actions/go-top]=gnome-wise
    [actions/go-up]=gnome-wise
    #~ [actions/view-fullscreen]=gnome-brave
    #~ [actions/view-restore]=gnome-brave

    #~ [apps/alacarte]=gnome-brave
    #~ [apps/fusion-icon]=gnome-brave
    #~ [apps/gnome-panel-workspace-switcher]=gnome-brave
    #~ [apps/preferences-desktop-remote-desktop]=gnome-brave
    #~ [apps/system-file-manager]=gnome-brave
    #~ [apps/vinagre]=gnome-brave

    #~ [categories/preferences-system-network]=gnome-brave

    #~ [devices/computer]=gnome-brave
    #~ [devices/video-display]=gnome-brave

    #~ [emblems/emblem-desktop]=gnome-brave

    #~ [places/folder-documents]=gnome-brave
    #~ [places/folder-downloads]=gnome-brave
    #~ [places/folder-music]=gnome-brave
    #~ [places/folder-pictures]=gnome-brave
    #~ [places/folder-publicshare]=gnome-brave
    #~ [places/folder-remote]=gnome-brave
    #~ [places/folder-saved-search]=gnome-brave
    #~ [places/folder]=gnome-brave
    #~ [places/folder-videos]=gnome-brave
    #~ [places/network-workgroup]=gnome-brave
    #~ [places/user-desktop]=gnome-brave
    #~ [places/user-home]=gnome-brave

    #~ [status/audio-volume-high]=gnome-brave
    #~ [status/audio-volume-low]=gnome-brave
    #~ [status/audio-volume-medium]=gnome-brave
    #~ [status/folder-open]=gnome-brave
    [status/network-error]=gnome-wine
    #~ [status/network-idle]=gnome-brave
    #~ [status/network-offline]=gnome-brave
    #~ [status/network-receive]=gnome-brave
    [status/network-transmit-receive]=gnome-wise
    [status/network-transmit]=gnome-human
)

###
### MAIN
###
cd "$DESTINATION_DIR"
#~ if [ -e "$GENERATED_ICON_SET_DIRNAME" ] ; then
    #~ echo "\"$GENERATED_ICON_SET_DIRNAME\" exists; exiting"
    #~ exit 1
#~ fi
# TODO: add user selectable exit
rm -Rf $GENERATED_ICON_SET_DIRNAME
mkdir "$GENERATED_ICON_SET_DIRNAME"

# make list of DIRECTORIES
DIRECTORIES=""
for file in `ls -1 "${SOURCE_DIR}/${DEFAULT_ICON_SET}"` ; do
    if [ -d "${SOURCE_DIR}/${DEFAULT_ICON_SET}/${file}" ] ; then 
        DIRECTORIES="$DIRECTORIES $file"
    fi
done

# copy directories full-o-icons from default icon set
for dir in $DIRECTORIES ; do
    cp -R "${SOURCE_DIR}/${DEFAULT_ICON_SET}/${dir}" "$GENERATED_ICON_SET_DIRNAME"
done

# generate index.theme file
cat > "${GENERATED_ICON_SET_DIRNAME}/index.theme" <<EOT
[Icon Theme]
Name=$NAME
Comment=$COMMENT
Inherits=$INHERITS
Example=folder

Directories=16x16/actions,16x16/apps,16x16/categories,16x16/devices,16x16/emblems,16x16/places,16x16/status,22x22/actions,22x22/apps,22x22/categories,22x22/devices,22x22/emblems,22x22/places,22x22/status,24x24/actions,24x24/apps,24x24/categories,24x24/devices,24x24/emblems,24x24/places,24x24/status,32x32/actions,32x32/apps,32x32/categories,32x32/devices,32x32/emblems,32x32/places,32x32/status,scalable/actions,scalable/apps,scalable/categories,scalable/devices,scalable/emblems,scalable/places,scalable/status

[16x16/actions]
Size=16
Context=Actions
Type=Fixed

[16x16/apps]
Size=16
Context=Applications
Type=Fixed

[16x16/categories]
Size=16
Context=Categories
Type=Fixed

[16x16/devices]
Size=16
Context=Devices
Type=Fixed

[16x16/emblems]
Size=16
Context=Emblems
Type=Fixed

[16x16/places]
Size=16
Context=Places
Type=Fixed

[16x16/status]
Size=16
Context=Status
Type=Fixed

[22x22/actions]
Size=22
Context=Actions
Type=Fixed

[22x22/apps]
Size=22
Context=Applications
Type=Fixed

[22x22/categories]
Size=22
Context=Categories
Type=Fixed

[22x22/devices]
Size=22
Context=Devices
Type=Fixed

[22x22/emblems]
Size=22
Context=Emblems
Type=Fixed

[22x22/places]
Size=22
Context=Places
Type=Fixed

[22x22/status]
Size=22
Context=Status
Type=Fixed

[24x24/actions]
Size=24
Context=Actions
Type=Fixed

[24x24/apps]
Size=24
Context=Applications
Type=Fixed

[24x24/categories]
Size=24
Context=Categories
Type=Fixed

[24x24/devices]
Size=24
Context=Devices
Type=Fixed

[24x24/emblems]
Size=24
Context=Emblems
Type=Fixed

[24x24/places]
Size=24
Context=Places
Type=Fixed

[24x24/status]
Size=24
Context=Status
Type=Fixed

[32x32/actions]
Size=32
Context=Actions
Type=Fixed

[32x32/apps]
Size=32
Context=Applications
Type=Fixed

[32x32/categories]
Size=32
Context=Categories
Type=Fixed

[32x32/devices]
Size=32
Context=Devices
Type=Fixed

[32x32/emblems]
Size=32
Context=Emblems
Type=Fixed

[32x32/places]
Size=32
Context=Places
Type=Fixed

[32x32/status]
Size=32
Context=Status
Type=Fixed

[scalable/actions]
Size=48
MinSize=32
MaxSize=256
Context=Actions
Type=Scalable

[scalable/apps]
Size=48
MinSize=32
MaxSize=256
Context=Applications
Type=Scalable

[scalable/categories]
Size=48
MinSize=32
MaxSize=256
Context=Categories
Type=Scalable

[scalable/devices]
Size=48
MinSize=32
MaxSize=256
Context=Devices
Type=Scalable

[scalable/emblems]
Size=48
MinSize=32
MaxSize=256
Context=Emblems
Type=Scalable

[scalable/places]
Size=48
MinSize=32
MaxSize=256
Context=Places
Type=Scalable

[scalable/status]
Size=48
MinSize=32
MaxSize=256
Context=Status
Type=Scalable

EOT

# for each icon in list, from each each directory overwrite icons
# (and other files, e.g. *.desktop, *.icon, etc.)
# note: this isn't the most efficient way to traverse, but so what.
for icon_name in "${!ICONLIST[@]}" ; do
    source_theme=${ICONLIST[$icon_name]}
    for dir in $DIRECTORIES ; do
        # this becomes a lot easier if we disallow spaces in paths and names
        source_subdir=${SOURCE_DIR}/${source_theme}/${dir}
        dest_subdir=`dirname "${GENERATED_ICON_SET_DIRNAME}/${dir}/${icon_name}"`
        for file in `ls -1 ${source_subdir}/${icon_name}.*` ; do
            cp $file $dest_subdir
        done
    done
done