19 February, 2008

Activating Debian menu in Xubuntu XFCE

Aufter installing crossover office and not finding its menu entries, I did this:
   update-menus
This will create/overwrite ~/.config/xfce4/desktop/menu.xml And then I added those lines in ~/.config/xfce4/desktop/menu.xml:
 <separator />
 <menu name="Debian" icon="debian">
  <include type="file" src="menudefs.hook"/>
 </menu>
 <separator />
It is however better to put the lines above in /etc/xdg/xfce4/desktop/menu.xml, so they are not overwritten after a call to update-menus.

Apache front-end for Archiva

To use apache httpd as a proxy/mirror for archiva, I made these modifications to httpd.conf:

<IfModule mod_proxy.c>
ProxyRequests Off

<Proxy /archiva>
    Order deny,allow
    Allow from all
</Proxy>

ProxyPass /8081/ http://myhost:8081/
ProxyPassReverse / http://myhost:8081/

RewriteEngine On
RewriteRule "^/archiva/(.*)" "http://myhost/8081/archiva/$1" [P]
RewriteRule "^/archiva" "http://myhost/8081/archiva/" [P]

</IfModule mod_proxy.c>

If SELinux is used, you might need to:
   /usr/sbin/togglesebool httpd_can_network_connect
to check the status of the relevant SE flags:
   sestatus -b | grep httpd
A less complicated variant:

<IfModule mod_proxy.c>
ProxyRequests Off
ProxyPreserveHost On

<Proxy /nexus>
    Order deny,allow
    Allow from all
</Proxy>

ProxyPass /nexus http://localhost:8081/nexus
ProxyPassReverse /nexus http://localhost:8081/nexus

<Location /nexus>
    Order allow,deny
    Allow from all      
</Location>

</IfModule>

11 February, 2008

GUI for svn diff

Instead of calling svn diff, I can call svndiff to get a graphical diff, by defining the alias (in my ~/.bashrc):
alias svndiff='svn diff --diff-cmd /home/kpe/diffwrapper'
This allows me to have a GUI diff, when I define diffwrapper like this:
#!/bin/sh

# Configure your favorite diff program here.
# Subversion provides the files as a 6-th and 7-th parameter.
meld "${6}" "${7}"

Tee Timer Bash Script

Loving Tee? Here a timer:
#! /bin/bash

beep -f 3000 -l 100

TIMEOUT=5
COUNTER=$TIMEOUT

echo "$(date +%H:%M:%S)  started"
while [ $COUNTER -gt 0 ]; do
  sleep 60

  COUNTER=$(($COUNTER-1))
  beep -f 7000 -l 100 -r $(($TIMEOUT-$COUNTER))

  echo "$(date +%H:%M:%S)  $COUNTER min left"
done
echo "$(date +%H:%M:%S)  done"

beep -f 1000 -l 10 -r 2; beep -f 3000 -l 1 -r 2;
beep -f 2000 -l 20 -r 2; beep -f 3000 -l 10 -r 3

while [ 1 -gt 0 ]; do 
  sleep 5

  beep -f 5000 -r 2 -l 100 
  beep -f 4000 -r 1 -l 50
  beep -f 11000 -r 2 -l 50
  beep -f 12000 -r 1 -l 100
done

Simple Line Counter with Bash

A simple bash based line counter could look like this:
#!/bin/bash

function count_lines() {
  local LINE_COUNT=0
  local FILE_COUNT=0
  for file in $(find -name $1); do
    local lines=$(wc -l $file | sed 's/\([0-9]\+\).*/\1/')
    LINE_COUNT=$(expr $LINE_COUNT + $lines)
    FILE_COUNT=$(expr $FILE_COUNT + 1)
  done
  echo "Counted [$LINE_COUNT] in [$FILE_COUNT] [$1] files"
}

count_lines $@

Starting Firefox as a different user

Trying to start firefox as a different user like this:
  sudo -u torred -H firefox
fails with:
Xlib: connection to ":0.0" refused by server
Xlib: No protocol specified


(firefox-bin:9132): Gtk-WARNING **: cannot open display:
This can be easily fixed by:
xhost +si:localuser:torred

Switching Keyboard Layouts in XFCE

Make sure to specify the XkbLayout and XkbOptions in the keyboard section of your /etc/X11/xorg.conf. Mine looks like this:
Section "InputDevice"
    Identifier     "Generic Keyboard"
    Driver         "kbd"
    Option         "CoreKeyboard"
    Option         "XkbRules" "xorg"
    Option         "XkbModel" "pc105"
    Option         "XkbLayout"     "us,de,bg"
    Option         "XKbOptions"    "grp:switch,grp:lwin_toggle"
