1 (*
2 
3   Licensed to the Apache Software Foundation (ASF) under one
4   or more contributor license agreements.  See the NOTICE file
5   distributed with this work for additional information
6   regarding copyright ownership.  The ASF licenses this file
7   to you under the Apache License, Version 2.0 (the
8   "License"); you may not use this file except in compliance
9   with the License.  You may obtain a copy of the License at
10 
11     http://www.apache.org/licenses/LICENSE-2.0
12 
13   Unless required by applicable law or agreed to in writing,
14   software distributed under the License is distributed on an
15   "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   KIND, either express or implied.  See the License for the
17   specific language governing permissions and limitations
18   under the License.
19 
20 
21 
22 This script is meant to
23 	1) Identify installed instances of the product
24 	2) check whether the user has write-access (and if not
25 	ask for authentification)
26 	3) install the shipped tarball
27 *)
28 
29 -- strings for localisations - to be meant to be replaced
30 -- by a makefile or similar
31 set OKLabel to "[OKLabel]"
32 set InstallLabel to "[InstallLabel]"
33 set AbortLabel to "[AbortLabel]"
34 set intro to "[IntroText1]
35 
36 [IntroText2]
37 
38 [IntroText3]"
39 set chooseMyOwn to "[ChooseMyOwnText]"
40 set listPrompt to "[ListPromptText]"
41 set chooseManual to "[ChooseManualText]"
42 set listOKLabel to "[ListOKLabelText]"
43 set listCancelLabel to "[ListCancelLabel]"
44 set appInvalid to "[AppInvalidText1]
45 
46 [AppInvalidText2]" -- string will begin with the chosen application's name
47 set startInstall to "[StartInstallText1]
48 
49 [StartInstallText2]"
50 set IdentifyQ to "[IdentifyQText]
51 
52 [IdentifyQText2]"
53 set IdentifyYES to "[IdentifyYES]"
54 set IdentifyNO to "[IdentifyNO]"
55 set installFailed to "[InstallFailedText]"
56 set installComplete to "[InstallCompleteTextPatch]"
57 
58 set sourcedir to (do shell script "dirname " & quoted form of POSIX path of (path to of me))
59 
60 display dialog intro buttons {AbortLabel, InstallLabel} default button 2
61 
62 if (button returned of result) is AbortLabel then
63 	return 2
64 end if
65 
66 set the found_ooos_all to (do shell script "mdfind \"kMDItemContentType == 'com.apple.application-bundle' && kMDItemDisplayName == '[PRODUCTNAME]*' && kMDItemDisplayName != '[FULLAPPPRODUCTNAME].app'\"") & "
67 " & chooseMyOwn
68 
69 set found_ooos_all_paragraphs to paragraphs in found_ooos_all
70 
71 set found_ooos to {}
72 repeat with currentApp in found_ooos_all_paragraphs
73 	if currentApp does not start with "/Volumes" then
74 		copy currentApp to the end of found_ooos
75 	end if
76 end repeat
77 
78 -- repeat with oneApp in found_ooos
79 --  display dialog oneApp
80 -- end repeat
81 
82 -- the choice returned is of type "list"
83 -- Show selection dialog only if more than one or no product was found
84 -- The first item is an empty string, if no app was found and no app started with "/Volumes"
85 -- The first item is chooseMyOwn, if no app was found and at least one app started with "/Volumes"
86 if (get first item of found_ooos as string) is "" then
87   set the choice to (choose from list found_ooos default items (get second item of found_ooos) with prompt listPrompt OK button name listOKLabel cancel button name listCancelLabel)
88   if choice is false then
89 	  -- do nothing, the user cancelled the installation
90     	return 2 --aborted by user
91   else if (choice as string) is chooseMyOwn then
92 	  -- yeah, one needs to use "choose file", otherwise
93 	  -- the user would not be able to select the .app
94 	  set the choice to POSIX path of (choose file with prompt chooseManual of type "com.apple.application-bundle" without showing package contents and invisibles)
95   end if
96 else if (get first item of found_ooos as string) is chooseMyOwn then
97   set the choice to (choose from list found_ooos default items (get first item of found_ooos) with prompt listPrompt OK button name listOKLabel cancel button name listCancelLabel)
98   if choice is false then
99 	  -- do nothing, the user cancelled the installation
100     	return 2 --aborted by user
101   else if (choice as string) is chooseMyOwn then
102 	  -- yeah, one needs to use "choose file", otherwise
103 	  -- the user would not be able to select the .app
104 	  set the choice to POSIX path of (choose file with prompt chooseManual of type "com.apple.application-bundle" without showing package contents and invisibles)
105   end if
106 else if (get second item of found_ooos as string) is chooseMyOwn then
107   -- set choice to found installation
108   -- set the choice to (get first paragraph of found_ooos)
109   set the choice to (get first item of found_ooos)
110 else
111   set the choice to (choose from list found_ooos default items (get first item of found_ooos) with prompt listPrompt OK button name listOKLabel cancel button name listCancelLabel)
112   if choice is false then
113 	  -- do nothing, the user cancelled the installation
114     	return 2 --aborted by user
115   else if (choice as string) is chooseMyOwn then
116 	  -- yeah, one needs to use "choose file", otherwise
117 	  -- the user would not be able to select the .app
118 	  set the choice to POSIX path of (choose file with prompt chooseManual of type "com.apple.application-bundle" without showing package contents and invisibles)
119   end if
120 end if
121 
122 -- now only check whether the path is really from [PRODUCTNAME]
123 try
124 	do shell script "grep '<string>[PRODUCTNAME] [PRODUCTVERSION]'   " & quoted form of (choice as string) & "/Contents/Info.plist"
125 on error
126 	display dialog (choice as string) & appInvalid buttons {InstallLabel} default button 1 with icon 0
127 	return 3 --wrong target-directory
128 end try
129 
130 (*
131 display dialog startInstall buttons {AbortLabel, InstallLabel} default button 2
132 
133 if (button returned of result) is AbortLabel then
134 	return 2
135 end if
136 *)
137 
138 set tarCommand to "/usr/bin/tar -C " & quoted form of (choice as string) & " -xjf " & quoted form of sourcedir & "/tarball.tar.bz2"
139 try
140 	do shell script tarCommand
141 
142 on error errMSG number errNUM
143 	display dialog IdentifyQ buttons {IdentifyYES, IdentifyNO} with icon 2
144 	if (button returned of result) is IdentifyYES then
145 		try
146 			do shell script tarCommand with administrator privileges
147 		on error errMSG number errNUM
148 			display dialog installFailed buttons {OKLabel} default button 1 with icon 0
149 			-- -60005 username/password wrong
150 			-- -128   aborted by user
151 			-- 2 error from tar - tarball not found (easy to test)
152 			return errNUM
153 		end try
154 	else
155 		return 2 -- aborted by user
156 	end if
157 end try
158 
159 display dialog installComplete buttons {OKLabel} default button 1
160