#!/bin/bash

# $Id: home_autostart,v 1.8 2003/10/16 23:49:03 mackstann Exp $

##############
# Autostart file - this file is executed
# whenever Kahakai is started (or restarted). 

##############
# The launch function will check if the command
# you pass it is already running, and will only
# run the command if there is not another instance
# already running.  The command is run in the
# background so you don't need to use explicit "&"'s.
#
# Use the following syntax:
# launch program arguments
# e.g.:
# launch gkrellm2 -w

# NOTE that if the name of your program changes
# (check ps or top) after it starts, this will
# not work.  For example. mozilla-firebird turns
# into MozillaFirebird after it starts.  launch()
# has no way to know that.

launch () {

    # grr, this only works in bash now

    sed_regexp='\([^:]*\):\{1,2\}\([0-9]\+\)\(\.\([0-9]\+\)\)\?'
    sed_actions='b_valid_display
s/.*/:./
b_canonicalize
:_valid_display
s//\1:\2.\4/1
:_canonicalize
s/^:/localhost:/
s/:\./:0./
s/\.$/.0/
p
'

    eval "current_display='`echo "$DISPLAY" | sed -n "/^$sed_regexp\$/$sed_actions"`'"

    if pgrep -f "$*"'$' | (
        while read pid
        do
            eval "display='`ps eo 'cmd=' "$pid" | \
                 sed -n "/.*\<DISPLAY=$sed_regexp\>.*/$sed_actions"`'"
            test "$display" == "$current_display" && exit 1
        done
        true )
    then
        echo "launch(): launching \`$*' on display \`$current_display'"
        "$@" & disown $!
    else
        echo "launch(): \`$*' already running on display \`$current_display'"
    fi
}

#############################################################
# put your launch statements and other shell commands below #
#############################################################

# like that :
#
# launch gkrellm2
# launch xmms

# launch checks the full command line, so the
# following lines start 2 wmCalClocks :
#
# launch wmCalClock
# launch wmCalClock -arial

# you can also launch the same command
# on different displays, like this :
#
# DISPLAY=0:0 launch wmCalClock
# DISPLAY=0:1 launch wmCalClock

