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 "[InstallCompleteText]
57cdf0e10cSrcweir
58cdf0e10cSrcweir[InstallCompleteText2]"
59cdf0e10cSrcweir
60cdf0e10cSrcweirset sourcedir to (do shell script "dirname " & quoted form of POSIX path of (path to of me))
61cdf0e10cSrcweir
62cdf0e10cSrcweirdisplay dialog intro buttons {AbortLabel, InstallLabel} default button 2
63cdf0e10cSrcweir
64cdf0e10cSrcweirif (button returned of result) is AbortLabel then
65cdf0e10cSrcweir	return 2
66cdf0e10cSrcweirend if
67cdf0e10cSrcweir
68cdf0e10cSrcweirset the found_ooos_all to (do shell script "mdfind \"kMDItemContentType == 'com.apple.application-bundle' && kMDItemDisplayName == '[PRODUCTNAME]*' && kMDItemDisplayName != '[FULLAPPPRODUCTNAME].app'\"") & "
69cdf0e10cSrcweir" & chooseMyOwn
70cdf0e10cSrcweir
71cdf0e10cSrcweirset found_ooos_all_paragraphs to paragraphs in found_ooos_all
72cdf0e10cSrcweir
73cdf0e10cSrcweirset found_ooos to {}
74cdf0e10cSrcweirrepeat with currentApp in found_ooos_all_paragraphs
75cdf0e10cSrcweir	if currentApp does not start with "/Volumes" then
76cdf0e10cSrcweir		copy currentApp to the end of found_ooos
77cdf0e10cSrcweir	end if
78cdf0e10cSrcweirend repeat
79cdf0e10cSrcweir
80cdf0e10cSrcweir-- repeat with oneApp in found_ooos
81cdf0e10cSrcweir--  display dialog oneApp
82cdf0e10cSrcweir-- end repeat
83cdf0e10cSrcweir
84cdf0e10cSrcweir-- the choice returned is of type "list"
85cdf0e10cSrcweir-- Show selection dialog only if more than one or no product was found
86cdf0e10cSrcweir-- The first item is an empty string, if no app was found and no app started with "/Volumes"
87cdf0e10cSrcweir-- The first item is chooseMyOwn, if no app was found and at least one app started with "/Volumes"
88cdf0e10cSrcweirif (get first item of found_ooos as string) is "" then
89cdf0e10cSrcweir  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)
90cdf0e10cSrcweir  if choice is false then
91cdf0e10cSrcweir	  -- do nothing, the user cancelled the installation
92cdf0e10cSrcweir    	return 2 --aborted by user
93cdf0e10cSrcweir  else if (choice as string) is chooseMyOwn then
94cdf0e10cSrcweir	  -- yeah, one needs to use "choose file", otherwise
95cdf0e10cSrcweir	  -- the user would not be able to select the .app
96cdf0e10cSrcweir	  set the choice to POSIX path of (choose file with prompt chooseManual of type "com.apple.application-bundle" without showing package contents and invisibles)
97cdf0e10cSrcweir  end if
98cdf0e10cSrcweirelse if (get first item of found_ooos as string) is chooseMyOwn then
99cdf0e10cSrcweir  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)
100cdf0e10cSrcweir  if choice is false then
101cdf0e10cSrcweir	  -- do nothing, the user cancelled the installation
102cdf0e10cSrcweir    	return 2 --aborted by user
103cdf0e10cSrcweir  else if (choice as string) is chooseMyOwn then
104cdf0e10cSrcweir	  -- yeah, one needs to use "choose file", otherwise
105cdf0e10cSrcweir	  -- the user would not be able to select the .app
106cdf0e10cSrcweir	  set the choice to POSIX path of (choose file with prompt chooseManual of type "com.apple.application-bundle" without showing package contents and invisibles)
107cdf0e10cSrcweir  end if
108cdf0e10cSrcweirelse if (get second item of found_ooos as string) is chooseMyOwn then
109cdf0e10cSrcweir  -- set choice to found installation
110cdf0e10cSrcweir  -- set the choice to (get first paragraph of found_ooos)
111cdf0e10cSrcweir  set the choice to (get first item of found_ooos)
112cdf0e10cSrcweirelse
113cdf0e10cSrcweir  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)
114cdf0e10cSrcweir  if choice is false then
115cdf0e10cSrcweir	  -- do nothing, the user cancelled the installation
116cdf0e10cSrcweir    	return 2 --aborted by user
117cdf0e10cSrcweir  else if (choice as string) is chooseMyOwn then
118cdf0e10cSrcweir	  -- yeah, one needs to use "choose file", otherwise
119cdf0e10cSrcweir	  -- the user would not be able to select the .app
120cdf0e10cSrcweir	  set the choice to POSIX path of (choose file with prompt chooseManual of type "com.apple.application-bundle" without showing package contents and invisibles)
121cdf0e10cSrcweir  end if
122cdf0e10cSrcweirend if
123cdf0e10cSrcweir
124cdf0e10cSrcweir-- now only check whether the path is really from [PRODUCTNAME]
125cdf0e10cSrcweirtry
126cdf0e10cSrcweir	do shell script "grep '<string>[PRODUCTNAME] [PRODUCTVERSION]'   " & quoted form of (choice as string) & "/Contents/Info.plist"
127cdf0e10cSrcweiron error
128cdf0e10cSrcweir	display dialog (choice as string) & appInvalid buttons {InstallLabel} default button 1 with icon 0
129cdf0e10cSrcweir	return 3 --wrong target-directory
130cdf0e10cSrcweirend try
131cdf0e10cSrcweir
132cdf0e10cSrcweir(*
133cdf0e10cSrcweirdisplay dialog startInstall buttons {AbortLabel, InstallLabel} default button 2
134cdf0e10cSrcweir
135cdf0e10cSrcweirif (button returned of result) is AbortLabel then
136cdf0e10cSrcweir	return 2
137cdf0e10cSrcweirend if
138cdf0e10cSrcweir*)
139cdf0e10cSrcweir
140cdf0e10cSrcweirset tarCommand to "/usr/bin/tar -C " & quoted form of (choice as string) & " -xjf " & quoted form of sourcedir & "/tarball.tar.bz2"
141cdf0e10cSrcweirtry
142cdf0e10cSrcweir	do shell script tarCommand
143cdf0e10cSrcweir
144cdf0e10cSrcweiron error errMSG number errNUM
145cdf0e10cSrcweir	display dialog IdentifyQ buttons {IdentifyYES, IdentifyNO} with icon 2
146cdf0e10cSrcweir	if (button returned of result) is IdentifyYES then
147cdf0e10cSrcweir		try
148cdf0e10cSrcweir			do shell script tarCommand with administrator privileges
149cdf0e10cSrcweir		on error errMSG number errNUM
150cdf0e10cSrcweir			display dialog installFailed buttons {OKLabel} default button 1 with icon 0
151cdf0e10cSrcweir			-- -60005 username/password wrong
152cdf0e10cSrcweir			-- -128   aborted by user
153cdf0e10cSrcweir			-- 2 error from tar - tarball not found (easy to test)
154cdf0e10cSrcweir			return errNUM
155cdf0e10cSrcweir		end try
156cdf0e10cSrcweir	else
157cdf0e10cSrcweir		return 2 -- aborted by user
158cdf0e10cSrcweir	end if
159cdf0e10cSrcweirend try
160cdf0e10cSrcweir
161cdf0e10cSrcweirdisplay dialog installComplete buttons {OKLabel} default button 1
162