summaryrefslogtreecommitdiffstats
path: root/win/install_windows.pl
blob: 44a2316c6fc9281b05de5b1d2e4c76e53bb42db1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
# GPLIGC Installer Windows
# $Id: install_windows.pl 9 2014-08-01 10:52:42Z kruegerh $

use Win32;
use Win32::TieRegistry(Delimiter=>"/");
use File::Copy;

$email ="Hannes.Krueger\@gmail.com";

$close_delay = 60;


print <<ENDE;

GPLIGC and OGIE installation (Windows XP/Vista)
    should work on Win2003, Win2008 and Win7 too, but was not tested yet!

(c) 2000-2014 by Hannes Krueger ($email)
read the GPLIGC_Manual.pdf for more information

Enter installation directory. Press enter for default.

ENDE


# Ask for installation directory
installdirinput:
print "[c:\\GPLIGC]:  ";
chomp($installdir=<>);

if ($installdir eq "") {$installdir="c:\\GPLIGC";}

if ($installdir !~ /(^.:\\)/) {print "give path with drive letter\n"; goto installdirinput;}

if ($installdir =~ /[;,!|<>\@\%\&\$\*\?\+]/) {   # ~ are allowed. (8.3 aliases as progra~1) # \s removed, trying whitespaces...
    print "Do not use whitespaces* and/or special characters in the PATH!\n";  #
    goto installdirinput;
}


# check for old installation and configfile
$prev_install = 0;

if (-d $installdir) {
    print "\n$installdir already exists... \n";


    if (-f $installdir."/ogie.ini") {
	print "Configurationfile (ogie.ini) detected... The new default configfile will be installed as ogie-new.ini\n";
	$prev_install=1;
	# move configfile to .old to avoid replacing
	move("$installdir/ogie.ini", "$installdir/ogie-old.ini") || failed_cp();
    }


    if (-f $installdir."/opengligcexplorer.ini") {
	print "Configurationfile (opengligcexplorer.ini) detected... The new default configfile will be installed as ogie-new.ini\n";
	$prev_install=1;
	# move configfile to .old to avoid replacing
	move("$installdir/opengligcexplorer.ini", "$installdir/ogie-old.ini") || failed_cp();
    }

}


# copying files
print "\ncopying files...\n";
my $cmd = "xcopy /f /r /i /e * \"$installdir\" ";
system($cmd) && failed_cp();
print "Finished copying files...\n";


# swap configfiles
if ($prev_install) {
    print "new default config file (ogie.ini) will be installed as ogie-new.ini\n";
    move("$installdir/ogie.ini", "$installdir/ogie-new.ini") || failed_cp();
    move("$installdir/ogie-old.ini", "$installdir/ogie.ini") || failed_cp();
}


#################################################

# setting environment variables
print "\nTrying to set environment variables GPLIGCHOME and PATH\n";

# check for OS
($osname, $edition) = Win32::GetOSName();
print "\nYour operating system is: \"$osname\"  \"$edition\" \n";


@autoexec_OSlist = qw(Win95 Win98 WinME);  # these OS's require ENV setting via autoexec.bat

# these OS's require ENV setting via registry (not sure whether 2008/Win7 have the same structure of the registry... I hope so.
@registry_OSlist = qw(WinNT Win2000 Win2003 WinHomeSrv WinXP WinVista Win2008 Win7 Win8);

# untested, will try to use registy anyway
@untested_OSlist = qw(Win2003 WinHomeSrv Win2008 Win10);

foreach (@autoexec_OSlist) {
    if ($osname =~ /$_/i) {
      print "Sorry, $osname is not longer supported! Maybe it will work anyway..?\n";
      print "GPLIGC/OGIE will be installed anyway.\n";
      autoexec(); }
}

foreach (@registry_OSlist) {
    if ($osname =~ /$_/i) { registry(); }
# ups NT seems to be still ok with cygwin 1.7
#    if ($osname eq "WinNT") {
#      print "Sorry, $osname is not longer supported (by cygwin). Consequently OGIE will not work!\n";
#    }
}

foreach (@untested_OSlist) {
    if ($osname =~ /$_/i) {
      untested();
      registry();
      }
}

print "Sorry, I did not recognize your Windows version...\n";
print "You should write an Email to $email and tell me this: ->$osname<--->$edition<- \n";

failed_env();
install_end();


##############################################


