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