#! /bin/sh
# @(#)openwin 23.25 90/06/19

# environment variables that this shell script sets/changes:
export DISPLAY FONTPATH FRAMEBUFFER HELPPATH LD_LIBRARY_PATH
export MANPATH NEWSSERVER OPENWINHOME PATH XAPPLRESDIR

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

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

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

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

# default FRAMEBUFFER is /dev/fb if not in the environment.
FRAMEBUFFER=${FRAMEBUFFER-"/dev/fb"}

# initial arglist is null
PASSTHRU=

NOAUTH=0

# Parse command line args.
while [ $# -gt 0 ]; do
    case "$1" in
	-dev)
	    if [ $# -lt 2 ]; then
		err "$1 option requires a framebuffer device name"
	    fi
	    FRAMEBUFFER=$2
	    PASSTHRU="$PASSTHRU $1 $2"
	    shift
	    ;;
	-display)
	    if [ $# -lt 2 ]; then
		err "$1 option requires a display name"
	    fi
	    shift
	    DISPLAY="$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=
	    ;;
	*)
	    PASSTHRU="$PASSTHRU $1"
	    ;;
    esac
    shift
done

# complain if $DISPLAY doesn't begin with a colon.
case "$DISPLAY" in
    :*)
	;;
    *)
	err "\"$DISPLAY\" is an illegal value for \$DISPLAY"
	;;
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
}

# Derive the $NEWSSERVER variable from the digit in $DISPLAY
newsport=`expr "$DISPLAY" : '.*:\([0-9][0-9]*\)' + 2000`
NEWSSERVER=`$OPENWINHOME/bin/newsserverstr $newsport`

# add $OPENWIN/mumble to several related environment variables.
prepend LD_LIBRARY_PATH		$OPENWINHOME/lib
prepend PATH			$OPENWINHOME/bin/xview
prepend PATH			$OPENWINHOME/bin
append	FONTPATH		$OPENWINHOME/lib/fonts
append	HELPPATH		$OPENWINHOME/lib/help

case "$MANPATH" in
"") MANPATH="$OPENWINHOME/share/man:/usr/man" ;;
*)  prepend MANPATH $OPENWINHOME/share/man ;;
esac

XAPPLRESDIR=$OPENWINHOME/lib/X11/app-defaults

# Create a .xinitrc for the user if they don't have one already.
if [ ! -f $HOME/.xinitrc ]; then
	if [ -d $HOME -a -w $HOME ]; then
		/bin/cp $OPENWINHOME/lib/Xinitrc $HOME/.xinitrc
		if [ ! -f $HOME/.xinitrc ] ; then
			err "Could not create $HOME/.xinitrc"
		fi
	else
		XINITRC=$OPENWINHOME/lib/Xinitrc
		export XINITRC
	fi
fi

# Create a .Xdefaults for the user if they don't have one already.
if [ ! -f $HOME/.Xdefaults ]; then
	if [ -d $HOME -a -w $HOME ]; then
		/bin/cp $OPENWINHOME/lib/Xdefaults $HOME/.Xdefaults
		if [ ! -f $HOME/.Xdefaults ]; then
			err "Could not create $HOME/.Xdefaults"
		fi
	fi
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/.xnews.`hostname`$DISPLAY
	$OPENWINHOME/lib/mkcookie $AUTHFNAME
	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

# confirm framebuffer configuration
if [ -x $OPENWINHOME/bin/xn_ck_fbs ]; then
    $OPENWINHOME/bin/xn_ck_fbs $PASSTHRU
fi

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

rm -f $AUTHFNAME

exit 0
