#!/bin/bash -
# startscwm
# (C) 1999 Greg J. Badros

# Starts scwm
#
# Reruns startscwm as a child of ssh-agent[12] process if they are in
# the path and this process is not already a child of ssh-agent[12]
#
# Starts an X server if $DISPLAY is not already set, so you can
# either run this from a GNU/Linux console or run it from Xsession or .xsession
#
# Uses .Xresources-cpp if it exists.
# Honors the SCWMDIR environment variable if it is set.
# or uses $HOME/scwm
#
# Writes errors to $HOME/.SCWM-errors, and starts an xterm
# watch those messages
#
# Starts an xterm instead of exiting if the started
# scwm exits with a non-zero (failing) exit status
#
# Depends on the cpp-sed-nl script, in this same directory.

ERRORSFILE=$HOME/.SCWM-errors; export ERRORSFILE
: ${SCWMDIR:=$HOME/scwm-source}
MODULEPATH=/usr/X11/lib/X11/fvwm2:$HOME/lib/fvwm2/modules/$UNAME:/uns/lib/fvwm2; export MODULEPATH
: ${XTERM_PROGRAM_NAME:=xterm}; export XTERM_PROGRAM_NAME


#test -r /etc/profile && . /etc/profile

if [ x"$DISPLAY" = x ]; then
    # we need to start an Xserver
    mcookie=`mcookie`
    serverargs="-auth $HOME/.Xauthority"
    : ${display:=:0}  # let "display=:1 startscwm" work
    xauth add $display . $mcookie
    xauth add `hostname -f`$display . $mcookie
    exec xinit $0 "$@" -- $display $serverargs
fi

# Now we know an X server is running and DISPLAY is set.

# Restart as a child of ssh-agent[12] if we are not already
if which ssh-add >/dev/null && \
    { which ssh-agent1 >/dev/null || which ssh-agent2 >/dev/null; } && \
    ! ssh-add -l >/dev/null; then
  # Check if we have secure shell capabilities
  # string them together if necessary!
  SSA=""
  which ssh-agent2 >/dev/null && SSA="ssh-agent2 $SSA"
  which ssh-agent1 >/dev/null && SSA="ssh-agent1 $SSA"
  exec $SSA $0 "$@" > $ERRORSFILE 2>&1
else
  exec > $ERRORSFILE 2>&1
fi


DISPLAYHOST=${DISPLAY%%:*}
if [ ! x = x"$DISPLAYHOST" ]; then
  xhost -$DISPLAYHOST
  FULLDISPLAY=$DISPLAY
else
  DISPLAYHOST=$HOST
  FULLDISPLAY=$HOST$DISPLAY
fi

export DISPLAY FULLDISPLAY DISPLAYHOST

echo $FULLDISPLAY > $HOME/.Xdisplay

WM=$SCWMDIR/src/scwm;  export WM
if [ ! -x $WM ]; then
  WM=scwm
else
  # use the development scheme sources/module libs since we're using the dev binary
  SCWM_LOAD_PATH=$SCWMDIR:$SCWMDIR/modules/lib; export SCWM_LOAD_PATH
fi


# This makes it easy to know what process to kill
# to end the x server
XSESNPID="$$"; export XSESNPID

# Add .0 if it's not already there-- this seems to be
# necessary for some Xservers at uw cse
echo $DISPLAY | grep -q \:.\.. || DISPLAY=$DISPLAY.0
if [ "$DISPLAY"x = ""x ]; then DISPLAY=:0.0; echo "Warning DISPLAY was empty!! Set to :0.0"; fi

xmodmap -pke > $HOME/.xmodmap-pke-${DISPLAY}

env > $HOME/.Xrootenv-${DISPLAY}

# This window will get iconified when gjb.scwmrc starts up
# Uses the title of the window, so be sure to change gjb.scwmrc
# if the title here changes.
$XTERM_PROGRAM_NAME -geometry 100x40 -name "scwmmessages" -T "SCWM Messages...." -e /bin/sh -c "echo 'SCWM MESSAGES: (Also writing to $ERRORSFILE)' &&\
 tail -f $ERRORSFILE" &
PID_TAIL_STARTSCWM=$!


if [ -r $HOME/.Xkeyboard-$HOST ]; then
  . $HOME/.Xkeyboard-$HOST
fi

# merge in X resources
for f in $HOME/.Xresources $HOME/.Xresources-cpp $HOME/.Xresources-local; do
  if [ -r $f ]; then
    echo "Merging in $f" 1>&2
    xrdb -cpp cpp-sed-nl -merge $f
  fi
done

# do keyboard mappings
for f in $HOME/.Xmodmap $HOME/.Xmodmap-local $HOME/.Xmodmap-${HOSTNAME%%.*}; do
  if [ -r $f ]; then
    echo "xmodmap $f" 1>&2
    xmodmap $f
  fi
done

# May want to run a different wm here.
$XTERM_PROGRAM_NAME -geometry +0-0 -e bash -c "trap bash SIGINT; echo \"Press CTRL-C to \
allow commands before starting wm\"; sleep 3"

echo $PID_TAIL_STARTSCWM > $HOME/.tracepids
WMEXEC=$WM

while true; do
echo "Exec-ing wm as $WMEXEC"
#FIX: These options really apply only to scwm, not any arbitrary wm
$WMEXEC --segv-reset-count 0 2>&1 >>$ERRORSFILE
# want an xterm below if scwm returns non-zero exit (in case something
# bad happened, we don't want all of our X-clients to die.
if [ 0 -ne "$?" ]; then
  $XTERM_PROGRAM_NAME -geometry -0-0 -e sh -c "echo exit && { $SHELL || zsh || bash }"
else
  exit
fi
done
