#!/bin/dash
# start-up script for xinit, launch window manager and panels
# Copyright (C) James Budiono 2012, 2015, 2017, 2019
# License: GNU GPL Version 3 or later
#
# Re-written for Fatdog64 600 from original $Xorg: xinitrc.cpp,v 1.3 2000/08/17 19:54:30 cpqbld Exp $
#
# $1 - window manager, $2 - window panel (both optional)

### redirect all std file descriptors so that they are not inherited and cause hangups later
#   closing them only works half of the time, it's better just to redirect to /dev/null
exec < /dev/null > /dev/null 2>&1	

### configuration
export EDITOR=geany	# override console settings
export TEXTDOMAIN=fatdog
[ "$UNDER_DUMB_INIT" ] || export XSESSION_ID=$(hexdump -e '"%08x"'  -n 4 /dev/urandom) # define our session

SYSTEM_XSETUP=/etc/X11/xsetup
USER_XSETUP=$FATDOG_STATE_DIR/xsetup

SYSTEM_SPLASH=/etc/X11/xinitrc-splash
USER_SPLASH=$FATDOG_STATE_DIR/xinitrc-splash

USER_RESOURCE=$HOME/.Xresources
USER_MODMAP=$HOME/.Xmodmap
SYSTEM_RESOURCE=/etc/X11/app-defaults/Xresources
SYSTEM_MODMAP=/etc/X11/app-defaults/Xmodmap

KEYMAP_CONFIG=$FATDOG_STATE_DIR/xkeymap	# set by fatdog-keyboard-wizard.sh
F_SESSION_PID=/tmp/xinitrc-session.pid.$USER.$XSESSION_ID # used by wmexit


######## main #########

### run it under dumb-init supervision
if [ -z "$UNDER_DUMB_INIT" ]; then
	echo $$ > $F_SESSION_PID
	# fork with dumb-init as supervisor
	export UNDER_DUMB_INIT=yes 
	exec dumb-init "$0" "$@"
fi
unset UNDER_DUMB_INIT

### xsetup (e.g. for NVIDIA Optimus)
[ -e $SYSTEM_XSETUP ] && [ -x $SYSTEM_XSETUP ] && $SYSTEM_XSETUP
[ -e $USER_XSETUP ] && [ -x $USER_XSETUP ] && $USER_XSETUP

### cleanup stuff
trap 'cleanup; exit;' 0 INT HUP TERM
cleanup() {
	: whatever is needed for cleanup
}

### screen mode / resolution setup
zarfy -l

### immediately something while rox/wm is loading
if type feh > /dev/null; then # use feh if we have it
	[ -e $USER_SPLASH ] && feh --bg-scale $USER_SPLASH || feh --bg-scale $SYSTEM_SPLASH
else # otherwise use retro fallback - use Fatdog64.jpg which doesn't look too bad
	#jpegtopnm < /usr/share/backgrounds/Fatdog64.jpg | pamditherbw | pamtopnm | pbmtoxbm > /tmp/x.xbm &&
	#xsetroot -bitmap /tmp/x.xbm; rm -f /tmp/x.xbm
	xsetroot -solid teal
fi

### setup Xresources, fonts, cursors
# merge in defaults and keymaps
[ -f $SYSTEM_RESOURCE ] && xrdb -merge $SYSTEM_RESOURCE
[ -f $SYSTEM_MODMAP ] && xmodmap $SYSTEM_MODMAP
[ -f $USER_RESOURCE ] && xrdb -merge $USER_RESOURCE
[ -f $USER_MODMAP ] && xmodmap $USER_MODMAP

# set keyboard layout - if needed
[ -e $KEYMAP_CONFIG ] && read p < $KEYMAP_CONFIG 2>/dev/null
if [ "$p" ]; then
	setxkbmap -option "" $p -synch
	setxkbmap -query # workaround for certain layout
fi

# fonts & default cursor
[ -f $HOME/.fonts/fonts.dir ] && xset fp+ $HOME/.fonts/
[ -f /usr/bin/xsetroot ] && xsetroot -cursor_name top_left_arrow

# apply settings from /usr/bin/pupx...
[ -f $HOME/.xset.sh ] &&  eval $HOME/.xset.sh

### start desktop
/etc/rc.d/rc.Xdesktop

### start window manager & panel last
/etc/rc.d/rc.Xstartwm "$@"

# don't die and stay as process supervisor
while :; do /bin/sleep 1000000; done