# this adds some settings to AUTOEXEC.BAT
# win95 / 98 / ME
sub autoexec {

    my $path_set = 0;

    if ($ENV{winbootdir}) {

	my $autoexec = substr($ENV{winbootdir},0,2) .'\autoexec.bat';

	# make it writable if necessary
	if (-e $autoexec && ! -w $autoexec) {
    	    chmod 0755, $autoexec;
	}


	my @autoexec_bat;

	# read autoexec.bat into @autoexec_bat
	if (open(AUTOR, "<$autoexec")) {
		     @autoexec_bat = <AUTOR>;
	} else { warn "Cannot read autoexec.bat\n"; failed_env(); install_end(); }


	# alter autoexec_bat in mem
	$alter = 0;
	$home_line = 0;
	for ($i=0; $i<=$#autoexec_bat; $i++) {

	    	if ( $autoexec_bat[$i] =~ /GPLIGCHOME/ ) {
		    print "Found this line in autoexec.bat: $autoexec_bat[$i]\n";
		    $autoexec_bat[$i] = "SET GPLIGCHOME=$installdir\n";
		    $alter = 0.5; # half is done
		    $home_line = $i;
		    print "Changed to: $autoexec_bat[$i]\n";

		    # prev line is probably the PATH line added by old installscript
		    if ($autoexec_bat[$i-1] =~ /PATH/ ) {
			print "Found this line in autoexec.bat: $autoexec_bat[$i-1]\n";
			$autoexec_bat[$i-1] = "SET PATH=$installdir;%PATH%\n";
			$alter = 1; # everything is done
			print "Changed to: $autoexec_bat[$i-1]\n";
		    }

		}
	}


	# hmmm, maybe we set the path a second time now...
	# (if previous install wasnt made by the install-script!)
	if ($alter == 0.5) {
	    splice(@autoexec_bat,$home_line,0, "SET PATH=$installdir;%PATH%\n");
	    print "The line SET PATH=$installdir;%PATH% was inserted before the line containing SET GPLIGCHOME\n";


	}

	if ($alter == 0) {
	    push(@autoexec_bat, "SET PATH=$installdir;%PATH%\n");
	    push(@autoexec_bat, "SET GPLIGCHOME=$installdir\n");
	    print "Environment varibles are set!\n";
	}


	# write back autoexec.bat
	if (open(AUTOW, ">$autoexec")) {

	    print "writing back $autoexec ... \n";

	    # write autoexec_bat
	    foreach (@autoexec_bat) {
		print AUTOW "$_";
	    }

	    close AUTOW;
            $path_set=1;

	} else {
    	    warn "Unable to open $autoexec for writing: $!\n";
	    failed_env();
	    install_end();
        }

    } else {
	warn "No winbootdir environment variable found.\n";
	failed_env();
	install_end();
    }


    unless ($path_set) {
        failed_env();
	install_end();
    }

    print "\nInstallation finished\nReboot to make the new settings in AUTOEXEC.BAT take effect\n";
    install_end();

}


# this sets the path in the registry
sub registry {

## I'm not sure if on every (2000/NT4/XP) platform the environment is
## in the same place in registry....

    print "Modifying registry...\n";


    #$environment = $Registry->{"LMachine/System/CurrentControlSet/Control/Session Manager/Environment/"};
    $environment = $Registry->{"CUser/Environment/"};

    my $path = $environment->{"/Path"};
    #print "Path : $path \n";


    # nur wenn nicht schon drinnen /\: in $installdir????
    $quotet_installdir=quotemeta($installdir);

    if ($path !~ /$quotet_installdir/) {
	# we have to put the new path before the rest (maybe there is an old installation?!
	$path = $installdir.";".$path;
	print "$installdir added to Path \n";
    } else { print "Path already set...\n"; }


    #setting environment variables in Registry

    $environment->{"/Path"} = $path;
    $environment->{"/GPLIGCHOME"} = $installdir;

    $path_new = $environment->{"/Path"};
    $installdir_new = $environment->{"/GPLIGCHOME"};

    # check the reread values
    my $failed = 0;
    if ($path eq $path_new) {
	print "\nPath is updated in HKEY_CURRENT_USER/Environment\n";
    } else {
	print "\nSetting Path in HKEY_CURRENT_USER/Environment _failed_ !!!\n";
	$failed = 1;

    }

    if ($installdir eq $installdir_new) {
	print "GPLIGCHOME is set in HKEY_CURRENT_USER/Environment\n";
    } else {
	print "Setting GPLIGCHOME in HKEY_CURRENT_USER/Environment _failed_ !!!\n";
	$failed = 1;
    }

    if ($failed == 0) {
        print "\nEnvironment Variables are set for current user!\n";
	print "Log out and in to make changes in Registry take effect\n";
	print "\nInstallation finished\n";
    } else {
	failed_env();
    }

    install_end();
}


##############################################


sub install_end {
    print "Window will be closed in $close_delay seconds.\n";
    sleep $close_delay;
    exit ;
}

sub failed_env {
    print "\Setting the environment variables failed.\n";
    print "You need to set the enviroment variable GPLIGCHOME to $installdir\n";
    print "and add $installdir to PATH by yourself.\n\n";
    print "Hhow to do this is described in the GPLIGC-manual.\n";
    $close_delay = 240;
}

sub failed_cp {
    print "\nInstallation failed!";
    print "Failure while copying or moving files... Maybe you do not have write permissions for $installdir ?!\n";
    print "If you cannot solve this feel free to contact me $email\n";
    $close_delay = 240;
    install_end();
}

sub untested {
    print "\nGPLIGC/OGIE have not yet been tested on your Windows platform!\n";
    print "Please check whether the installation was succesfull and write my a short notice\n";
    print ">>> $email \n";
    print "provide this line with your report: >>>$osname<<< >>>$edition<<< \n";
    print "Thank you very much!\n";
    #$close_delay = 240;
}