#! /bin/sh
#
# /etc/init.d/rx
#
# Copyright (c) 1997-2000 SuSE GmbH Nuernberg, Germany.   
# Copyright (c) 2005,2007 SuSE LINUX Products GmbH Nuernberg, Germany.   
# Copyright (c) 2017 SUSE SUSE Linux GmbH, Nuremberg, Germany.
#
# Author:  Werner Fink        <werner@suse.de>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by 
# the Free Software Foundation; either version 2 of the License, or 
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, 
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
#
# Description:  This Skript should be started out of /etc/inittab
#               with a argument: tty<#num> with <#num>= 1, 2, ..., 12
#               This argument shows on which virtual terminal
#               line of the console the X server will be started.
#               Note: This line should correspond with the first
#                     number of the /etc/inittab entry!
#
# please send bugfixes or comments to http://www.suse.de/feedback
#
# Example Entry in /etc/inittab:
#
#  8:2:respawn:+/etc/init.d/rx tty8
#
# Options to set in /etc/sysconfig/xdmsc:
#
#   START_RX   "yes" if you want to start this service
#   RX_XDMCP   xdm control protocol: "query" or "indirect" or "broadcast"
#   RX_RHOST   xdm host, necessary for RX_XDMCP equal "query" or "indirect"
#   RX_DSP     optional DISPLAY number, e.g. ":1" or ":2" (don't forget `:')
#   RX_BPP     optional color depth of local X server
#   RX_CLASS   optional class name for naming the resource class name
#              in remote xdm-config. Always given:
#              hostname combined with display number dots and colon
#              replaced by underscores.
#

if test -x /usr/bin/Xorg ; then
: ${RX_SERVER=/usr/bin/Xorg}
else
: ${RX_SERVER=/usr/bin/X}
fi
: ${quiet=-quiet}
typeset -i version

function do_version ()
{
    local -i a=$1 b=$2 c=$3
    echo $(((a<<16)+(b<<8)+c))
}

xorg=no
function get_version () 
{ 

    local l version="$(${RX_SERVER} -version 2>&1)"
    OIFS="$IFS" ; IFS=$'\n'
    for l in $version ; do
	case "$l" in
	    X.Org\ X\ Server*)
		xorg=yes
		break ;;
	    X\ Window\ System\ Version*)
		break ;;
	    XFree86\ Version*)
		break ;;
	esac
	l=""
    done
    IFS="$OIFS"

    set -- $l
    eval version=\${$#}
    if test $xorg = yes ; then
	OIFS="$IFS" ; IFS=.
	set -- $version
	IFS="$OIFS"
	l=$(do_version $1 $2 $3)
    else
	l=${version%%.*}
    fi
    case ${l} in
	[0-9]*) echo ${l} ;;
	*)      echo 0    ;;
    esac
}

XCONFIG=""
if test -x $RX_SERVER ; then
    version=$(get_version)
    if test $version -gt 3 ; then
	if test -e /etc/X11/xorg.conf ; then
	    XCONFIG=/etc/X11/xorg.conf
	else
	    XCONFIG=/etc/X11/XF86Config
	fi
	DEPTHOPT=-depth
    else
	if test -e /etc/X11/xorg.conf -a ! -e /etc/XF86Config ; then
	    XCONFIG=/etc/X11/xorg.conf
	else
	    XCONFIG=/etc/XF86Config
	fi
	DEPTHOPT=-bpp
    fi
else
    echo "Error $0: No X server found, sleeping for next 10 minutes."
    sleep 600
fi

if test -n "$XCONFIG" -a -r "$XCONFIG" ; then
    . /etc/sysconfig/xdmsc
    if test "$START_RX" != "yes" -a "$PPID" = "1" ; then
	echo "Error $0: Starting X for a X terminal is disabled in /etc/sysconfig/xdmsc,"
	echo "          please disable the start line in /etc/inittab and run"
	echo "          the command   /sbin/init q"
	echo "      $0: Sleeping for next 10 minutes."
	sleep 600
	exit 0
    fi
elif test $version -ge $(do_version 1 6 5) ; then
    . /etc/sysconfig/xdmsc
    if test "$START_RX" != "yes" -a "$PPID" = "1" ; then
	echo "Error $0: Starting X for a X terminal is disabled in /etc/sysconfig/xdmsc,"
	echo "          please disable the start line in /etc/inittab and run"
	echo "          the command   /sbin/init q"
	echo "      $0: Sleeping for next 10 minutes."
	sleep 600
	exit 0
    fi
else
    if test "$PPID" = "1" ; then
	echo "Error $0: No X server found or no X configured,"
	echo "          sleeping for next 10 minutes."
	sleep 600
    else
	echo "Error $0: No X server found or no X configured."
    fi
	exit 0
fi

case "$1" in
    tty[0-9]|tty[0-9][0-9])

	tty="$1" ; shift
	vtline="vt${tty#tty}"

	options="-to 15 -once $quiet"

	test -z "$RX_DSP"   && RX_DSP=":0"
	options="$RX_DSP $options"

	test -z "$RX_LOG"   && RX_LOG="/var/log/X${RX_DSP}msgs"

	test -n "$RX_BPP"   && options="$options $DEPTHOPT $RX_BPP"

	case "$RX_XDMCP" in
	    query)
		if test -n "$RX_RHOST" -a "$RX_RHOST" != "YAST_ASK" ; then
		    options="$options -query $RX_RHOST"
		else
		    echo "Error $0: No remote xdm host given."
		    exit 0
		fi
		;;
	    indirect)
		if test -n "$RX_RHOST" -a "$RX_RHOST" != "YAST_ASK" ; then
		    options="$options -indirect $RX_RHOST"
		else
		    echo "Error $0: No remote xdm host given."
		    exit 0
		fi
		;;
	    broadcast)
		options="$options -broadcast"
		;;
	    *)
		echo "Error $0: Wrong X Display Manager Control Protocol"
		exit 0
	esac

	# Add class after -query or -indirect
	test -z "$RX_CLASS" && RX_CLASS="SUSE-X-Terminal"
	options="$options -class $RX_CLASS"

	options="$options $tty $vtline"

	if test -x ${RX_SERVER} ; then
	    echo "Starting remote X display login."
	    exec >> $RX_LOG 2>&1
	    exec ${RX_SERVER} $options
	else
	    echo "Error $0: No X server found"
	fi
	;;
    *)
	echo "Usage: $0 tty<#num>"
	echo "       with <#num> = 1, ..., 12"
	exit 1
esac

exit 0