EndSection

Next time you start X, you must be able to change the keyboard layout by pressing the left windows key (grp:lwin_toggle), or specify grp:alts_toggle if you prefer pressing both ALTs.
If it still don't work, execute setxkbmap like this (you can configure it in xfce4-autostart-editor, so it is started once for each X session):
   setxkbmap -option grp:switch,grp:lwin_toggle us,de,bg
This should work immediately - the left Windows key (grp:lwin_toggle) or both ALTs (grp:alts_toggle) to toggle between keyboard layouts.
Instead of grp:alts_toggle use one of those:
  alt_shift_toggle
  ctrl_shift_toggle
  ctrls_toggle
  rwin_toggle
  lwin_toggle

Switching Between JDK Versions

I use the init-java-env script described here to switch between different JDK versions. After the installation/unpacking of the different JDKs under a common parent folder (e.g. /use/local/java), the script bellow can be used to set JAVA_HOME and put JAVA_HOME/bin in the PATH. Let say you have installed JDK 1.5 and JDK 1.6 at /usr/local/java/jdk1.5.0_13 and /use/local/java/jdk1.6.0_03 respectively, than execute the script like this to switch between the JDK versions:
source /usr/local/java/init-java-env jdk1.6
source /usr/local/java/init-java-env jdk1.5

Here comes the script:
#!/bin/bash
#

function get_script_location() {
 if [[ "$0" == *"init-java-env" ]]; then
   echo 
   echo " *************************************"
   echo "   Should be called for sourcing only:"
   echo "          source $0"
   echo "   or"
   echo "          . $0"
   echo 
   exit 1
 fi

 local script_loc=$(history 1)
 if [ -z "$script_loc" ] ; then script_loc="${BASH_SOURCE[0]}" ; fi

 script_loc=${script_loc#*. }
 script_loc=${script_loc#*source }
 script_loc=${script_loc%% }
 script_loc=$(eval echo $script_loc)
 script_loc=$(readlink -f "$script_loc")
 script_loc=$(dirname "$script_loc")

 eval "$1=$script_loc"
}

function get_java_home {
 local script_dir=""
 local java_version=$2 
 get_script_location script_dir
 local _jh=$(find $script_dir -maxdepth 1 -name "*$java_version*"  -printf '%p')

 if [ -z "$_jh" ]; then
  echo "Cannot find Java $java_version under $script_dir"
 else 
  _jh=${_jh/\.\//$(pwd)/}
  eval "$1=$_jh"
 fi
}

function init_java_env {
 local java_version="jdk1.5"
 [ $# == 0 ] || java_version="$1"

 local java_home_in_path="`which java`"
 [ "$java_home_in_path" == "" ] || java_home_in_path="$(dirname $(dirname $java_home_in_path))"
 local java_home="$java_home_in_path"

 get_java_home java_home $java_version
 echo "Found Java at: $java_home"


 if [ "$java_home" == "$JAVA_HOME" ] && [ "$java_home" == "$java_home_in_path" ]; then
  echo "JAVA_HOME is already set to: $JAVA_HOME"
 else
  # remove JAVA_HOME/bin from PATH
  if [ -n "$JAVA_HOME" ]; then
    echo "removing $JAVA_HOME/bin from $PATH"
    export PATH=${PATH/$JAVA_HOME\/bin:/}
    export PATH=${PATH/$JAVA_HOME\/bin/}
  fi
  export JAVA_HOME="$java_home"

  export PATH=$JAVA_HOME/bin:$PATH
  echo "Using Java $JAVA_VERSION at JAVA_HOME: $JAVA_HOME"
 fi
}


init_java_env $@

Resolving the Host IP Address

To automatically update the local host's IP entry in /etc/hosts put this script under /etc/network/if-up.d:
#!/bin/bash

# Update /etc/host only if eth0 or eth1 changes
[ "$IFACE" != "eth0" ] || [ "$IFACE" != "eth1" ] ||  exit 0

HOSTNAME=$(< /etc/hostname)
IPADDRESS=$(ip -o -4 addr show dev $IFACE scope global primary \
   | sed -n 's/.*inet\ \([^\/]*\)\/.*/\1/p' \
   | sed -n 's/\([0-9]\{1,3\}\.\)\{1,3\}[0-9]\{1,3\}/\0/p')

if [ -n "$IPADDRESS" ]; then
  sed -i 's/\(^[1-9]\+\S\+\)\(\s\+\(\S+\s+\)*'$HOSTNAME'\(\S+\s*\)*\)/'$IPADDRESS'\2/g' /etc/hosts
fi

Make sure that your /etc/hosts contains an entry like:
10.1.208.31 myhost myhost.mydomain.org