#!/bin/sh --
#
# sx - start an xserver
# requires Xorg

# ----------------------------------------------------------------------------
# d=0
# while [ -e "/tmp/.X${d}-lock" ] && [ -S "/tmp/.X11-unix/X${d}" ]; do
#   d=$((d + 1))
# done
# [ -n "$1" ] && c="$(command -v "$1")" && shift 1
# exec xinit "${c}" "$*" -- :"${d}" vt"${XDG_VTNR:-}"
# ----------------------------------------------------------------------------

cleanup() {
  if [ -n "${pid}" ] && kill -0 "${pid}" 2> /dev/null; then
    kill "${pid}"
    wait "${pid}"
    xorg=$?
  fi

  if ! stty "${stty}"; then
    stty sane
  fi

  if [ "$1" = exit ]; then
    exit "${xorg:-0}"
  fi
}

stty=$(stty -g)
tty=$(tty)
tty=${tty#/dev/tty}

datadir=${XDG_DATA_HOME:-${HOME%/}/.local/share}/sx

export XAUTHORITY="${XAUTHORITY:-${datadir}/xauthority}"
touch -- "${XAUTHORITY}"

trap 'cleanup; trap - INT; kill -INT "$$"' INT
trap 'cleanup exit' EXIT HUP TERM QUIT

# Xorg will check whether it inherited a USR1 with a disposition of SIG_IGN and
# use this state to reply back to the parent process with its own USR1.
# This is done to indicate that the server is ready to accept connections.
# Taking advantage of this feature allows launching the client directly from a
# USR1 signal trap which obviates the need to poll for server readiness.
trap 'DISPLAY=:$tty exec dwm & wait "$!"' USR1

# (trap '' USR1 && exec Xfbdev vt2 :"${tty}" -noreset -auth "${XAUTHORITY}") & pid=$!

(trap '' USR1 && exec Xorg :"${tty}" -keeptty vt"${tty}" -noreset -auth "${XAUTHORITY}") & pid=$!
wait "${pid}"
