#!/usr/bin/perl -w
#
# Converts a bbkeys-0.8.6 .bbkeysrc file into the new syntax used by bbkeys-0.9.0
#
# Version 1.0 by Hannes Reich (hannesATskynet.ie), 2005-04-09.
# This code is in the public domain.
#
# Example of usage:
#        cp .bbkeysrc .bbkeysrc.old
#        convert_bbkeysrc < bbkeysrc.old > .bbkeysrc
#
# If you have an old-style .bbkeysrc file this script fails to
# process, mail it to me.
#

use strict;
use sigtrap;

# "old-style command name" => "new-style command name"
# Note: "workspace*" and "execcommand" are handled separately.
my %commandMap = (
		  "maximizewindow" => "toggleMaximizeFull",
		  "maximizevertical" => "toggleMaximizeVertical",
		  "maximizehorizontal" => "toggleMaximizeHorizontal",
		  "minimize" => "iconify",
		  "horizontaldecrement" => "resizeWindowWidth",
		  "horizontalincrement" => "resizeWindowWidth",
		  "verticaldecrement" => "resizeWindowHeight",
		  "verticalincrement" => "resizeWindowHeight",
		  "bignudgeup" => "moveWindowUp",
		  "bignudgedown" => "moveWindowDown",
		  "bignudgeleft" => "moveWindowLeft",
		  "bignudgeright" => "moveWindowRight",
		  "nextwindowallworkspaces" => "nextWindowOnAllWorkspaces",
		  "prevwindowallworkspaces" => "prevWindowOnAllWorkspaces",
		  "shadewindow" => "toggleShade",
		  "stickwindow" => "toggleOmnipresent",
		  "toggledecor" => "toggleDecorations",
);

# "old-style command name" => "options for new-style command"
my %optionMap = (
		 "horizontaldecrement" => "-1",
		 "horizontalincrement" => "1",
		 "verticaldecrement" => "-1",
		 "verticalincrement" => "1",
		 "bignudgeup" => 10,
		 "bignudgedown" => 10,
		 "bignudgeleft" => 10,
		 "bignudgeright" => 10,
		 "nudgeup" => 1,
		 "nudgedown" => 1,
		 "nudgeleft" => 1,
		 "nudgeright" => 1,
);



print <<ENDSTARTINGBOILERPLATE
[begin] (bbkeys configuration file)

  [config]
    # Add config options here - see bbkeysrc man page
  [end]

  [keybindings] (begin keybindings)
ENDSTARTINGBOILERPLATE
;

# Examples of config lines in the old file format:
# KeyToGrab(Tab), WithModifier(Shift+Mod1), WithAction(PrevWindow)
# KeyToGrab(F1), WithModifier(None), WithAction(ExecCommand), DoThis(exec xterm)
LINE:
while(<>)
  {
    my $inputLine = $_;

    ################################################################
    # Parse the old config file line

    # keep comments and blanks
    if($inputLine =~ /^\s*(#|$)/)
      {
	print $inputLine;
	next LINE;
      }

    # parse line
    my %inputLineData;
    for my $key ("KeyToGrab", "WithModifier", "WithAction", "DoThis")
      {
	$inputLineData{$key} = $1
	  if($inputLine =~ /$key\s*\(\s*([^\)]+?)\s*\)/);
      }

    # trap and report parse errors
    unless(exists $inputLineData{"KeyToGrab"} and exists $inputLineData{"WithAction"})
      {
	my $m = "$0: Failed to parse line: $inputLine";
        print "# $m";
        print STDERR $m;
	next LINE;
      }

    ################################################################
    # Generate a new config line
    my $command = lc($inputLineData{"WithAction"});
    my $options;

    $options = $optionMap{$command}
      if exists $optionMap{$command};
    $command = $commandMap{$command}
      if exists $commandMap{$command};

    ($command, $options) = ("changeWorkspace", $1)
      if $command =~ /^workspace(\d+)/;

    ($command, $options) = ("execute", $inputLineData{"DoThis"})
      if $command eq "execcommand";

    my $keySpec = $inputLineData{"KeyToGrab"};
    if(exists $inputLineData{"WithModifier"}
       and $inputLineData{"WithModifier"}
       and "none" ne lc($inputLineData{"WithModifier"})
      )
      {
	$keySpec = $inputLineData{"WithModifier"} . '-' . $keySpec;
	$keySpec =~ s/\+/-/g;
      }

    print "    [$command] ($keySpec)";
    print " {$options}" if $options;
    print "\n";
  }

print <<ENDFINALBOILERPLATE
  [end] (end keybindings)
[end] (end bbkeys configuration)
ENDFINALBOILERPLATE
;
