#!/bin/sh

### Starts xemacs if there's no gnuserv running, and otherwise starts
### gnuclient. Always runs in background.

xeargs="--sm-restart-command $0"
unmapped=""
startserv="(progn (setq gnuserv-shutdown-hook 'save-buffers-kill-emacs) (gnuserv-start))"

case $1 in
"-sm-client-id"|"--sm-client-id")
	xeargs="$xeargs --sm-client-id $2"
	shift 2
esac
case $1 in
"-unmapped")
	unmapped="yes"
	shift 1
esac
if ! ( gnuclient -batch -eval t > /dev/null 2>&1 ) then
	if [ -z "$unmapped" ]; then
		xemacs $xeargs -unmapped -eval "$startserv" &
	else
		xemacs $xeargs -unmapped -eval "$startserv" "$@" &
	fi
	until gnuclient -batch -eval t > /dev/null 2>&1 ; do
		sleep 1
	done
fi
if [ -z "$unmapped" ] ; then
	exec gnuclient -q "$@"
fi

