#!/usr/bin/env ruby

# echo-new-icon: Script for echo-icon-theme artists to create new icon from 
# template
#
# Copyright (C) 2008, Martin Sourada
# 
# This script is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
# 
# This script 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
# Lesser General Public License for more details.
# 
# You should have received a copy of the GNU Lesser General Public
# License along with this script; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

require "rexml/document"
require "ftools"
include REXML

contexts = ['actions', 'animations', 'apps', 'categories', 'devices', 'emblems', 'emotes', 'intl', 'mimetypes', 'places', 'status']

template = File.expand_path "~/Templates/echo-one-canvas-template.svg"

if !File.exist?(template)
  template = "/usr/share/echo-artist/echo-one-canvas-template.svg"
  if !File.exist?(template)
    raise "Could not locate echo-one-canvas-template.svg in standard locations."
  end
end

icon = "#{ARGV[0]}"
context = "#{ARGV[1]}"
file = "#{icon}.svg"

if icon == ""
  raise "You must set icon-name.\n Usage: echo-new-icon <icon-name> <context>"
end

if context == ""
  raise "You must set icon context.\n Usage: echo-new-icon <icon-name> <context>"
end

if File.exist?(file)
  raise "Selected icon (#{file}) already exists. \nPlease set another icon name."
end

if !(contexts.include?(context))
  out = "Context must be one of: "
  contexts.each {|el| out = out + "#{el}, "}
  out = (out + "\n").gsub(", \n", ".\n")
  raise out
end

svg = Document.new(File.new("#{template}", 'r'))

element = svg.root.elements["metadata/rdf:RDF/cc:Work"]

element.elements.delete_all("dc:title")
element.elements.delete_all("dc:description")

title = Element.new "dc:title"
title.add_text "#{icon}"
element.add_element title

description = Element.new "dc:description"
description.add_text "#{context}"
element.add_element description

f = File.open(file,"w")
svg.write(f, -1, false)
f.close

puts "New icon #{file} created."

