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