#!/etc/bin/sh
#
# Xwin menu child, dless-Xwin toolkit, (C) 1997 Roman Dolejsi - roman@sorry.vse.cz
#

CNT=1
OUT=$1
TMP1=`echo $1 | cut -f2 -d:`
if [ "$TMP1" != "$1" ]; then
  CNT=`echo $TMP1 | cut -f1 -d\ `
  OUT=`echo $1 | cut -f1 -d:`
  TMP2=`echo $TMP1 | cut -f2- -d\ ` 
  if [ "$TMP2" != "$TMP1" ]; then
    OUT="$OUT `echo $TMP1 | cut -f2- -d\ `"
  fi
fi
if [ -z "$OUT" ]; then
  OUT=$DEFAULT
fi
while [ -f "/tmp/.tty$CNT" -a "$CNT" -lt "$MAX_TTY" ]; do
  CNT=`expr $CNT + 1`
done
if [ -f "/tmp/.xwinflag" ]; then
  dialog --backtitle "$L_COPY" --title "X-windows" --msgbox "$L_XWALR tty`cat /tmp/.xwinflag`." 5 46
else
  if [ "$CNT" = "$MAX_TTY" ]; then
    dialog --backtitle "$L_COPY" --title "X-windows" --msgbox "$L_TELERR" 5 46
  else
    CNT2=`expr $CNT + 1`
    while [ -f "/tmp/.tty$CNT2" -a "$CNT2" -lt "$MAX_TTY" ]; do
      CNT2=`expr $CNT2 + 1`
    done
    if [ "$CNT2" = "$MAX_TTY" ]; then
      dialog --backtitle "$L_COPY" --title "X-windows" --msgbox "$L_TELERR" 5 46
    else

      echo "$$ xlog" > /tmp/.tty$CNT		# allocate console for X messages
      echo "0 xdsk $OUT $2" > /tmp/.tty$CNT2	# allocate console for X desktop
      echo "$CNT2" > /tmp/.xwinflag
      export CNT CNT2
      doprog /dev/tty$CNT runX2 $OUT $2 vt$CNT2
      dialog --backtitle "$L_COPY" --title "X-windows" --msgbox "$L_XWRUN tty$CNT (log), tty$CNT2 (desktop)." 5 62
      if [ -f "/tmp/.tty$CNT" ]; then
        echo "0 xlog $OUT $2" > /tmp/.tty$CNT
      fi
    fi
  fi
fi