#! /bin/sh
# @(#)openwin 23.50 92/10/23
#
# Copyright (c) 1994, by Sun Microsystems, Inc.
# All rights reserved.

# environment variables that this shell script sets/changes:
export DISPLAY HELPPATH LD_LIBRARY_PATH
export MANPATH NOSUNVIEW OPENWINHOME PATH XFILESEARCHPATH

# look in /usr/openwin if OPENWINHOME is not set.
OPENWINHOME="${OPENWINHOME-/usr/openwin}"

# error message function
err () {
	echo "`basename $0`: $@" 1>&2
	exit 1
}

# Help message function
HelpMessage() {
	echo "This script supports the following command line options"
	echo "-includedemo	if specified, adds $OPENWINHOME/bin/demo to your path"
	echo "-dev		used to specify the name of framebuffer device"
	echo "-display          used to specify the display name"
	echo "-auth             used to specify the authentication protocol"
	echo ":*                used to specify the display name"
	echo "-server           used to specify the name of server executable"
	echo "-noauth           if specified, sets host-based authentication"
	echo "-wm               used to specify the name of the window manager"
	echo "-help             this message"
	echo "Any other arguments are passed on to the server unchanged."
	echo "See openwin(1) man page for more details"
}

# don't bother to continue if X is not installed.
if [ ! -f $OPENWINHOME/bin/X ]; then
	err \
"OpenWindows is not installed correctly in $OPENWINHOME.
Set the environment variable \$OPENWINHOME to the directory
where OpenWindows is installed."
fi

## IF $OPENWINHOME is not set to /usr/openwin...
##	print error message and then exit
if [ $OPENWINHOME != "/usr/openwin" ]; then

##  IF /usr/openwin does not exist...
##	create link to $OPENWINHOME
    if [ ! -r /usr/openwin ]; then
	echo ""
	echo "	OpenWindows is not installed in the default location"
	echo "	of /usr/openwin.  As root, type the following command:"
	echo ""
	echo "		# ln -s $OPENWINHOME /usr/openwin"

##  ELSE /usr/openwin exists...
##	move it aside
##	create link to $OPENWINHOME
    else
	echo ""
	echo "	OpenWindows is not installed in the default location"
	echo "	of /usr/openwin.  As root, type the following commands:"
	echo ""
	echo "		# mv /usr/openwin /usr/openwin.save"
	echo "		# ln -s $OPENWINHOME /usr/openwin"
    fi

##  set $OPENWINHOME to /usr/openwin
    echo ""
    echo "	As yourself, set OPENWINHOME to /usr/openwin and restart"
    echo "	OpenWindows.  See the Solaris Getting Started guide for"
    echo "	more details."
    echo ""
    exit 1
fi

# default DISPLAY is :0
DISPLAY=":0"


# initial arglist is null
PASSTHRU=

NOAUTH=0
SERVERHELP=0
INCLUDEDEMOPATH=0
NOSUNVIEW=0
AUTH_PROTOCOL="magic-cookie"

