#!/bin/sh

# convertkeys for Fluxbox 
# by vlaad <vlaadbrain@operamail.com>
# Copyright (c) 2001 Henrik Kinnunen (fluxgen@linuxmail.org)
# Modified by Anton Denisov <avd@altlinux.ru>
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.
check_version()
{
fluxversion=`fluxbox -version | cut -d \  -f 2`;
        if [ $fluxversion = "0.1.5" ]; then
                        echo "You have version 0.1.5 installed on your system.";
                        echo "If you still want to convert your bbkeysrc file";
                        echo "try -fk to force key conversion";
                        echo "ie. convertkeys -fk ~/.bbkeysrc > yournewfile"
                        exit 0;
        fi
}
my_convert()
{
cat $file \
| sed s/KeyToGrab\(//g \
| sed s/\),\ WithModifier\(/\ /g \
| sed s/\),\ WithAction\(/\ :/g \
| sed s/\),\ DoThis\(/\ /g \
| sed s/\)/\ /g \
| awk -F: '
        {
        split($1,a," ");
                if(a[2] ~ /+/)
                        {
                                split(a[2],b,"+");
                                print b[1],b[2],a[3]a[1]" :"$2
                        }
                else
                print a[2],a[3]a[1]" :"$2
        }'
}
show_full_help()
{
echo "NAME"
echo "  convertkeys for Fluxbox"
echo "SYNOPSIS"
echo "  convertkeys [-h] [-fullhelp] [-fb] [-fk old_bbkeys_file] "
echo "  [-k old_bbkeys_file]"
echo "DISCRIPTION"
echo "  convertkeys will parse your old bbkeysrc file and "
echo "  convert it so that Fluxbox 0.1.5 can read it.  The"
echo "  output is printed to stdout."
echo "OPTIONS"
echo "  -h                                      "
echo "          show small help."
echo "  -fullhelp"
echo "          show this help.  Hopefully you can scroll up :)."
echo "  -k old_bbkeys_file"
echo "          You must specify the entire path to your old "
echo "          bbkeys file. ie /home/foo/.bbkeysfile."
echo "  -fb"
echo "          Use keys file specified in ~/.fluxbox/init for "
echo "          conversion."
echo "  -fk old_bbkeys_file"
echo "          This option forces the conversion of your keys file"
echo "SEE ALSO"
echo "  http://fluxbox.sourceforge.net/fbgrab.txt"
}
show_help()
{
                echo "Fluxbox keys converter"
                echo "examples:"
                echo "convertkeys -k ~/.bbkeysrc > newkeys"
                echo "convertkeys -fb > newkeys"
                echo "usage:"
                echo "convertkeys [-h] [-fullhelp] [-fb] [-fk ~/.bbkeysrc]"
                echo "            [-k ~/.bbkeysrc] > newkeys"
}
if [ "$1" = "" ]; then
                show_help
                check_version
                exit 0
fi
if [ "$1" = "-h" ]; then
                show_help
                exit 0
fi
if [ "$1" = "-fb" ]; then
                check_version
                file=`grep -r session.keyFile ~/.fluxbox/init| awk -F: '{print $2}'`
                my_convert
                exit 0
fi
if [ "$1" = "-k" ]; then
                check_version
                file=$2
                my_convert
                exit 0
fi
if [ "$1" = "-fk" ]; then
                file=$2
                my_convert
                exit 0
fi
if [ "$1" = "-fullhelp" ]; then
                show_full_help
                exit 0
fi


