#!/bin/bash
##########################
#
# gtk2jwm
# this script integrates
# the default jwm theme 
# with the current gtk 
# theme.
#
# author: thoughtjourney
# date: 28/08/2005
#
# Hacked BK Feb 2006 for JWM 1.4
# Abused by Dougal, Dec 2006
#########################
## Latest update: July 14th: add gradient support for JWM
# BK jan 2008: integrated into jwmconfig2.


##-------------variables---------------->>
		
GTKTHEME=`fgrep 'include' /root/.gtkrc-2.0 | fgrep '/usr/share/themes/' | grep -v '^#' | fgrep -m1 'gtkrc' | tr -d "'" | tr -d '"' | sed 's/include //' | tr -d '\t' | tr -d ' '`
[ ! -e $GTKTHEME ] && exit 1 

JWMTHEME=/root/.jwm/jwmrc-theme

MENU_BG=
ACTIVE_BG=
FOREGROUND="black"

DEFAULT=
FULL_COLOUR=255

##------get relevant gtk colours-------->>
MENU_BG=`grep -v "^#" $GTKTHEME | grep -m1 '\Wbg\[NORMAL\]' | cut -d'"' -f2`
ACTIVE_BG=`grep -v "^#" $GTKTHEME | grep -m1 '\Wbg\[SELECTED\]' | cut -d'"' -f2`
FOREGROUND=`grep -v "^#" $GTKTHEME | grep -m1 '\Wfg\[NORMAL\]' | cut -d'"' -f2`
PAGER_BG=`grep -v "^#" $GTKTHEME | grep -m1 '\Wbg\[ACTIVE\]' | cut -d'"' -f2`
FG_SELECTED=`grep -v "^#" $GTKTHEME | grep -m1 '\Wfg\[SELECTED\]' | cut -d'"' -f2`

#save them...
echo '#This is written to by /usr/local/jwmconfig2/gtk2jwm script' > /root/.jwm/jwm_colors
echo "MENU_BG='${MENU_BG}'" >> /root/.jwm/jwm_colors
echo "ACTIVE_BG='${ACTIVE_BG}'" >> /root/.jwm/jwm_colors
echo "FOREGROUND='${FOREGROUND}'" >> /root/.jwm/jwm_colors
echo "PAGER_BG='${PAGER_BG}'" >> /root/.jwm/jwm_colors
echo "FG_SELECTED='${FG_SELECTED}'" >> /root/.jwm/jwm_colors

rm -f /root/.tcl-gtk 
if [ -z "$MENU_BG" ]; then
 echo > "$JWMTHEME"
 pidof jwm >/dev/null && jwm -restart
 exit 2
