#!/usr/bin/env python
# vim: fileencoding=utf-8 et sw=4 ts=4 tw=80:

# Copyright (C) 2011  Björn Ricks <bjoern.ricks@intevation.de>
#
# 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.

import os
import os.path
import shutil
import sys
import re

from optparse import OptionParser

def load_mapping_list(filename):
    iconlist = []
    f = open(filename, 'r')
    pattern = re.compile(r'\s+')
    for iconfilename in f:
        iconfilename = iconfilename.strip()
        if not iconfilename or iconfilename.startswith("#"):
            continue
        icon = pattern.split(iconfilename)
        iconlist.append(icon)
    f.close()

    return iconlist

def copy_icons(iconlist, src, dest):
    sizes = ["8x8", "128x128", "22x22", "32x32", "64x64",
             "16x16", "256x256", "48x48", "scalable"]

    types = ["png", "xpm", "svg", "svgz"]

    for icon in iconlist:
        copied = False
        src_icon = icon[0]
        src_section = icon[1]
        dest_icon = icon[2]
        if len(icon) > 3:
            dest_section = icon[3]
        else:
            dest_section = src_section
        for size in sizes:
            src_dir = os.path.join(src, size, src_section)
            dest_dir = os.path.join(dest, size, dest_section)
            for ftype in types:
                src_path = os.path.join(src_dir, src_icon + "." + ftype)
                dest_path = os.path.join(dest_dir, dest_icon + "." + ftype)
                if os.path.isfile(src_path):
                    if not os.path.exists(dest_dir):
                        print "creating directory '%s'" % dest_dir
                        os.makedirs(dest_dir)
                    print "copying '%s' to '%s'" % (src_path, dest_path)
                    # copy file. already existing file will be replaced
                    shutil.copyfile(src_path, dest_path)
                    copied = True
                    continue
        if not copied:
            print >> sys.stderr, "could not find source for '%s'" % src_icon


def main():
    parser = OptionParser()
    parser.add_option("-f", "--file", dest="filename",
                      help="file with icon names", metavar="FILE")
    parser.add_option("--src", dest="src", help="souce directory",
                      metavar="DIRECTORY")
    parser.add_option("--dest", dest="dest", help="destination directory",
                      metavar="DIRECTORY")
    (options, args) = parser.parse_args()

    if not options.filename or not options.src or not options.dest:
        parser.print_help()
        sys.exit(0)

    src_dir = os.path.expanduser(options.src)
    dest_dir = os.path.expanduser(options.dest)
    filename = os.path.expanduser(options.filename)

    if not os.path.isdir(src_dir):
        print >> sys.stderr, "Source directory \"%s\"does not exist" % src_dir
        sys.exit(1)

    iconlist = load_mapping_list(filename)
    copy_icons(iconlist, src_dir, dest_dir)


if __name__ == "__main__":
        main()

