xref: /trunk/main/setup_native/scripts/osx_install_patch.applescript (revision e88c27187f2588c2cbaa28304e98e9fcf62910dc)
1cdf0e10cSrcweir(*
25b501c92SAndrew Rist
35b501c92SAndrew Rist  Licensed to the Apache Software Foundation (ASF) under one
45b501c92SAndrew Rist  or more contributor license agreements.  See the NOTICE file
55b501c92SAndrew Rist  distributed with this work for additional information
65b501c92SAndrew Rist  regarding copyright ownership.  The ASF licenses this file
75b501c92SAndrew Rist  to you under the Apache License, Version 2.0 (the
85b501c92SAndrew Rist  "License"); you may not use this file except in compliance
95b501c92SAndrew Rist  with the License.  You may obtain a copy of the License at
105b501c92SAndrew Rist
115b501c92SAndrew Rist    http://www.apache.org/licenses/LICENSE-2.0
125b501c92SAndrew Rist
135b501c92SAndrew Rist  Unless required by applicable law or agreed to in writing,
145b501c92SAndrew Rist  software distributed under the License is distributed on an
155b501c92SAndrew Rist  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
165b501c92SAndrew Rist  KIND, either express or implied.  See the License for the
175b501c92SAndrew Rist  specific language governing permissions and limitations
185b501c92SAndrew Rist  under the License.
195b501c92SAndrew Rist
205b501c92SAndrew Rist
215b501c92SAndrew 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]"
56*e88c2718SJim Jagielskiset installSignedFailed to "[InstallSignedPatchFailedText]"
57cdf0e10cSrcweirset installComplete to "[InstallCompleteTextPatch]"
58cdf0e10cSrcweir
59cdf0e10cSrcweirset sourcedir to (do shell script "dirname " & quoted form of POSIX path of (path to of me))
60cdf0e10cSrcweir
61cdf0e10cSrcweirdisplay dialog intro buttons {AbortLabel, InstallLabel} default button 2
62cdf0e10cSrcweir
63cdf0e10cSrcweirif (button returned of result) is AbortLabel then
64cdf0e10cSrcweir    return 2
65cdf0e10cSrcweirend if
66cdf0e10cSrcweir
67cdf0e10cSrcweirset the found_ooos_all to (do shell script "mdfind \"kMDItemContentType == 'com.apple.application-bundle' && kMDItemDisplayName == '[PRODUCTNAME]*' && kMDItemDisplayName != '[FULLAPPPRODUCTNAME].app'\"") & "
68cdf0e10cSrcweir" & chooseMyOwn
69cdf0e10cSrcweir
70cdf0e10cSrcweirset found_ooos_all_paragraphs to paragraphs in found_ooos_all
71cdf0e10cSrcweir
72cdf0e10cSrcweirset found_ooos to {}
73cdf0e10cSrcweirrepeat with currentApp in found_ooos_all_paragraphs
74cdf0e10cSrcweir    if currentApp does not start with "/Volumes" then
75cdf0e10cSrcweir        copy currentApp to the end of found_ooos
76cdf0e10cSrcweir    end if
77cdf0e10cSrcweirend repeat
78cdf0e10cSrcweir
79cdf0e10cSrcweir-- repeat with oneApp in found_ooos
80cdf0e10cSrcweir--  display dialog oneApp
81cdf0e10cSrcweir-- end repeat
82cdf0e10cSrcweir
83cdf0e10cSrcweir-- the choice returned is of type "list"
84cdf0e10cSrcweir-- Show selection dialog only if more than one or no product was found
85cdf0e10cSrcweir-- The first item is an empty string, if no app was found and no app started with "/Volumes"
86cdf0e10cSrcweir-- The first item is chooseMyOwn, if no app was found and at least one app started with "/Volumes"
87cdf0e10cSrcweirif (get first item of found_ooos as string) is "" then
88cdf0e10cSrcweir  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)
89cdf0e10cSrcweir  if choice is false then
90cdf0e10cSrcweir      -- do nothing, the user cancelled the installation
91cdf0e10cSrcweir        return 2 --aborted by user
92cdf0e10cSrcweir  else if (choice as string) is chooseMyOwn then
93cdf0e10cSrcweir      -- yeah, one needs to use "choose file", otherwise
94cdf0e10cSrcweir      -- the user would not be able to select the .app
95cdf0e10cSrcweir      set the choice to POSIX path of (choose file with prompt chooseManual of type "com.apple.application-bundle" without showing package contents and invisibles)
96cdf0e10cSrcweir  end if
97cdf0e10cSrcweirelse if (get first item of found_ooos as string) is chooseMyOwn then
98cdf0e10cSrcweir  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)
99cdf0e10cSrcweir  if choice is false then
100cdf0e10cSrcweir      -- do nothing, the user cancelled the installation
101cdf0e10cSrcweir        return 2 --aborted by user
102cdf0e10cSrcweir  else if (choice as string) is chooseMyOwn then
103cdf0e10cSrcweir      -- yeah, one needs to use "choose file", otherwise
104cdf0e10cSrcweir      -- the user would not be able to select the .app
105cdf0e10cSrcweir      set the choice to POSIX path of (choose file with prompt chooseManual of type "com.apple.application-bundle" without showing package contents and invisibles)
106cdf0e10cSrcweir  end if
107cdf0e10cSrcweirelse if (get second item of found_ooos as string) is chooseMyOwn then
108cdf0e10cSrcweir  -- set choice to found installation
109cdf0e10cSrcweir  -- set the choice to (get first paragraph of found_ooos)
110cdf0e10cSrcweir  set the choice to (get first item of found_ooos)
111cdf0e10cSrcweirelse
112cdf0e10cSrcweir  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)
113cdf0e10cSrcweir  if choice is false then
114cdf0e10cSrcweir      -- do nothing, the user cancelled the installation
115cdf0e10cSrcweir        return 2 --aborted by user
116cdf0e10cSrcweir  else if (choice as string) is chooseMyOwn then
117cdf0e10cSrcweir      -- yeah, one needs to use "choose file", otherwise
118cdf0e10cSrcweir      -- the user would not be able to select the .app
119cdf0e10cSrcweir      set the choice to POSIX path of (choose file with prompt chooseManual of type "com.apple.application-bundle" without showing package contents and invisibles)
120cdf0e10cSrcweir  end if
121cdf0e10cSrcweirend if
122cdf0e10cSrcweir
123cdf0e10cSrcweir-- now only check whether the path is really from [PRODUCTNAME]
124cdf0e10cSrcweirtry
125cdf0e10cSrcweir    do shell script "grep '<string>[PRODUCTNAME] [PRODUCTVERSION]'   " & quoted form of (choice as string) & "/Contents/Info.plist"
126cdf0e10cSrcweiron error
127cdf0e10cSrcweir    display dialog (choice as string) & appInvalid buttons {InstallLabel} default button 1 with icon 0
128cdf0e10cSrcweir    return 3 --wrong target-directory
129cdf0e10cSrcweirend try
130cdf0e10cSrcweir
131*e88c2718SJim Jagielski-- A patch would break a sealed application. Test for the seal itself: codesign
132*e88c2718SJim Jagielski-- --display also accepts the linker's ad-hoc signature on arm64.
133d8849243SJim Jagielskitry
134*e88c2718SJim Jagielski    do shell script "test -e " & quoted form of ((choice as string) & "/Contents/_CodeSignature/CodeResources")
135d8849243SJim Jagielski    display dialog installSignedFailed buttons {OKLabel} default button 1 with icon 0
136d8849243SJim Jagielski    return 4
137d8849243SJim Jagielskion error
138d8849243SJim Jagielski    -- Preserve support for unsigned legacy installations.
139d8849243SJim Jagielskiend try
140d8849243SJim Jagielski
141cdf0e10cSrcweir(*
142cdf0e10cSrcweirdisplay dialog startInstall buttons {AbortLabel, InstallLabel} default button 2
143cdf0e10cSrcweir
144cdf0e10cSrcweirif (button returned of result) is AbortLabel then
145cdf0e10cSrcweir    return 2
146cdf0e10cSrcweirend if
147cdf0e10cSrcweir*)
148cdf0e10cSrcweir
149cdf0e10cSrcweirset tarCommand to "/usr/bin/tar -C " & quoted form of (choice as string) & " -xjf " & quoted form of sourcedir & "/tarball.tar.bz2"
150cdf0e10cSrcweirtry
151cdf0e10cSrcweir    do shell script tarCommand
152cdf0e10cSrcweir
153cdf0e10cSrcweiron error errMSG number errNUM
154cdf0e10cSrcweir    display dialog IdentifyQ buttons {IdentifyYES, IdentifyNO} with icon 2
155cdf0e10cSrcweir    if (button returned of result) is IdentifyYES then
156cdf0e10cSrcweir        try
157cdf0e10cSrcweir            do shell script tarCommand with administrator privileges
158cdf0e10cSrcweir        on error errMSG number errNUM
159cdf0e10cSrcweir            display dialog installFailed buttons {OKLabel} default button 1 with icon 0
160cdf0e10cSrcweir            -- -60005 username/password wrong
161cdf0e10cSrcweir            -- -128   aborted by user
162cdf0e10cSrcweir            -- 2 error from tar - tarball not found (easy to test)
163cdf0e10cSrcweir            return errNUM
164cdf0e10cSrcweir        end try
165cdf0e10cSrcweir    else
166cdf0e10cSrcweir        return 2 -- aborted by user
167cdf0e10cSrcweir    end if
168cdf0e10cSrcweirend try
169cdf0e10cSrcweir
170cdf0e10cSrcweirdisplay dialog installComplete buttons {OKLabel} default button 1
171