- Shunt some trace statements over to the typelib channel that aren't
[wine] / programs / winelauncher.in
1 #!/bin/sh
2 #------------------------------------------------------------------------------
3 #   Winelauncher
4 #       This shell script attempts to intelligently manage the process
5 #   of launching a program with Wine.  It adds some level of
6 #   visual feedback to an end user.
7 #
8 #   Usage:
9 #       winelauncher [options]  "<windows program> [program arguments]"
10 #
11 #       This script is meant to be installed to /usr/bin/wine, and
12 #   to be used to invoke a Windows executable.
13 #       The options are passed through directly to Wine, and are
14 #   documented in the Wine man page.
15 #
16 #   Copyright (c) 2000 by Jeremy White for CodeWeavers
17 #
18 # This library is free software; you can redistribute it and/or
19 # modify it under the terms of the GNU Lesser General Public
20 # License as published by the Free Software Foundation; either
21 # version 2.1 of the License, or (at your option) any later version.
22 #
23 # This library is distributed in the hope that it will be useful,
24 # but WITHOUT ANY WARRANTY; without even the implied warranty of
25 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
26 # Lesser General Public License for more details.
27 #
28 # You should have received a copy of the GNU Lesser General Public
29 # License along with this library; if not, write to the Free Software
30 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
31 #------------------------------------------------------------------------------
32
33 #------------------------------------------------------------------------------
34 #  Primary configuration area - change this if you installed Wine to
35 #    a different location
36 #------------------------------------------------------------------------------
37 WINEBIN=@bindir@
38 WINELIB=@libdir@
39 WINESERVER=
40 WINEDLLPATH=@dlldir@
41
42 #------------------------------------------------------------------------------
43 #  Establish Color Scheme
44 #------------------------------------------------------------------------------
45 COLOR=' -xrm *.Command.background:darkgrey
46 -xrm *.Command.foreground:black
47 -xrm *.Text.background:black
48 -xrm *.Text.foreground:green
49 -xrm *.Form.background:grey
50 -xrm *.Form.foreground:green
51 -xrm *.foreground:green
52 -xrm *.background:black'
53
54
55 #------------------------------------------------------------------------------
56 #  Locate either xmessage or gmessage, if we can.
57 #------------------------------------------------------------------------------
58 type xmessage >/dev/null 2>/dev/null
59 if [ $? -ne 0 ] ; then
60     # xmessage not found; make sure the user notices this error
61     # (GUI users wouldn't even notice if we printed the text on console !)
62     MSGFILE=`mktemp "/tmp/wine.xmessage.XXXXXX"`
63     cat > $MSGFILE <<EOF
64 Warning:
65     The Wine launcher is unable to find the xmessage program,
66     which it needs to properly notify you of Wine execution status
67     or problems.
68
69     This launcher script relies heavily on finding this tool,
70     and without it, it will behave very poorly.
71
72     We strongly recommend that you use your distribution's
73     software methods to locate xmessage, or alternatively
74     use your favourite internet search engine to find out
75     how you are supposed to install xmessage on your system.
76 EOF
77
78     # try to display message file with tons of different X11 editors
79     # until we find one that's installed and working
80     for EDITPRG in nedit gedit kedit gvim xemacs; do
81       type $EDITPRG >/dev/null 2>/dev/null
82       if [ $? -eq 0 ] ; then
83         # execute editor and exit if successful
84         $EDITPRG $MSGFILE && exit
85       fi
86     done
87
88     # ok, we really give up now, this system is hosed ;-)
89     cat $MSGFILE
90     rm $MSGFILE
91 else
92     XMESSAGE="xmessage $COLOR"
93 fi
94
95 launch_winesetup()
96 {
97     which winesetup
98     if [ $? -eq 0 ] ; then
99         winesetup
100     else
101         if [ -x /opt/wine/bin/winesetup ] ; then
102             /opt/wine/bin/winesetup
103         else
104             $XMESSAGE -title "Error" \
105         "Error:  Unable to find winesetup in your PATH or in /opt/wine/bin:
106         I am not able to configure Wine.
107
108         If winesetup is being distributed in a separate package
109         that is not installed yet on your system, then please install this
110         package.
111         winesetup can also be downloaded from www.codeweavers.com.
112
113         If you choose to not use winesetup for configuration, then
114         you can find information on how to prepare a Wine config file manually
115         in the Wine README / README.gz file or in the Wine User Guide."
116         fi
117     fi
118 }
119
120 #------------------------------------------------------------------------------
121 #  We're going to do a lot of fancy footwork below.
122 #    Before we get started, it would be nice to know the argv0
123 #    of the actual script we're running (and lets remove at least
124 #    one level of symlinking).
125 #------------------------------------------------------------------------------
126 argv0_path=`which $0`
127 if [ -z $argv0_path ] ; then
128     argv0_path=$0
129 fi
130
131 real_name=`find $argv0_path -type l -printf "%l\n"`
132 if [ ! $real_name ]; then
133     real_name=$argv0_path
134 elif [ ! -x $real_name ]; then
135     real_name=`find $argv0_path -printf "%h\n"`/$real_name
136 fi
137
138 argv0_dir=`find $real_name -printf "%h\n"`
139
140 if [ -z $argv0_dir ] ; then
141     argv0_dir=.
142 fi
143
144 #------------------------------------------------------------------------------
145 #  Okay, now all that junk above was established at configure time.
146 #   However, if this is an RPM install, they may have chosen
147 #   to relocate this installation.  If so, that stuff above
148 #   is all broken and we should rejigger it.
149 #------------------------------------------------------------------------------
150 WINE_BIN_NAME=wine.bin
151 if [ ! -x $WINEBIN/$WINE_BIN_NAME ] ; then
152     WINEBIN=`find $argv0_dir -maxdepth 1 -perm +0111 -type f -name "$WINE_BIN_NAME" -printf "%h\n" | head -n 1`
153 fi
154
155 if [ ! -x $WINEBIN/$WINE_BIN_NAME ] ; then
156     WINEBIN=`find $argv0_dir/../ -maxdepth 1 -perm +0111 -type f -name "$WINE_BIN_NAME" -printf "%h\n" | head -n 1`
157 fi
158
159 if [ ! -x $WINEBIN/$WINE_BIN_NAME ] ; then
160     WINE_BIN_NAME=wine
161     if [ ! -x $WINEBIN/$WINE_BIN_NAME ] ; then
162         WINEBIN=`find $argv0_dir -maxdepth 1 -perm +0111 -type f -name "$WINE_BIN_NAME" -printf "%h\n" | head -n 1`
163     fi
164
165     if [ ! -x $WINEBIN/$WINE_BIN_NAME ] ; then
166         WINEBIN=`find $argv0_dir/../ -maxdepth 1 -perm +0111 -type f -name "$WINE_BIN_NAME" -printf "%h\n" | head -n 1`
167     fi
168 fi
169
170 if [ ! -r $WINELIB/libwine.so ] ; then
171     WINELIB=`find $argv0_dir -maxdepth 2 -name 'libwine.so' -printf "%h\n" | head -n 1`
172 fi
173
174 if [ ! -r $WINELIB/libwine.so ] ; then
175     WINELIB=`find $argv0_dir/../ -maxdepth 2 -name 'libwine.so' -printf "%h\n" | head -n 1`
176 fi
177
178 if [ -x $WINEBIN/wineserver ] ; then
179     WINESERVER=$WINEBIN/wineserver
180 fi
181
182 #------------------------------------------------------------------------------
183 #  Hey, if we built Wine from source, let's add a little extra fun to
184 #   mix it up a bit
185 #------------------------------------------------------------------------------
186 if [ -x $WINEBIN/server/wineserver ] ; then
187     WINESERVER=$WINEBIN/server/wineserver
188 fi
189
190 if [ -r $WINELIB/dlls/ntdll.dll.so ] ; then
191     WINEDLLPATH=$WINELIB/dlls
192 fi
193
194
195 #------------------------------------------------------------------------------
196 #  Okay, set the paths and move on.
197 #------------------------------------------------------------------------------
198 export LD_LIBRARY_PATH=$WINELIB:$LD_LIBRARY_PATH
199 export PATH=$WINEBIN:$PATH
200 export WINELOADER=$WINEBIN/$WINE_BIN_NAME
201 export WINEDLLPATH
202 export WINESERVER
203
204 info_flag=~/.wine/.no_prelaunch_window_flag
205 debug_flag=~/.wine/.no_debug_window_flag
206 debug_options="warn+all"
207
208 if [ -f $info_flag ] ; then
209     use_info_message=0
210 else
211     use_info_message=1
212 fi
213
214 if [ -f $debug_flag ] ; then
215     use_debug_message=0
216 else
217     use_debug_message=1
218 fi
219
220
221 #------------------------------------------------------------------------------
222 #  Handle winelib apps going through here
223 #------------------------------------------------------------------------------
224 winelib=0
225 if [ -f $argv0_path.so ] ; then
226     winelib=1
227     export WINEPRELOAD=$argv0_path.so
228 fi
229
230
231 #------------------------------------------------------------------------------
232 #  No arguments?  Help 'em out
233 #------------------------------------------------------------------------------
234 always_see_output=0
235 no_args=0
236 if [ $# -eq 0 ] ; then
237     no_args=1
238 fi
239
240 if [ $# -eq 1 -a "$1" = "" ] ; then
241     no_args=1
242 fi
243
244 if [ $winelib -eq 1 ] ; then
245     no_args=0
246 fi
247
248 if [ $no_args -eq 1 ] ; then
249     echo "Wine called with no arguments."
250     echo "Invoking $WINEBIN/$WINE_BIN_NAME $@ ..."
251     $XMESSAGE -buttons "    Okay    ":0," See the Wine Usage Statement ":1,"  Configure Wine  ":2 \
252         -title "Welcome to Wine" \
253         "
254
255         You have started Wine without specifying any arguments.
256
257         Wine requires at least one argument - the name of the Windows
258         application you would like to run.
259
260         If you have launched this through the KDE menu system
261         and your KDE installation is specially configured for Wine,
262         then you can use the KDE file browser to select a Windows
263         executable and then click on it to launch Wine with
264         that application.
265
266         You can similarly use the GNOME file manager to
267         select a Windows executable and double click on it.
268
269         If you would like to see the command line arguments
270         for Wine, select the second option, below.
271
272         "
273     welcome_rc=$?
274     if [ $welcome_rc -eq 0 ] ; then
275         exit
276     fi
277
278     if [ $welcome_rc -eq 2 ] ; then
279         launch_winesetup
280         exit
281     fi
282
283     use_info_message=0
284     always_see_output=1
285 fi
286
287 #------------------------------------------------------------------------------
288 #  No config file?  Offer to help 'em out...
289 #------------------------------------------------------------------------------
290 conf=0
291
292 while [ $conf -eq 0 ] ; do
293
294     if [ -f ~/.winerc ] ; then
295         conf=1
296     fi
297     if [ -f ~/.wine/config ] ; then
298         conf=2
299     fi
300     if [ -f /etc/wine.conf ] ; then
301         conf=3
302     fi
303
304     if [ $conf -ne 0 ] ; then
305         break;
306     fi
307
308     echo "No configuration file detected."
309     $XMESSAGE -buttons "    Cancel    ":0,"  Proceed   ":1,"  Configure Wine  ":2 \
310         -title "Welcome to Wine" \
311         "
312
313         You have started Wine, but we cannot find a Wine
314         configuration file.
315
316         This is normal if you have never run Wine before.
317         If this is the case, select the 'Configure Wine'
318         option, below, to create a configuration file.
319
320         "
321     init_rc=$?
322     if [ $init_rc -eq 0 ] ; then
323         exit
324     fi
325
326     if [ $init_rc -eq 1 ] ; then
327         break
328     fi
329
330     if [ $init_rc -eq 2 ] ; then
331         launch_winesetup
332     fi
333
334     # 127 == xmessage not available
335     if [ $init_rc -eq 127 ] ; then
336       exit
337     fi
338
339 done
340
341 #------------------------------------------------------------------------------
342 #  Optionally Warn the user we're going to be launching Wine...
343 #------------------------------------------------------------------------------
344 if [ $use_info_message -ne 0 ] ; then
345     echo "Invoking $WINEBIN/$WINE_BIN_NAME $@ ..."
346     $XMESSAGE -timeout 30 -buttons "    Dismiss    ":0," Never display this message again ":3 \
347         -title "Wine Launch Window" \
348         "Invoking $WINEBIN/$WINE_BIN_NAME $@ ...
349
350         This dialog box is a temporary status dialog to let you know
351         that Wine is attempting to launch your application.
352
353         Since Wine is still very much in a development stage,
354         many applications will fail silently.
355         This dialog box is your indication
356         that we're *trying* to run your application.
357
358         This dialog box will automatically disappear after 30 seconds,
359         or after your application finishes.
360
361         You can permanently disable this dialog by selecting
362         the option below.
363         " &
364     info_message_pid=$!
365 fi
366
367 #------------------------------------------------------------------------------
368 #  Here's a little function to clean up after that dialog...
369 #------------------------------------------------------------------------------
370 clean_up_info_message ()
371 {
372     if [ $use_info_message -ne 0 ] ; then
373
374         #------------------------------------------------------------------------------
375         #  Okay, make sure that the notice window is dead (and kill it if it ain't)
376         #------------------------------------------------------------------------------
377         ps $info_message_pid >/dev/null 2>&1
378         if [ $? -ne 0 ] ; then
379             wait $info_message_pid
380             info_return=$?
381         else
382             info_return=0
383             kill $info_message_pid
384         fi
385
386         #------------------------------------------------------------------------------
387         #  If they didn't like the warning window, turn it off
388         #------------------------------------------------------------------------------
389         if [ $info_return -eq 3 ] ; then
390             $XMESSAGE -title "Wine Prelaunch Control" \
391             "Wine will now disable the prelaunch window you just saw.
392             You will no longer be notified when Wine is attempting
393             to start a Windows application.
394
395             Please take note that you can reenable this window
396             by removing the $info_flag file." -buttons "  Okay  ":0," Cancel ":1
397             if [ $? -eq 0 ] ; then
398                 touch $info_flag
399             fi
400         fi
401     fi
402
403     use_info_message=0
404 }
405 #------------------------------------------------------------------------------
406 #  Generate a temporary log file name
407 #------------------------------------------------------------------------------
408 if [ -n "$TMP" ] ; then
409   log_dir="$TMP"
410 else
411   log_dir="/tmp"
412 fi
413 use_log_name=0
414 log_name=`mktemp "$log_dir/wine.log.XXXXXX"`
415 if [ $? -eq 0 ] ; then
416     use_log_name=1
417 fi
418 use_status_name=0
419 status_name=`mktemp "$log_dir/wine.status.XXXXXX"`
420 if [ $? -eq 0 ] ; then
421     use_status_name=1
422 fi
423
424 #------------------------------------------------------------------------------
425 #  Okay, really launch Wine...
426 #------------------------------------------------------------------------------
427 if [ $use_log_name -ne 0 -a $use_status_name -ne 0 ] ; then
428     ( $WINEBIN/$WINE_BIN_NAME "$@"; echo $? >$status_name ) 2>&1 | tee "$log_name"
429     wine_return=`cat $status_name`
430 else
431     $WINEBIN/$WINE_BIN_NAME "$@"
432     wine_return=$?
433 fi
434 if [ $use_status_name -ne 0 ] ; then
435     rm -f $status_name
436 fi
437
438 #------------------------------------------------------------------------------
439 #  Test the return code, and see if it fails
440 #------------------------------------------------------------------------------
441 if [ $always_see_output -eq 0 -a $wine_return -eq 0 ] ; then
442     echo "Wine exited with a successful status"
443     if [ $use_log_name -ne 0 ] ; then
444         rm -f "$log_name"
445     fi
446 else
447     if [ $always_see_output -eq 0 ] ; then
448         echo "Wine failed with return code $wine_return"
449     fi
450
451     #------------------------------------------------------------------------------
452     #  Gracefully display a debug message if they like...
453     #------------------------------------------------------------------------------
454     while [ $use_debug_message -gt 0 ] ; do
455
456         #------------------------------------------------------------------------------
457         #  Build up the menu of choices they can make...
458         #------------------------------------------------------------------------------
459         BUTTONS='    Okay    :0'
460         if [ $use_log_name -ne 0 ] ; then
461             BUTTONS="$BUTTONS"', View Log :1'
462         fi
463
464         BUTTONS="$BUTTONS"',  Debug  :2'
465         BUTTONS="$BUTTONS"',  Configure :4'
466         BUTTONS="$BUTTONS"',  Disable :3'
467
468         #------------------------------------------------------------------------------
469         #  Build an error message
470         #------------------------------------------------------------------------------
471         MESSAGE="
472 Wine has exited with a failure status of $wine_return.
473
474 Wine is still development software, so there can be many
475 explanations for this problem.
476
477 You can choose to run Wine again with a higher level
478 of debug messages (the debug option, below).
479
480 You can attempt to reconfigure Wine to make it work better.
481 Note that one change you can make that will dramatically
482 effect Wine's behaviour is to change whether or not
483 Wine uses a true Windows partition, mounted under Linux,
484 or whether it uses an empty Windows directory.
485 The Wine Configuration program can assist you in making
486 those changes (select Configure, below, for more).
487
488 You can disable this message entirely by selecting the
489 Disable option below."
490
491         if [ $always_see_output -ne 0 -a $wine_return -eq 0 ] ; then
492             MESSAGE="
493 Wine has exited with a failure status of $wine_return.
494
495 You can disable this message entirely by selecting the
496 Disable option below."
497
498         fi
499
500         if [ $use_log_name -ne 0 ] ; then
501             MESSAGE="$MESSAGE
502
503 Wine has captured a log of the Wine output in the file $log_name.
504 You may view this file by selecting View Log, below."
505         fi
506
507         #------------------------------------------------------------------------------
508         #  Display the message
509         #------------------------------------------------------------------------------
510         $XMESSAGE -title "Wine Finished With Error" -buttons "$BUTTONS" "$MESSAGE"
511         debug_return=$?
512
513         #------------------------------------------------------------------------------
514         #  Dismiss the other window...
515         #------------------------------------------------------------------------------
516         clean_up_info_message
517
518         #------------------------------------------------------------------------------
519         #  Process a configure instruction
520         #------------------------------------------------------------------------------
521         if [ $debug_return -eq 4 ] ; then
522             launch_winesetup
523             continue;
524         fi
525
526         #------------------------------------------------------------------------------
527         #  Process a view instruction
528         #------------------------------------------------------------------------------
529         if [ $debug_return -eq 1 ] ; then
530             $XMESSAGE -title "View Wine Log" -file "$log_name" -buttons "  Okay  ":0,"Delete $log_name":1
531             if [ $? -eq 1 ] ; then
532                 echo "Deleting $log_name"
533                 rm -f "$log_name"
534                 use_log_name=0
535             fi
536         else
537             use_debug_message=0
538         fi
539
540         #------------------------------------------------------------------------------
541         #  If they didn't like the warning window, turn it off
542         #------------------------------------------------------------------------------
543         if [ $debug_return -eq 3 ] ; then
544             $XMESSAGE -title "Wine Debug Log Control" \
545             "Wine will now disable the Wine debug output control window you just saw.
546             You will no longer be notified when Wine fails to start a
547             Windows application.
548
549             Please take note that you can reenable this window
550             by removing the $debug_flag file." -buttons "  Okay  ":0," Cancel ":1
551
552             if [ $? -eq 0 ] ; then
553                 touch $debug_flag
554             fi
555
556         fi
557
558         #------------------------------------------------------------------------------
559         #  If they want to retry with debug, let 'em.
560         #------------------------------------------------------------------------------
561         if [ $debug_return -eq 2 ] ; then
562             echo "Rerunning WINEDEBUG=$debug_options $0 $@"
563             WINEDEBUG=$debug_options exec $0 "$@"
564         fi
565     done
566 fi
567
568
569 clean_up_info_message
570
571 # killed by signal?
572 if [ $wine_return -ge 128 ]; then
573         # try to kill myself with the same signal
574         kill -$[wine_return - 128] $$
575         # if we get here the kill didn't work
576         exit 1
577 fi