else 
 # set variables for tcl/tk apps:
 ## need to make sure they're not blank
 [ "$MENU_BG" ] && echo  "bg_normal=$MENU_BG" >>/root/.tcl-gtk
 [ "$ACTIVE_BG" ] && echo  "bg_selected=$ACTIVE_BG" >>/root/.tcl-gtk
 [ "$PAGER_BG" ] && echo  "bg_active=$PAGER_BG" >>/root/.tcl-gtk
 [ "$FOREGROUND" ] && echo  "fg_normal=$FOREGROUND" >>/root/.tcl-gtk
 [ "$FG_SELECTED" ] && echo  "fg_selected=$FG_SELECTED" >>/root/.tcl-gtk
 
  # create gradient colours:
  DIFF="16#20"
  for COLOUR in ACTIVE_BG MENU_BG
  do
    NAME=${COLOUR:0:3}
    eval RR=\${$COLOUR:1:2}
    eval GG=\${$COLOUR:3:2}
    eval BB=\${$COLOUR:5:2}

    for ONE in RR GG BB
    do
      eval A=\$$ONE  
      let MORE="16#$A"+$DIFF
      let LESS="16#$A"-$DIFF

      if [ $MORE -gt 255 ] ; then # limit to max
        eval ${ONE}_LOWER="bf"
        eval ${ONE}_UPPER="ff"
      elif [ $LESS -lt 0 ] ; then
        eval ${ONE}_LOWER="00"
        eval ${ONE}_UPPER="40"
      else
        TMP_LOWER="`echo "$LESS 16 o p" |dc`"
        [ ${#TMP_LOWER} -eq 1 ] && TMP_LOWER="0$TMP_LOWER"
        eval ${ONE}_LOWER=$TMP_LOWER
        TMP_UPPER="`echo "$MORE 16 o p" |dc`"
        [ ${#TMP_UPPER} -eq 1 ] && TMP_UPPER="0$TMP_UPPER"
        eval ${ONE}_UPPER=$TMP_UPPER
      fi
    done
  #echo "#$RR_LOWER$GG_LOWER$BB_LOWER:#$RR_UPPER$GG_UPPER$BB_UPPER"
    eval ${NAME}_GRAD="#$RR_LOWER$GG_LOWER$BB_LOWER:#$RR_UPPER$GG_UPPER$BB_UPPER"
    eval ${NAME}_REVERSED="#$RR_UPPER$GG_UPPER$BB_UPPER:#$RR_LOWER$GG_LOWER$BB_LOWER"
  done
  
  DEFAULT="
<JWM>

<WindowStyle>
	<Active>
		<Text>$FG_SELECTED</Text>
		<Title>$ACT_GRAD</Title>
		<Corner>white</Corner>
       	<Outline>black</Outline>
	</Active>
	<Inactive>
		<Text>$FOREGROUND</Text>
		<Title>$MEN_GRAD</Title>
		<Corner>#aaaaaa</Corner>
       	<Outline>black</Outline>
	</Inactive>
</WindowStyle>

<TaskListStyle>
	<Foreground>$FOREGROUND</Foreground>
	<Background>$MEN_REVERSED</Background>
	<ActiveForeground>$FG_SELECTED</ActiveForeground>
	<ActiveBackground>$ACT_GRAD</ActiveBackground>
</TaskListStyle>

<TrayStyle>
	<Foreground>$FOREGROUND</Foreground>
	<Background>$MENU_BG</Background>
</TrayStyle>

<PagerStyle>
	<Outline>black</Outline>
	<!-- non-active programs: bg[NORMAL]-->
	<Foreground>$MENU_BG</Foreground>
	<!-- non-active desktops: bg[ACTIVE]-->
	<Background>$PAGER_BG</Background>
	<!-- selected app/desktop bg[SELECTED]-->
	<ActiveForeground>$FG_SELECTED</ActiveForeground>
	<!-- frame of selected desktop...bg[SELECTED] -->
	<ActiveBackground>$ACTIVE_BG</ActiveBackground>
</PagerStyle>


<MenuStyle>
	<Foreground>$FOREGROUND</Foreground>
	<Background>$MENU_BG</Background>
	<ActiveForeground>$FG_SELECTED</ActiveForeground>
	<ActiveBackground>$ACT_REVERSED</ActiveBackground>
</MenuStyle>

</JWM>"

fi

echo "$DEFAULT" > "$JWMTHEME"

#########
#John Doe created code for the applet backgrounds, old jwmconfig, port here...
TrayFile="/root/.jwmrc-tray"
TrayFileBak="/root/.jwmrc-tray.bak"

BackgroundColor="$MENU_BG" #"#ffc100"

function GetLineIndex
{
	lineVal=`fgrep -in "$1" $TrayFile | awk -F":" '{print $1}'`
	#return `expr $lineVal`
	return $lineVal
}

cp -f $TrayFile $TrayFileBak

GetLineIndex "blinkydelayed"
linenumber=$?
echo $linenumber

GetLineIndex "xload -nolabel"
linenumber2=$?
echo $linenumber2

lineNumber=`expr $linenumber`
lineNumber2=`expr $linenumber2`

blinkyline="			blinkydelayed -bg \"$BackgroundColor\""
xloadline="			xload -nolabel -fg red -hl white -bg \"$BackgroundColor\""

sed -e "$lineNumber"i"$blinkyline" -e "$lineNumber"d -e "$lineNumber2"i"$xloadline" -e "$lineNumber2"d $TrayFileBak > $TrayFile
#end John Doe's code.
########

sync
pidof jwm >/dev/null && jwm -restart
