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