#!/bin/sh
# Copyright @BABOLO (C)2000,2001,2002,2003
# All rights reserved.
# $XConsortium: startx.cpp,v 1.4 91/08/22 11:41:29 rws Exp $
# $XFree86: xc/programs/xinit/startx.cpp,v 3.0.8.4 1998/12/27 13:10:18 dawes Exp $
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#
# $Id: start3,v 1.1 2003/05/10 00:07:44 babolo Exp $
# @BABOLO V.M %%VERS%%  %%DATE%%

         bin=/bin
       mkdir=${bin}/mkdir

      usrbin=/usr/bin
          id=${usrbin}/id
         tty=${usrbin}/tty
        grep=${usrbin}/grep
    basename=${usrbin}/basename

 sysclientrc=/usr/X11R6/lib/X11/xinit/xinitrc
 sysserverrc=/usr/X11R6/lib/X11/xinit/xserverrc
     deskdir=${HOME}/.desk
userclientrc=${deskdir}/xinitrc
userserverrc=${deskdir}/xserverrc

if  [ `${id} -u` = 0 ]; then
    echo "X server with root privileges is not recommended"
    exit 1
fi
if  ! ${tty} | ${grep} -q '/ttyv' ; then
    echo `${basename} ${0}`": must be on a virtual console"
fi
if  [ ! -d ${deskdir} ]; then
    ${mkdir} -p ${deskdir}
fi

clientargs=""
serverargs=""
bpp=32

case "${1}" in
8)
    bpp=8
    shift
    ;;
15)
    bpp=15
    shift
    ;;
16)
    bpp=16
    shift
    ;;
24)
    bpp=24
    shift
    ;;
32)
    bpp=32
    shift
    ;;
esac

if  [ -f ${userclientrc} ]; then
    clientargs=${userclientrc}
else if [ -f ${sysclientrc} ]; then
    clientargs=${sysclientrc}
fi; fi

if  [ -f ${userserverrc} ]; then
    serverargs=${userserverrc}
else if [ -f ${sysserverrc} ]; then
    serverargs=${sysserverrc}
fi; fi

whoseargs="client"
listen_tcp="-nolisten tcp"
while [ -n "${1}" ]; do
    case "${1}" in
    -listen_tcp)
        listen_tcp=""
        ;;
    /''*|\.*)
        if  [ "${whoseargs}" = "client" ]; then
            if  [ -z "${clientargs}" ]; then
                clientargs="${1}"
            else
                clientargs="${clientargs} ${1}"
            fi
        else
            if  [ -z "${serverargs}" ]; then
                serverargs="${1}"
            else
                serverargs="${serverargs} ${1}"
            fi
        fi ;;
    --) whoseargs="server" ;;
    *)  if [ "${whoseargs}" = "client" ]; then
            clientargs="${clientargs} ${1}"
        else
            serverargs="${serverargs} ${1}"
        fi ;;
    esac
    shift
done

i=0

while [ -e /tmp/.X${i}-lock ]; do
    i=$((${i} + 1))
done

exec %%BINOVER%%/xinit ${clientargs} -- %%BINOVER%%/Xwrapper3 :${i} -bpp ${bpp} -dpi 100 ${serverargs} ${listen_tcp} > ${deskdir}/startx.log 2>&1
