added version option
[PsN.git] / bin / setup.pl
blobb61fe25565f2d7871c1078857dde3369af179dc5
1 use strict;
2 use Config;
3 use CPAN;
4 use File::Spec;
6 my $version = '2.2.5_rc6';
8 my $name_safe_version = $version;
9 $name_safe_version =~ s/\./_/g;
10 my @utilities = ('bootstrap', 'cdd', 'execute', 'llp', 'scm', 'sumo', 'sse', 'mc_cdd',
11 'data_stats', 'create_extra_data_model', 'single_valued_columns',
12 'gam42toconf', 'create_cont_model', 'create_cont_data', 'unwrap_data', 'create_subsets',
13 'check_termination','se_of_eta','update_inits','npc','vpc' );
15 my @win_modules = ('Math::Random');
16 my @nix_modules = ('Math::Random','Storable');
18 my @modules;
20 sub confirm {
21 my $input = <STDIN>;
22 if( $input =~ /^\s*[yY]\s*$/ ){
23 return 1;
25 return 0;
28 sub get_input {
29 my $default = shift;
30 my $input = <STDIN>;
31 chomp( $input );
32 if( $input =~ /^\s*$/ ){
33 return( $default );
34 } else {
35 return( $input );
40 $| = 1; # Make sure autoflush is on
41 $, = ',';
42 print "\nThis is the PsN installer. I will install version $version of PsN Core,
43 Toolkit and the following utilities: \n", @utilities, ".
44 You will be presented with a few questions. Just pressing ENTER means
45 that you accept the default values, except for YES and NO questions,
46 in which you must be explicit.\n\n";
47 $, ='';
49 if( $Config{osname} eq 'linux' ){
50 my $user = `whoami`;
51 chomp($user);
52 unless( $user =~ /^root$/ ){
53 print "Hi $user, you don't look like root. You need root privileges to install PsN systemwide. Would you like to try anyway [y/n] ?\n";
54 exit unless( confirm() );
58 my $binary_dir;
59 my $library_dir;
60 my $perl_binary;
62 print "PsN Utilities installation directory [$Config{bin}]:";
64 $binary_dir = get_input( $Config{bin} );
66 unless( -e $binary_dir ){
67 print "Directory $binary_dir does not exist! Would you like to create it?[y/n]\n";
68 if( confirm() ){
69 unless( mkdir( $binary_dir ) ){ print "Unable to create $binary_dir!!\n"; die };
70 } else {
71 exit;
75 print "Path to perl binary used to run Utilities [$Config{perlpath}]:";
77 $perl_binary = get_input( $Config{perlpath} );
79 print "PsN Core and Toolkit installation directory [$Config{sitelib}]:";
81 $library_dir = get_input( $Config{sitelib} );
83 unless( -e $library_dir ){
84 print "Directory $library_dir does not exist! Would you like to create it? [y/n]\n";
85 if( confirm() ){
86 unless( mkdir( $library_dir ) ){ print "Unable to create $library_dir!!\n"; die };
87 } else {
88 exit;
92 my $overwrite = 0;
93 my $old_version = 'X_X_X';
94 my $keep_conf = 0;
96 unless( mkdir( "$library_dir/PsN_$name_safe_version" ) ){
97 print "Failed to create $library_dir/PsN_$name_safe_version: $!\n";
98 print "PsN is probably already installed. Would you like to continue anyway [y/n] ?";
99 exit unless( confirm() );
102 if( -e "$library_dir/PsN_$name_safe_version/psn.conf" ){
103 print "An older version of psn.conf exists in $library_dir/PsN_$name_safe_version.\n";
104 print "Keep the old version [y/n] ?";
105 $keep_conf = confirm();
108 my $copy_cmd;
109 my $copy_recursive_cmd;
111 if( $Config{osname} eq 'MSWin32' ){
112 $copy_cmd = "copy /Y";
113 $copy_recursive_cmd = "xcopy /Y /E";
114 @modules = @win_modules;
115 } else {
116 @modules = @nix_modules;
117 $copy_cmd = "cp";
118 $copy_recursive_cmd = "cp -r";
121 system( $copy_cmd . " \"" . File::Spec -> catfile( $library_dir, "PsN_$name_safe_version", "psn.conf" ) . "\" old.conf" ) if $keep_conf;
122 system( $copy_recursive_cmd . " " . File::Spec -> catfile( "lib", "*" ) . " \"" . File::Spec -> catdir( $library_dir, "PsN_$name_safe_version" ) . "\"" );
123 system( $copy_cmd . " old.conf \"" . File::Spec -> catfile( $library_dir, "PsN_$name_safe_version", "psn.conf" ) . "\"" ) if $keep_conf;
125 my $confirmed = 0;
126 foreach my $file ( @utilities ){
128 #system( $copy_cmd . " " . File::Spec -> catfile( "bin", $file ) . " " . File::Spec -> catfile( $binary_dir, "$file-$version" ) );
130 open( INST , "<" , File::Spec -> catfile( "bin", $file ) ) or die "Unable to install $file!\n";
131 open( UTIL , ">" , File::Spec -> catfile( $binary_dir, "$file-$version" ) ) or die "Unable to install $file!\n";
132 my $replace_line_found = 0;
133 while( <INST> ){
134 if( /\# Everything above this line will be replaced \#/ ){
135 print UTIL "\#!" , $perl_binary , "\n";
136 print UTIL "use lib '$library_dir';\n\n";
137 print UTIL "\# Everything above this line was entered by the setup script \#\n";
138 $replace_line_found = 1;
139 } elsif( /use PsN/ ){
140 print UTIL "use PsN_$name_safe_version;\n";
141 } elsif( /require PsN/ ){
142 print UTIL "require PsN_$name_safe_version;\n";
143 } elsif( $replace_line_found ){
144 print UTIL $_;
147 close UTIL;
148 close INST;
150 chmod( 0755, File::Spec -> catfile( $binary_dir, "$file-$version" ) );
152 if( -e "$binary_dir/$file" ){
154 if( $Config{osname} ne 'MSWin32' ){
155 my $link = readlink( "$binary_dir/execute" );
156 if( $old_version eq 'X_X_X' and not($link eq '') ) {
157 $link =~ /-(\d+\.\d+\.\d+)/;
158 $old_version = $1;
161 next if( $old_version eq $version );
163 if( not $confirmed ){
165 print( "\nAn older version($old_version) of PsN is installed. Would you like to\n",
166 "make this version ($version) the default? Your old version will not be\n",
167 "removed - it will be available as\n",
168 "[bootstrap|cdd|execute|llp|scm|sumo]-$old_version [y/n]" );
170 $confirmed = 1;
171 $overwrite = confirm();
173 if( $overwrite ){
174 unlink( "$binary_dir/$file" );
175 if( $Config{osname} eq 'MSWin32' ){
176 system( "copy /Y \"$binary_dir\\$file-$version\" \"$binary_dir\\$file\"" );
177 system( "copy /Y \"$Config{bin}\\runperl.bat\" \"$binary_dir\\$file.bat\"" );
178 } else {
179 symlink( "$binary_dir/$file-$version", "$binary_dir/$file" );
182 } else {
184 if( $Config{osname} eq 'MSWin32' ){
185 system( "copy /Y \"$binary_dir\\$file-$version\" \"$binary_dir\\$file\"" );
186 system( "copy /Y \"$Config{bin}\\runperl.bat\" \"$binary_dir\\$file.bat\"" );
187 } else {
188 symlink( "$binary_dir/$file-$version", "$binary_dir/$file" );
193 open( TEMPLATE, "lib/PsN_template.pm" ) or die "Unable to open PsN_template.pm in $library_dir: $!\n";
194 open( PSN, '>', "$library_dir" . "/PsN_$name_safe_version.pm" ) or die "Unable to install PsN-$name_safe_version.pm in $library_dir: $!\n";
196 if( $Config{osname} eq 'MSWin32' ){
197 require Win32;
198 $library_dir = Win32::GetShortPathName($library_dir);
201 print( PSN "package PsN;\n" );
202 print( PSN "use lib '$library_dir/PsN_$name_safe_version';\n" );
203 print( PSN "\$lib_dir = '$library_dir/PsN_$name_safe_version';\n" );
204 print( PSN "\$config_file = '$library_dir/PsN_$name_safe_version/psn.conf';\n" );
205 print( PSN "\$version = '$version';\n";
207 for ( <TEMPLATE> ) {
208 print PSN $_;
210 close( PSN );
211 close( TEMPLATE );
214 if( -e "$library_dir/PsN.pm" ){
215 if( $overwrite ){
216 unlink( "$library_dir/PsN.pm" );
217 if( $Config{osname} eq 'MSWin32' ){
218 system( "copy \"$library_dir\\PsN_$name_safe_version.pm\" \"$library_dir\\PsN.pm\"" );
219 } else {
220 symlink( "$library_dir/PsN_$name_safe_version.pm", "$library_dir/PsN.pm" );
223 } else {
224 if( $Config{osname} eq 'MSWin32' ){
225 system("copy \"$library_dir\\PsN_$name_safe_version.pm\" \"$library_dir\\PsN.pm\"" );
226 } else {
227 symlink( "$library_dir/PsN_$name_safe_version.pm", "$library_dir/PsN.pm" );
231 open( PSN, '<', "$library_dir" . "/PsN_$name_safe_version/nonmem.pm" ) or die "Unable to install PsN-$name_safe_version/nonmem.pm in $library_dir: $!\n";
233 my @nonmem_pm = <PSN>;
235 close( PSN );
237 open( PSN, '>', "$library_dir" . "/PsN_$name_safe_version/nonmem.pm" ) or die "Unable to install PsN-$name_safe_version/nonmem.pm in $library_dir: $!\n";
239 for( @nonmem_pm ){
240 if( /require PsN/ ){
241 print PSN "require PsN_$name_safe_version;\n";
242 } else {
243 print PSN;
246 close PsN;
247 print "\nPsN has been successfully installed.
249 The next step is to install module dependencies. If you
250 know that all dependencies are installed or you'd
251 like to install them yourself, then you are finished now.
253 Would you like this script to install dependencies [y/n]?";
255 if(confirm()){
257 # my $inst = ExtUtils::Installed->new();
258 # my @modules = $inst -> modules();
260 # my %modules;
262 # foreach my $module ( @modules ){
263 # $modules{$module} = 1;
266 foreach my $module( @modules ){
268 my $mod_file = $module;
269 $mod_file =~ s/::/-/g;
270 print "\nInstalling $mod_file\n", "=" x 80, "\n";
271 if( $Config{osname} eq 'MSWin32' ){
272 if( -e "modules/$mod_file-Win" ){
273 chdir( "modules/$mod_file-Win" );
274 system( "ppm install $mod_file.ppd" );
275 chdir( "../.." );
276 } else {
277 print( "$module might not be installed and you don't have a source distribution. Would you like to try installing using ppm over internet?[y/n]" );
278 next unless( confirm() );
279 system( "ppm install $mod_file" );
281 } elsif ( -e "modules/$mod_file" ){
282 chdir( "modules/$mod_file" );
283 system( "$Config{bin}/perl Makefile.PL" );
284 system( "make install" );
285 chdir( "../.." );
286 } else {
287 print( "$module might not be installed and you don't have a source distribution. Would you like to try installing from CPAN?[y/n]" );
288 next unless( confirm() );
289 CPAN::install( $module );
291 print "=" x80, "\n";
295 print "\nInstallation complete!\n";
296 print "\nNow you should edit $library_dir/PsN_$name_safe_version/psn.conf so that PsN can find your NONMEM installations.\n\nPress the ENTER key to finish.";
298 confirm(); # Wait here so windows users see the message
300 # Uninstall ?
301 # rm -rf /usr/lib/perl5/site_perl/5.8.4/PsN* /usr/bin/*-2.1.?.pl /usr/bin/execute.pl /usr/bin/llp.pl /usr/bin/cdd.pl /usr/bin/bootstrap.pl /usr/bin/scm.pl /usr/bin/sumo.pl