#!/bin/sh
##
# xnview.sh - Wrapper script for starting xnview from Aqua-GUI via Platypus
#
# Author:  Christoph Dalitz
# License: free for use, change and distribution without restriction
##


# set focus on X11
# (otherwise new application window will not have focus)
#osascript -e 'tell application "X11" to activate'

# load private environment
if [ -f $HOME/.profile ]
  then
  . $HOME/.profile
fi

# try to figure out DISPLAY cleverly
DISPLAY=""
for x in 0 1 2 3 4
do
  if [ -O /tmp/.X$x-lock ]
      then
      #echo "X11 setup: $USER has an active lock on DISPLAY :$x.0"
      DISPLAY=:$x.0
      break
  fi
done
#if [ -z "$DISPLAY" ]
#    then
#    echo "$USER has no X11 DISPLAY open" 1>&2
#    exit 1
#fi
#export DISPLAY
export DISPLAY=localhost:0

# xnview bundled with application
XNVIEW="$1/Contents/Resources/xnview"

# Platypus adds itself to the command line arguments
shift

# start xfig in background so that Platypus accepts further requests
cd
#"$XNVIEW" "$@" &

open-x11 sh 
"$XNVIEW" "$@" &

