#!/bin/zsh -f

# There is a bug in OS X x-windows that interferes with xdlmapman and xdldataman.
# The pop-up frameless window that permits you to select map or data format does
# not appear unless you first iconify the major display window.  This is always
# tedious and sometimes everything freezes.  This shell script uses xnest to 
# create a nested X-windows process with a more well-behaved, light-weight x-window
# manager (evilwm or twm).

###############################################################################
# Do some reality checks:

if [[ ! -x $(which Xnest) ]];then
    print "xnest does not appear to be on this system"
    print "running the vanilla $0 command"
    command $0
    return 42
fi

# No point in using this unless we are on OS X:
if [[ $(uname) != Darwin ]];then
	print "This is a bugfix for OS X X-windows. This does not appear to be OS X."
	print "Don't use this.  Use the real $0"
	return 42
fi

# CCP4 must be present and initialized.
if [[ -z $CCP4 ]]; then
	print "I cannot find CCP4. Please make sure CCP4 is installed and initialized."
	return 43
fi

# The resolution will not be teleported.
if [[ -n $SSH_TTY ]]; then
	print "This won't work on a remote display"
	return 44
fi
###############################################################################

# Uses evilwm if present; otherwise the ugly twm.  These are both low overhead.

################ Make a temporary .xinitrc file on the fly ####################

cat >| /tmp/.xenmapman_$$_junk  << eof

# start the xdlmapman program
$CBIN/xdlmapman & 


command xterm -geometry 80X25+600+400 -bg black -fg white -sb -sl 1000 -rightbar  -T "$PWD" -e '/bin/zsh -c print " "; print "\e[1mPlease select a map file by highlighting it:\e[0m" ; print " "; ls *.map; print " "; print "\e[1mThen paste it in the \"Read a New Map File\" dialogue box\e[0m "; print " "; /bin/zsh' &

if [[ -x \$(which evilwm) ]];then
   exec evilwm -bg blue -fg RoyalBlue3 -bw 5
 else
    exec  twm
fi

eof
###############################################################################


# Find the resolution of the monitor:
res_array=($(system_profiler -detailLevel mini | grep Resolution | awk '{print $2 "  "  $4}'))
 
# Shave off 100 or 200 pixels in each dimension
adj_horiz=$((res_array[1]-200))
adj_vert=$((res_array[2]-100))


if [[  -x  /usr/X11/bin/Xephyr   ]]; then
    ( xinit /tmp/.xenmapman_$$_junk -- /usr/X11/bin/Xephyr :$$[3]$$[4] -screen ${adj_horiz}x${adj_vert}+100+0 -br  -I  2>| /dev/null & )


elif [[  -x  /usr/X11/bin/Xnest   ]]; then
    ( xinit /tmp/.xenmapman_$$_junk -- /usr/X11/bin/Xnest :$$[3]$$[4]  -name XDLMAPMAN -br  \
	                                   -geometry ${adj_horiz}x${adj_vert}+100+0 2>| /dev/null & )
  
elif [[ -x  /usr/X11R6/bin/Xnest  ]]; then
    ( xinit /tmp/.xenmapman_$$_junk -- /usr/X11/bin/Xnest :$$[3]$$[4]  -name XDLMAPMAN -br  \
	                                   -geometry ${adj_horiz}x${adj_vert}+100+0 2>| /dev/null & )

else
	print "Could not find Xnest or Xephyr"
	return 1000
fi

 
#  xinit /tmp/.xenmapman_$$_junk --  $XNESTPATH :9$$[4] -name XDLMAPMAN \
#                        -geometry ${adj_horiz}x${adj_vert}+100+0  2>| /dev/null &
   
sleep 5
rm -f /tmp/.xenmapman_$$_junk
  


 
