1#!/bin/bash
2
3#
4# x11-launcher.sh - X11 Application Launcher for the VICE Project
5#
6# Written by
7#  Christian Vogelgsang <chris@vogelgsang.org>
8#
9# inspired by Gimp.app of Aaron Voisine <aaron@voisine.org>
10#
11# This file is part of VICE, the Versatile Commodore Emulator.
12# See README for copyright notice.
13#
14#  This program is free software; you can redistribute it and/or modify
15#  it under the terms of the GNU General Public License as published by
16#  the Free Software Foundation; either version 2 of the License, or
17#  (at your option) any later version.
18#
19#  This program is distributed in the hope that it will be useful,
20#  but WITHOUT ANY WARRANTY; without even the implied warranty of
21#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22#  GNU General Public License for more details.
23#
24#  You should have received a copy of the GNU General Public License
25#  along with this program; if not, write to the Free Software
26#  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
27#  02111-1307  USA.
28#
29
30# --- debug echo ---
31# only available if DEBUG_VICE_LAUNCHER is defined
32dbgecho () {
33  [ "$DEBUG_VICE_LAUNCHER" != "" ] && echo "$@"
34  [ "$LOG_VICE_LAUNCHER" != "" ] && echo "$@" >> $HOME/vice_launcher.log
35}
36
37# --- find bundle name and resource dir ---
38SCRIPT_DIR="`dirname \"$0\"`"
39RESOURCES_DIR="`cd \"$SCRIPT_DIR/../Resources\" && pwd`"
40BUNDLE_DIR="`cd \"$SCRIPT_DIR/../..\" && pwd`"
41BUNDLE_NAME="`basename \"$BUNDLE_DIR\" .app`"
42dbgecho "BUNDLE_DIR=$BUNDLE_DIR"
43dbgecho "BUNDLE=$BUNDLE_NAME"
44dbgecho "ARGS=""$@"
45
46# --- determine launch environment ---
47LAUNCH=cmdline
48# finder always appends a -psn_ switch
49echo "$1" | grep -e -psn_ > /dev/null
50if [ "$?" == "0" ]; then
51  LAUNCH=finder
52fi
53# platypus passes the bundle as in first arg
54if [ "$1" = "$BUNDLE_DIR" ]; then
55  LAUNCH=platypus
56  shift
57fi
58dbgecho "LAUNCH=$LAUNCH"
59
60# only do the following if no X11 env (i.e. DISPLAY) is defined
61# e.g. not required for Mac OS X 10.5 Leopard
62CREATED_XINITRC=0
63if [ "x$DISPLAY" = "x" ]; then
64
65# --- create a temporary .xinitc if X11 is not running and user has none ---
66# check for X11
67ps -wx -ocommand | grep X11.app > /dev/null | grep -v grep > /dev/null
68if [ "$?" != "0" ]; then
69  # if user has no config then create one
70  if [ ! -f "$HOME/.xinitrc" ]; then
71    DEFAULT_XINITRC="/usr/X11R6/lib/X11/xinit/xinitrc"
72    if [ -f "$DEFAULT_XINITRC" ]; then
73      # create a one time xinitrc for gimp without an xterm
74      sed 's/xterm/# xterm/' "$DEFAULT_XINITRC" > "$HOME/.xinitrc"
75      CREATED_XINITRC=1
76      dbgecho "created user's .xinitrc"
77    fi
78  fi
79fi
80
81# --- launch x11 if not already here and find out our DISPLAY ---
82# create temp
83TMP_DIR="/var/tmp/$UID-$$"
84mkdir -p "$TMP_DIR" || exit 1
85DISPLAY_RUN="$TMP_DIR/display.run"
86DISPLAY_RESULT="$TMP_DIR/display.result"
87rm -f "$DISPLAY_RESULT"
88# make display emitter command
89echo "#!/bin/sh" > "$DISPLAY_RUN"
90echo "echo \"\$DISPLAY\" > \"$DISPLAY_RESULT\"" >> "$DISPLAY_RUN"
91chmod 755 "$DISPLAY_RUN"
92# launch x11 and run display emitter
93/usr/bin/open-x11 "$DISPLAY_RUN"
94# wait for command
95while [ "$?" == "0" -a ! -f "$DISPLAY_RESULT" ]; do sleep 1; done
96# fetch display
97DISPLAY="`cat \"$DISPLAY_RESULT\"`"
98if [ "$DISPLAY" = "" ]; then
99  DISPLAY=":0"
100fi
101dbgecho "DISPLAY=$DISPLAY"
102export DISPLAY
103# clean up
104rm -f "$DISPLAY_RUN" "$DISPLAY_RESULT"
105rmdir "$TMP_DIR"
106
107fi # xDISPLAY=x
108
109# --- prepare platypus dropped file args for VICE ---
110if [ "$LAUNCH" = "platypus" ]; then
111  if [ "$1" != "" ]; then
112    LAUNCH_FILE="$1"
113  fi
114fi
115
116# --- setup environment ---
117# setup dylib path
118LIB_DIR="$RESOURCES_DIR/lib"
119if [ -d "$LIB_DIR" ]; then
120  PROGRAM_PREFIX="env DYLD_LIBRARY_PATH=\"$LIB_DIR\""
121fi
122dbgecho "PROGRAM_PREFIX=$PROGRAM_PREFIX"
123# setup path
124BIN_DIR="$RESOURCES_DIR/bin"
125if [ ! -d "$BIN_DIR" ]; then
126  dbgecho "Directory $BIN_DIR not found!"
127  exit 1
128fi
129export PATH="$BIN_DIR:/usr/X11R6/bin:$PATH"
130
131# GTK: setup fontconfig
132ETC_DIR="$RESOURCES_DIR/etc"
133if [ -d "$ETC_DIR/fonts" ]; then
134  export "FONTCONFIG_PATH=$ETC_DIR/fonts"
135fi
136
137# --- find VICE binary ---
138# derive emu name from bundle name
139if [ "x$PROGRAM" = "x" ]; then
140  EMUS="x128,x64,x64dtv,x64sc,xcbm2,xcbm5x0,xpet,xplus4,xvic"
141  case "$BUNDLE_NAME" in
142  x128*)
143    PROGRAM=x128
144    ;;
145  x64*)
146    PROGRAM=x64
147    ;;
148  x64dtv*)
149    PROGRAM=x64dtv
150    ;;
151  x64sc*)
152    PROGRAM=x64sc
153    ;;
154  xcbm2*)
155    PROGRAM=xcbm2
156    ;;
157  xcbm5x0*)
158    PROGRAM=xcbm5x0
159    ;;
160  xpet*)
161    PROGRAM=xpet
162    ;;
163  xplus*)
164    PROGRAM=xplus
165    ;;
166  xvic*)
167    PROGRAM=xvic
168    ;;
169  VICE*)
170    # pick emu name in dialog
171    PROGRAM=`xmessage -nearmouse -print -buttons "$EMUS" "Please select an Emulator to run:"`
172    ;;
173  *)
174    # invalid bundle name
175    xmessage -nearmouse -buttons "Abort" -default "Abort" "Invalid Bundle Name! (use: VICE,$EMUS)"
176    PROGRAM=""
177    ;;
178  esac
179fi
180dbgecho "PROGRAM=$PROGRAM"
181PROGRAM_PATH="$BIN_DIR/$PROGRAM"
182dbgecho "PROGRAM_PATH=$PROGRAM_PATH"
183
184# --- now launch the VICE emulator ---
185if [ "$LAUNCH" = "cmdline" ]; then
186  # launch in cmd line without xterm
187  dbgecho "CMDLINE ARGS=""$@"
188  $PROGRAM_PREFIX "$PROGRAM_PATH" "$@"
189else
190  # use xterm as console
191  XTERM_BIN=`which xterm`
192  if [ "x$XTERM_BIN" != "x" ]; then
193    dbgecho "xterm not found!"
194    exit 1
195  fi
196  dbgecho "XTERM LAUNCH_FILE=" "$LAUNCH_FILE"
197  if [ "$LAUNCH_FILE" != "" ]; then
198    "$XTERM_BIN" \
199      -sb -title "VICE $PROGRAM Console" \
200      -e $PROGRAM_PREFIX "$PROGRAM_PATH" -autostart "$LAUNCH_FILE"
201  else
202    "$XTERM_BIN" \
203      -sb -title "VICE $PROGRAM Console" \
204      -e $PROGRAM_PREFIX "$PROGRAM_PATH"
205  fi
206fi
207
208# --- clean up ---
209# remove temporary .xinitc
210if [ $CREATED_XINITRC = 1 ]; then
211  rm "$HOME/.xinitrc"
212  dbgecho "removed user's .xinitrc"
213fi
214
215exit 0
216
217