#! /bin/bash

# -*- sh -*-

#---------------------------------------------------------------------
# This bash script tries to make a gtkrc for use with gimp
# based on the stuff in your kde configuration.
# Use at your own risk and will. :-)
#
# This is version 1.05
# Tools needed by this script: grep, awk, sed, expr and bc
#
# Changes from v1.0:
#
# * Updated for KDE Beta4, will probably break on older. :-(
# * Removed the XDefaults part, it wasn't all that useful
#
# (c)1998 Andreas Fredriksson (dep@canit.se)
#---------------------------------------------------------------------

#---------------------------------------------------------------------
# script-global variables

# the kde configuration file (not changed in any way)
KDERC=$HOME/.kderc

# the gimp/gtk configuration file (backuped to $GTKRC.old)
GTKRC=$HOME/.gimp/gtkrc

#---------------------------------------------------------------------
# function definitions

# check wether a file list exists or not, exit
checkfiles()
{
  while [ $1 ]
  do
    if [ \! -f $1 ];
    then
      echo "$0: can't find $1"
      [ -z $DISPLAY ] || sleep 3    # in case we're started from xinitrc
      exit 1                        # so xinitrc users see the error
    fi
  shift
  done
}

# one-char hexadecimal to integer
nibble()
{
  case $1 in
    A)
	echo '10';;
    a)
	echo '10';;
    B)
	echo '11';;
    b)
	echo '11';;
    C)
	echo '12';;
    c)
	echo '12';;
    D)
	echo '13';;
    d)
	echo '13';;
    E)
	echo '14';;
    e)
	echo '14';;
    F)
	echo '15';;
    f)
	echo '15';;
    *)
	echo $1;;
  esac;
}

# two-byte hexadecimal to decimal
hexdec()
{
  DIG1=`echo $1 | cut -c 1`
  DIG2=`echo $1 | cut -c 2`
  expr 16 \* `nibble $DIG1` + `nibble $DIG2` 
}

# convert a integer 0 - 255 to a float 0.0 - 1.0
decfloat()
{
  STR=`echo scale=5\; $1 / 255.0 | bc`

  # add a zero to make the buggy gtk rc parser happy
  if [ `echo $STR | cut -c 1` = '.' ];
    then STR=0$STR;
  fi

  # make sure the number includes a dot
  if [ "$STR" = "0" ]; 
    then STR=$STR.0;
  fi;

  if [ "$STR" = "1" ]; 
    then STR=$STR.0;
  fi;

  echo $STR
}

# convert a #-preceeded 6-character hex string into a gtk color triple
convert()
{
  DEC1=`echo $1 | cut -d , -f 1`
  DEC2=`echo $1 | cut -d , -f 2`
  DEC3=`echo $1 | cut -d , -f 3`

  echo \{ `decfloat $DEC1`, `decfloat $DEC2`, `decfloat $DEC3` \}
}

# get a label identifier from the .kderc file
getlabel()
{
  grep "^$1=" $KDERC | sed s/^.\*=//
}

#---------------------------------------------------------------------
# start of script

# we need the .kderc file
checkfiles $KDERC

# extract labels

# is this correct?
WEIGHT=`getlabel Weight`
if [ "$WEIGHT" = "75" ];
then
  WEIGHT=bold;
else
  WEIGHT=regular;
fi;

FONT="`getlabel font`"

FAMILY=`echo "$FONT" | cut --delimiter=, -f 1`
POINTSIZE=`echo "$FONT" | cut --delimiter=, -f 2`
WEIGHT=`echo "$FONT" | cut --delimiter=, -f 5`

if [ $WEIGHT = "50" ]; then WEIGHT="medium"; else WEIGHT="bold"; fi
FONT="\"-*-$FAMILY-$WEIGHT-r-normal-*-$POINTSIZE-*-*-*-*-*-*-*\""

BG=`getlabel background`
FG=`getlabel foreground`

if [ "x$BG" = "x" ] || [ "x$FG" = "x" ]
 then
  echo "$0: $KDERC: possibly old KDE version"
  echo "Giving up. Download KDE Beta4 from ftp.kde.org or it's mirrors."
  [ -z $DISPLAY ] || sleep 3    # sleep in xinitrc
  exit 1
fi

BG1=`convert $BG`
FG1=`convert $FG`

# output the gtkrc

echo "# Automatically generated by $0, do not edit" > $GTKRC
echo "" >> $GTKRC
echo "style \"default\"" >> $GTKRC
echo "{" >> $GTKRC
echo "  font = " $FONT >> $GTKRC
echo "  bg[NORMAL]   = $BG1" >> $GTKRC
echo "  bg[PRELIGHT] = $BG1" >> $GTKRC
echo "  bg[ACTIVE] = $BG1" >> $GTKRC
echo "  bg[INSENSITIVE] = $BG1" >> $GTKRC
echo "  bg[SELECTED] = $BG1" >> $GTKRC
echo "  fg[NORMAL] = $FG1" >> $GTKRC
echo "}" >> $GTKRC
echo "" >> $GTKRC
echo "widget_class \"*\" style \"default\"" >> $GTKRC
