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