aboutsummaryrefslogtreecommitdiff
path: root/src/installer/setup.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/installer/setup.cpp')
-rwxr-xr-xsrc/installer/setup.cpp353
1 files changed, 353 insertions, 0 deletions
diff --git a/src/installer/setup.cpp b/src/installer/setup.cpp
new file mode 100755
index 0000000..8083510
--- /dev/null
+++ b/src/installer/setup.cpp
@@ -0,0 +1,353 @@
1// This is a very rudimentary installer framework-
2// it won't do much more than copy a few files to the proper location.
3// Of course there are a few wizard screens which are made in fluid (FLTK),
4// these can be altered as needed.
5// A small portion of the actual install stuff is done in the READY screen
6// (search for it in the code), this could still be moved out to the
7// installer_resources.h file.
8// At this moment, desktop shortcuts are not yet created, for more info
9// read
10// http://delphi.about.com/od/windowsshellapi/a/create_lnk.htm (shell links)
11#include <FL/Fl.H>
12#include <FL/Fl_Window.H>
13#include <FL/Fl_Box.H>
14#include "ui_welcome.h"
15#include "ui_license.h"
16#include "ui_instdir.h"
17#include "ui_ready.h"
18#include "setupapi.cpp"
19#include "installer_resources.h"
20#include <iostream>
21#include <fstream>
22#include <string>
23#ifdef WINDOWS
24#define DEFAULT_INSTDIR "C:\\HD24tools\\"
25#include <windows.h>
26#include <FL/x.H>
27#define IDI_ICON1 101
28#endif
29#ifndef WINDOWS
30#define DEFAULT_INSTDIR "/usr/local/bin/HD24tools/"
31#endif
32#include <sys/types.h>
33#include <sys/stat.h>
34#include <fcntl.h>
35#ifndef WINDOWS
36#include <unistd.h>
37#endif
38
39#define ALLOW_OVERWRITE true
40#define NO_OVERWRITE false
41
42#ifndef S_IXGRP
43#define S_IXGRP 0
44#endif
45#ifndef S_IXOTH
46#define S_IXOTH 0
47#endif
48bool confirm(const char* question)
49{
50 int result=fl_choice(question,"No","Yes",NULL);
51 if (result==1) {
52 return true;
53 }
54return false;
55}
56
57int lastx=0;
58int lasty=0;
59string fullbinname="";
60int installer_writefile(unsigned char* filedata,long long filesize,const char* instdir,const char* filename,bool allow_overwrite)
61{
62 string fname=instdir;
63 fname+=filename;
64 fstream binfile(fname.c_str(),ios::binary|ios::out);
65 binfile.write(reinterpret_cast<char *>(filedata),filesize);
66 binfile.close();
67#ifndef WINDOWS
68 chmod(fname.c_str(),S_IRWXU|S_IXGRP|S_IRGRP|S_IXOTH|S_IROTH);
69#endif
70 return 0;
71}
72void installer_getsysdir(char* sysdir)
73{
74 // on Windows: GetSystemDirectory()
75}
76void installer_getprogdir(char* progdir)
77{
78 // on Windows: getProgramDirectory()
79}
80int main(int argc, char **argv,char *envp[]) {
81 UI_Welcome ui_welcome;
82 UI_License ui_license;
83 UI_Instdir ui_instdir;
84 char* instdir=new char[4096];
85 instdir[0]='\0';
86 bool done=false;
87 char* currscreen;
88 currscreen=new char[9];
89 currscreen="welcome";
90#ifndef WINDOWS
91 // on non-windows systems, default to user home dir
92 // (on non windows or no home found,
93 // defaults to systemwide dir)
94 int count=0;
95 char homedir[255];
96 homedir[0]='\0';
97 while (envp[count] != NULL)
98 {
99 if (strncmp(envp[count],"HOME=",5)==0)
100 {
101 strncpy(&homedir[0],&envp[count][5],250);
102 }
103 ++count;
104 }
105
106 if (strlen(homedir)>0)
107 {
108 strncpy(&instdir[0],&homedir[0],128);
109 int dirlen=strlen(instdir)-1;
110 if ((instdir[dirlen]!='/')
111 &&(instdir[dirlen]!='\\'))
112 {
113#ifdef WINDOWS
114 instdir[dirlen+1]='\\';
115 instdir[dirlen+2]='\0';
116#else
117 instdir[dirlen+1]='/';
118 instdir[dirlen+2]='\0';
119#endif
120 }
121 dirlen=strlen(instdir)-1;
122 strcat(instdir,"HD24tools/");
123 }
124#endif
125 int insterror=0;
126 int result=0;
127 UI_Ready ui_ready;
128 char* sysdir=new char[4096];
129 installer_getsysdir(&sysdir[0]);
130 while (done==false) {
131 if (strncmp(currscreen,"done",4)==0)
132 {
133 done=true;
134 continue;
135 }
136 if (strncmp(currscreen,"welcome",7)==0)
137 {
138 Fl_Window *window = ui_welcome.make_window(&currscreen[0]);
139#ifdef WINDOWS
140 window->icon((char *)LoadIcon(fl_display,MAKEINTRESOURCE(IDI_ICON1)));
141#endif
142 window->position(lastx,lasty);
143 window->end();
144 window->show(); //argc, argv);
145 Fl::run(); lastx=window->x(); lasty=window->y();
146
147 currscreen = ui_welcome.currscreen;
148 if (strncmp(currscreen,"next",4)==0) {
149 currscreen="license\0";
150 continue;
151 }
152 if (strncmp(currscreen,"cancel",4)==0) {
153 currscreen="done\0";
154 continue;
155 }
156 // screen closed otherwise
157 currscreen="done\0";
158 continue;
159 }
160 if (strncmp(currscreen,"license",7)==0)
161 {
162 Fl_Window *window = ui_license.make_window(&currscreen[0]);
163#ifdef WINDOWS
164 window->icon((char *)LoadIcon(fl_display,MAKEINTRESOURCE(IDI_ICON1)));
165#endif
166 window->position(lastx,lasty);
167 window->end();
168 window->show(); //argc, argv);
169 Fl::run(); lastx=window->x(); lasty=window->y();
170 currscreen = ui_license.currscreen;
171 if (strncmp(currscreen,"back",4)==0) {
172 currscreen="welcome\0";
173 continue;
174 }
175 if (strncmp(currscreen,"next",4)==0) {
176 currscreen="instdir\0";
177 continue;
178 }
179 if (strncmp(currscreen,"cancel",4)==0) {
180 currscreen="done\0";
181 continue;
182 }
183 // screen closed otherwise
184 currscreen="done\0";
185 continue;
186 }
187 if (strncmp(currscreen,"instdir",7)==0)
188 {
189 if (instdir[0]=='\0')
190 {
191 strncpy(&instdir[0],DEFAULT_INSTDIR,128);
192 }
193 Fl_Window *window = ui_instdir.make_window(&currscreen[0],&instdir[0]);
194#ifdef WINDOWS
195 window->icon((char *)LoadIcon(fl_display,MAKEINTRESOURCE(IDI_ICON1)));
196#endif
197 window->position(lastx,lasty);
198 window->end();
199 window->show(); //argc, argv);
200 Fl::run(); lastx=window->x(); lasty=window->y();
201 currscreen = ui_instdir.currscreen;
202 instdir = ui_instdir.instdir;
203 int dirlen=strlen(instdir)-1;
204 if ((instdir[dirlen]!='/')
205 &&(instdir[dirlen]!='\\'))
206 {
207#ifdef WINDOWS
208 instdir[dirlen+1]='\\';
209 instdir[dirlen+2]='\0';
210#else
211 instdir[dirlen+1]='/';
212 instdir[dirlen+2]='\0';
213#endif
214 }
215 if (strncmp(currscreen,"cancel",6)==0) {
216 currscreen="done\0";
217 continue;
218 }
219 if (strncmp(currscreen,"back",4)==0) {
220 currscreen="license\0";
221 continue;
222 }
223 if (!setupDirExists(instdir))
224 {
225 bool mustcreate=confirm("Install directory does not exist. Create it?");
226 if (!mustcreate) {
227 currscreen="instdir\0";
228 continue;
229 }
230 setupCreateDir(instdir,0755);
231
232 if (!(setupDirExists(instdir)))
233 {
234 fl_message("Could not create install directory.\nPlease create it manually, then try again.");
235 currscreen="instdir\0";
236 continue;
237 }
238 }
239 if (strncmp(currscreen,"next",4)==0) {
240 currscreen="ready\0";
241 continue;
242 }
243 // screen closed otherwise
244 currscreen="done\0";
245 continue;
246 }
247 if (strncmp(currscreen,"ready",5)==0)
248 {
249 // READY
250 cout << "actually doing the install now" << endl;
251 insterror=0;
252 result=installer_writefile(
253 resource_hd24tools_manual,sizeof(resource_hd24tools_manual),(const char*)instdir,resource_hd24tools_manual_filename,ALLOW_OVERWRITE);
254 if (result!=0) { insterror=1; currscreen="done\0"; continue; }
255 result=installer_writefile(
256 resource_longliverec,sizeof(resource_longliverec),(const char*)instdir,resource_longliverec_filename,ALLOW_OVERWRITE);
257 if (result!=0) { insterror=1; currscreen="done\0"; continue; }
258 result=installer_writefile(
259 resource_unquickformat,sizeof(resource_unquickformat),instdir,resource_unquickformat_filename,ALLOW_OVERWRITE);
260 if (result!=0) { insterror=1; currscreen="done\0"; continue; }
261#ifdef LINUX
262 /* for linux, wrap the binary into a shell script
263 * that prepends the application dir to the library
264 * search path */
265 fullbinname=resource_hd24connect_filename;
266 fullbinname+="_bin";
267 result=installer_writefile(
268 resource_hd24connect,sizeof(resource_hd24connect),(const char*)instdir,fullbinname.c_str(),ALLOW_OVERWRITE);
269 fullbinname=instdir;
270 fullbinname+=resource_hd24connect_filename;
271 fstream wrapper(fullbinname.c_str(),ios::out);
272 wrapper << "#!/bin/sh" << endl;
273 wrapper << "export LD_LIBRARY_PATH=" << instdir <<":$LD_LIBRARY_PATH" << endl;
274 wrapper << "exec " << instdir << "hd24connect_bin $*" << endl;
275 wrapper.close();
276 chmod(fullbinname.c_str(),S_IRWXU|S_IXGRP|S_IRGRP|S_IXOTH|S_IROTH);
277#endif
278#ifdef WINDOWS
279 result=installer_writefile(
280 resource_hd24connect,sizeof(resource_hd24connect),(const char*)instdir,resource_hd24connect_filename,ALLOW_OVERWRITE);
281#endif
282#ifdef DARWIN
283 /* For MacOS, wrap the binary into a .app bundle.
284 * Note: What is called LD_LIBRARY_PATH under Linux
285 * is called DYLD_LIBRARY_PATH on MacOSX
286 */
287 string bundledir=instdir;
288 bundledir+="hd24connect.app/";
289 setupCreateDir(bundledir.c_str(),0755);
290 bundledir+="Contents/";
291 setupCreateDir(bundledir.c_str(),0755);
292 string macosdir;
293 macosdir=bundledir+"MacOS/";
294 setupCreateDir(macosdir.c_str(),0755);
295 string resourcedir=bundledir+"Resources/";
296 setupCreateDir(resourcedir.c_str(),0755);
297 string strplist="Info.plist";
298 string strpkginfo="PkgInfo";
299
300 result=installer_writefile(resource_plist_connect,sizeof(resource_plist_connect),bundledir.c_str(),"info.plist",ALLOW_OVERWRITE);
301 result=installer_writefile(resource_pkginfo_connect,sizeof(resource_pkginfo_connect),bundledir.c_str(),strpkginfo.c_str(),ALLOW_OVERWRITE);
302 result=installer_writefile(resource_hd24connect,sizeof(resource_hd24connect),macosdir.c_str(),resource_hd24connect_filename,ALLOW_OVERWRITE);
303
304
305#endif
306 if (result!=0) { insterror=1; currscreen="done\0"; continue; }
307 result=installer_writefile(
308 resource_hd24hexview,sizeof(resource_hd24hexview),(const char*)instdir,resource_hd24hexview_filename,ALLOW_OVERWRITE);
309 if (result!=0) { insterror=1; currscreen="done\0"; continue; }
310 result=installer_writefile(
311 resource_libsndfile_1,sizeof(resource_libsndfile_1),(const char*)instdir,resource_libsndfile_1_filename,ALLOW_OVERWRITE);
312 if (result!=0) { insterror=1; currscreen="done\0"; continue; }
313// result=installer_writefile(
314// resource_portaudio,sizeof(resource_portaudio),(const char*)instdir,resource_portaudio_filename,NO_OVERWRITE);
315// if (result!=0) { insterror=1; currscreen="done\0"; continue; }
316
317 Fl_Window *window = ui_ready.make_window(&currscreen[0]);
318#ifdef WINDOWS
319 window->icon((char *)LoadIcon(fl_display,MAKEINTRESOURCE(IDI_ICON1)));
320#endif
321 window->position(lastx,lasty);
322 window->end();
323 window->show(); //argc, argv);
324 Fl::run(); lastx=window->x(); lasty=window->y();
325
326 currscreen = ui_ready.currscreen;
327 if (strncmp(currscreen,"back",4)==0) {
328 currscreen="license\0";
329 continue;
330 }
331 if (strncmp(currscreen,"next",4)==0) {
332 currscreen="doinst\0";
333 continue;
334 }
335 if (strncmp(currscreen,"cancel",4)==0) {
336 currscreen="done\0";
337 continue;
338 }
339 // screen closed otherwise
340 cout << "???" << currscreen << endl;
341 currscreen="done\0";
342 continue;
343 }
344 if (strncmp(currscreen,"doinst",6)==0)
345 {
346 currscreen="done\0";
347 continue;
348 }
349 cout << "Unknown screen '" << currscreen << "'." << endl;
350 done=true;
351 }
352 return 0;
353}