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