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