#!/usr/bin/ruby
#
# kdecolor4to3.rb - version 1.0
#
# This script will make KDE3 apps obey changes made under KDE4's
# color settings. Run it after you have applied color settings.
# Note that you have to restart running KDE3-applications for
# the color settings to be applied to them.
#
# NOTE: You might have to change srcfilename and/or dstfilename
# below if your KDE3 and 4 directories are different. Under Kubuntu
# it's the same, but if your system has put KDE4-files under ~/.kde4
# then you need to change srcfilename. (This is untested though...)
#
# And by the way: Use at your own risk!
#
# By Tomas Åkesson, tomas at datasupporten . se
#
##################################################################

require 'ftools'

srcfilename = "#{ENV['HOME']}/.kde/share/config/kdeglobals"
dstfilename = "#{ENV['HOME']}/.kde/share/config/kdeglobals"

class Ini
  def initialize(srcfilename, dstfilename)
    @srcfilename = srcfilename
    @filename = dstfilename
    @tmp_filename = "#{@filename}.kc4t3new"
    @old_filename = "#{@filename}.kc4t3old"
    @cfg = Hash.new
  end

  # Read an ini-file into a hash
  def read
    open_section = nil

    File.open(@srcfilename).each do |line|
      line.strip!
      
      if line =~ /^\[/
        # Start a new section
        @cfg[line] = Hash.new
        open_section = line
      
      elsif not open_section.nil? and not line.empty?
        # A config section is open
        m = line.match /^([^=]+)=(.*)$/
        if m and m[1] and m[2]
          @cfg[open_section][m[1]] = m[2]
        end
      end
    end
  end

  # Convert KDE4 style color configuration to KDE3
  def convert4to3
    # Format of this map is: kde3-keyword => [kde4-section, kde4-keyword]
    # which means: take the value in [kde4-section, kde4-keyword] and
    # set kde3-keyword to this value.
    convmap = {
      'alternateBackground' => ['Colors:Window',    'BackgroundAlternate'],
      'background'          => ['Colors:Window',    'BackgroundNormal'],
      'buttonBackground'    => ['Colors:Button',    'BackgroundNormal'],
      'buttonForeground'    => ['Colors:Button',    'ForegroundNormal'],
      'foreground'          => ['Colors:Window',    'ForegroundNormal'],
      'linkColor'           => ['Colors:Window',    'ForegroundLink'],
      'selectBackground'    => ['Colors:Selection', 'BackgroundNormal'],
      'selectForeground'    => ['Colors:Selection', 'ForegroundNormal'],
      'visitedLinkColor'    => ['Colors:Window',    'ForegroundVisited'],
      'windowBackground'    => ['Colors:Window',    'BackgroundNormal'],
      'windowForeground'    => ['Colors:Window',    'ForegroundNormal']
    }

    convmap.each do |key_3, sec_and_key_4|
      @cfg['[General]'][key_3] =
        @cfg["[#{sec_and_key_4[0]}]"][sec_and_key_4[1]]
    end
  end


  # Write the hash as ini-style data to a temporary file
  def write
    File.open(@tmp_filename, 'w') do |file|
      @cfg.sort.each do |sec_name,section|
        file.puts sec_name
        section.sort.each do |key,value|
          file.puts "#{key}=#{value}"
        end
        file.puts ""
      end
    end

    # Backup the current config, and install the temporary file as current.
    # The @old_filename is left as a reference of what's been changed.
    File.move @filename, @old_filename
    File.move @tmp_filename, @filename
  end

end


# Main script
ini = Ini.new srcfilename, dstfilename
ini.read
ini.convert4to3
ini.write