# Parse command line args.
while [ $# -gt 0 ]; do
    case "$1" in
	-includedemo)
	    INCLUDEDEMOPATH=1
	    ;;
	-dev)
	    if [ $# -lt 2 ]; then
		err "$1 option requires a framebuffer device name"
	    fi
	    PASSTHRU="$PASSTHRU $1 $2"
	    shift
	    ;;
	-display)
	    if [ $# -lt 2 ]; then
		err "$1 option requires a display name"
	    fi
	    shift
	    DISPLAY="$1"
	    ;;
	-auth)
		if [ $# -lt 2 ]; then
			err "$1 option requires a protocol name"
		fi
		shift
		AUTH_PROTOCOL="$1"
		;;
	:*)
	    DISPLAY="$1"
	    ;;
	-server)
	    if [ $# -lt 2 ]; then
		err "$1 option requires a server binary name"
	    fi
	    shift
	    SERVER="$1"
	    case "$SERVER" in
	    /*) ;;
	    *) SERVER=$OPENWINHOME/bin/$SERVER ;;
	    esac
	    ;;
	-noauth)
	    NOAUTH=1
	    AUTH=
	    ;;
	-wm)
	    if [ $# -lt 2 ]; then
		err "$1 option requires a window manager command"
	    fi
	    shift
	    OW_WINDOW_MANAGER="$1"
	    export OW_WINDOW_MANAGER
	    ;;
	-help)
	    HelpMessage
	    SERVERHELP=1
	    ;;
	*)
	    PASSTHRU="$PASSTHRU $1"
	    ;;
    esac
    shift
done

# complain if $DISPLAY doesn't begin with a colon.
case "$DISPLAY" in
    :*)
	;;
    *)
	;;
esac

# Add to the path variable named by $1 the component $2.  $3 must be
# "append" or "prepend" to indicate where the component is added.
addpath () {
    eval value=\"\$$1\"
    case "$value" in
	*:$2:*|*:$2|$2:*|$2)
	    result="$value"
	    ;;
	"")
	    result="$2"
	    ;;
	*)
	    case "$3" in
		p*)
		    result="$2:${value}"
		    ;;
		*)
		    result="${value}:$2"
		    ;;
	    esac
    esac
    eval $1=$result
    unset result value
}

# convenience routine which appends a string to a path.
append () {
    addpath "$1" "$2" append
}

# convenience routine which prepends a string to a path.
prepend () {
    addpath "$1" "$2" prepend
}

# add $OPENWIN/mumble to several related environment variables.
[ $INCLUDEDEMOPATH -eq 1 ] && prepend PATH $OPENWINHOME/demo

append	HELPPATH	$OPENWINHOME/lib/locale
append	HELPPATH	$OPENWINHOME/lib/help
prepend XFILESEARCHPATH	$OPENWINHOME/lib/locale/%L/%T/%N%S
append	XFILESEARCHPATH	$OPENWINHOME/lib/%T/%N%S
prepend PATH		$OPENWINHOME/bin

if [ -d $OPENWINHOME/share/man ]; then
    case "$MANPATH" in
    "") MANPATH="$OPENWINHOME/share/man:/usr/man" ;;
    *)  prepend MANPATH $OPENWINHOME/share/man ;;
    esac
fi

# Add locale-specific support
#
if [ -f $OPENWINHOME/bin/set_locale_env ]; then
    . $OPENWINHOME/bin/set_locale_env
fi

# User asked for help
#
if [ $SERVERHELP -eq 1 ]; then
	echo
	echo "The X server supports the following command line options"
	echo
	${SERVER-$OPENWINHOME/bin/X} -help
	exit 0
fi

# If the user doesn't have a .xinitrc and they haven't specified an alternate
# one via XINITRC, then set XINITRC to the system default Xinitrc
if [ ! -f $HOME/.xinitrc -a -z "${XINITRC}" ]; then
	XINITRC=$OPENWINHOME/lib/Xinitrc
	export XINITRC
fi

#
# Check to see if OpenWindows is already running on requested DISPLAY
#
$OPENWINHOME/lib/checkOW $DISPLAY
retval=$?
if [ $retval -ne 1 ]; then
	err "There is already a server running on $DISPLAY"
fi

#
# Create magic cookie for authentication, and write server and client files.
#
if [ $NOAUTH -eq 0 ]; then
	if [ ! \( -d $HOME -a -w $HOME \) ]; then
		err \
"Home directory is not writeable; Cannot write authority file.
Perhaps you should start openwin with -noauth."
	fi
	AUTHFNAME=$HOME/.xsun.`uname -n`$DISPLAY
	$OPENWINHOME/lib/mkcookie $AUTHFNAME -auth $AUTH_PROTOCOL
	AUTH="-auth $AUTHFNAME"
fi

# Get rid of our function definitions because a bug in some versions of
# the SunOS sh will turn their names into environment variables otherwise.
unset err addpath append prepend

# start up xinit and thus the server.
$OPENWINHOME/bin/xinit -- ${SERVER-$OPENWINHOME/bin/X} $DISPLAY $PASSTHRU $AUTH 

if [ "" != "$AUTHFNAME" -a -f "$AUTHFNAME" ]
then
	rm -f $AUTHFNAME
fi

exit 0
