]> git.donarmstrong.com Git - biopieces.git/blob - bp_scripts/biopieces_installer-0.42.sh
fixed bugs in installer
[biopieces.git] / bp_scripts / biopieces_installer-0.42.sh
1 #!/bin/bash
2
3 # Install script for Biopieces.
4 # October 2011, Martin A. Hansen
5
6 bp_code="$HOME/biopieces"
7 bp_data="$HOME/BP_DATA"
8  bp_log="/var/log"
9  bp_tmp="/tmp"
10
11 # Function that renders a given message in ASCII green.
12 function echo_green
13 {
14     local msg=$1
15
16     echo -e "\033[32;38m$msg\033[0m"
17 }
18
19 # Function that renders a given message in ASCII yellow.
20 function echo_yellow
21 {
22     local msg=$1
23
24     echo -e "\033[33;38m$msg\033[0m"
25 }
26
27 # Function that renders a given message in ASCII red.
28 function echo_red
29 {
30     local msg=$1
31
32     echo -e "\033[31;38m$msg\033[0m"
33 }
34
35 # Function to output an abort message and exit
36 function exit_abort
37 {
38     echo_red "abort"
39
40     exit
41 }
42
43 # Function to output an success message and exit
44 function exit_success
45 {
46     echo ""
47     echo_green "Congratulations - you have now installed Biopieces."
48     echo ""
49     echo "   To list all available Biopieces try 'list_biopieces'"
50     echo ""
51     echo "   To see the synopsis of a Biopiece try 'read_fastq'"
52     echo ""
53     echo "   To see the full description and exampels try 'read_fastq -?'"
54     echo ""
55     echo "   Don't forget to join the Biopieces Google Group for important"
56     echo "   messages, questions, discussion, and suggestions:"
57     echo ""
58     echo "      http://groups.google.com/group/biopieces"
59     echo ""
60     echo "   And of cause there is the introduction:"
61     echo ""
62     echo "      http://code.google.com/p/biopieces/wiki/Introduction"
63     echo ""
64     echo "   Happy hacking!"
65     echo ""
66
67     exit
68 }
69
70 # Function to create a directory if it doesn't exist.
71 function dir_create
72 {
73     local dir=$1
74
75     echo -n "Creating $dir: "
76
77     if error=$( mkdir $dir 2>&1 ); then
78         echo_green "OK"
79     else
80         echo_red "FAIL"
81         echo "   $error"
82         exit_abort
83     fi
84 }
85
86 # Function check if a directory is writable.
87 function dir_writable
88 {
89     local dir=$1
90     local file="$dir/bp_writable"
91
92     echo -n "Writable? $dir: "
93
94     if error=$( touch $file 2>&1 ); then
95         echo_green "OK"
96         rm $file
97     else
98         echo_red "FAIL"
99         echo "   $error"
100         exit_abort
101     fi
102 }
103
104 # Function to prompt for continuation of installation.
105 function prompt_install
106 {
107     echo ""
108     echo "Welcome to the Biopieces installer."
109     echo ""
110     echo "   This installer is experimental, and is being evaluated to replace"
111     echo "   the previous, tedious way:"
112     echo ""
113     echo "      http://code.google.com/p/biopieces/wiki/Installation"
114     echo ""
115     echo "   The installer will now do the following:"
116     echo ""
117     echo "   *  Check for existing Biopieces installation."
118     echo "   *  Check prerequisites:"
119     echo "      -  Subversion client"
120     echo "      -  Perl"
121     echo "      -  Perl modules"
122     echo "      -  Ruby"
123     echo "      -  Ruby gems"
124     echo "      -  Auxillary programs"
125     echo ""
126     echo "   *  Create installation directories."
127     echo "   *  Download code from repository."
128     echo "   *  Set environment in .bashrc."
129     echo "   *  Run tests"
130     echo ""
131     echo "   Problems? Check out the FAQ:"
132     echo ""
133     echo "      http://code.google.com/p/biopieces/wiki/FAQ"
134     echo ""
135     echo "   Help is available at the Biopieces Google Group:"
136     echo ""
137     echo "      http://groups.google.com/group/biopieces"
138     echo ""
139     echo "   Bugs & issues:"
140     echo ""
141     echo "      http://code.google.com/p/biopieces/issues/list"
142     echo ""
143
144     while true; do
145         read -p "Continue (yes/no)? " answer
146         case $answer in
147             [Yy]* ) break;;
148             [Nn]* ) exit_abort;;
149         esac
150     done
151 }
152
153 # Function to prompt the checking of any existing Biopieces installation.
154 function prompt_install_existing
155 {
156     echo "Checking for existing Biopieces installation:"
157
158     if [ $BP_DIR ]; then
159         echo_yellow "   WARNING: \$BP_DIR is already set to: $BP_DIR"
160         found=1
161     fi
162
163     if [ $BP_DATA ]; then
164         echo_yellow "   WARNING: \$BP_DATA is already set to: $BP_DATA"
165         found=1
166     fi
167
168     if [ $BP_TMP ]; then
169         echo_yellow "   WARNING: \$BP_TMP is already set to: $BP_TMP"
170         found=1
171     fi
172
173     if [ $BP_LOG ]; then
174         echo_yellow "   WARNING: \$BP_LOG is already set to: $BP_LOG"
175         found=1
176     fi
177
178     if [ $found ]; then
179         echo ""
180         echo "   An old installation of Biopeices appears to exists."
181     else
182         echo ""
183         echo "   No installation of Biopeices found."
184     fi
185
186     while true; do
187         read -p "Continue (yes/no)? " answer
188         case $answer in
189             [Yy]* ) break;;
190             [Nn]* ) exit_abort;;
191         esac
192     done
193 }
194
195 # Function to prompt the testing of prerequisites.
196 function prompt_test_prerequisites
197 {
198     echo "Testing prerequisites:"
199
200     test_bash
201     test_svn
202     test_perl
203     test_perl_module "Inline"
204     test_perl_module "JSON::XS"
205     test_perl_module "SVG"
206     test_perl_module "Bit::Vector"
207     test_perl_module "Time::HiRes"
208     test_ruby
209     test_ruby_gem "gnuplot"
210     test_ruby_gem "narray"
211     test_aux_program "blastall"
212     test_aux_program "blat"
213     test_aux_program "bwa"
214     test_aux_program "bowtie"
215     test_aux_program "formatdb"
216     test_aux_program "gnuplot"
217     test_aux_program "idba"
218     test_aux_program "muscle"
219     test_aux_program "mummer"
220     test_aux_program "mysql"
221     test_aux_program "prodigal"
222     test_aux_program "Ray"
223     test_aux_program "scan_for_matches"
224     test_aux_program "uclust"
225     test_aux_program "velveth"
226     test_aux_program "velvetg"
227     test_aux_program "vmatch"
228
229     echo ""
230     echo "   Any WARNINGs indicate that the executable for that auxillary"
231     echo "   program could not be found. While not critical, this will"
232     echo "   cause some Biopieces to FAIL."
233
234     while true; do
235         read -p "Continue (yes/no)? " answer
236         case $answer in
237             [Yy]* ) break;;
238             [Nn]* ) exit_abort;;
239         esac
240     done
241 }
242
243 # Function to prompt the selection of the code directory.
244 function prompt_install_dir_code
245 {
246     read -p "Enter directory for the Biopieces code (default: $bp_code): " answer;
247
248     bp_code=${answer:-"$bp_code"}
249
250     if [ ! -d "$bp_code" ]; then
251         while true; do
252             read -p "Create directory: $bp_code (yes/no)? " answer
253             case $answer in
254                 [Yy]* ) dir_create $bp_code && break;;
255                 [Nn]* ) exit_abort;;
256             esac
257         done
258     fi
259
260     dir_writable $bp_code
261 }
262
263 # Function to prompt the selection of the data directory.
264 function prompt_install_dir_data
265 {
266     read -p "Enter directory for the Biopieces data (default: $bp_data): " answer;
267
268     bp_data=${answer:-"$bp_data"}
269
270     if [ ! -d "$bp_data" ]; then
271         while true; do
272             read -p "Create directory: $bp_data (yes/no)? " answer
273             case $answer in
274                 [Yy]* ) dir_create $bp_data && break;;
275                 [Nn]* ) exit_abort;;
276             esac
277         done
278     fi
279
280     dir_writable $bp_data
281 }
282
283 # Function to prompt the selection of the log directory.
284 function prompt_install_dir_log
285 {
286     read -p "Enter directory for the Biopieces log file (default: $bp_log): " answer;
287
288     bp_log=${answer:-"$bp_log"}
289
290     if [ ! -d "$bp_log" ]; then
291         while true; do
292             read -p "Create directory: $bp_log (yes/no)? " answer
293             case $answer in
294                 [Yy]* ) dir_create $bp_log && break;;
295                 [Nn]* ) exit_abort;;
296             esac
297         done
298     fi
299
300     dir_writable $bp_log
301 }
302
303 # Function to prompt the selection of the tmp directory.
304 function prompt_install_dir_tmp
305 {
306     read -p "Enter directory for the Biopieces temporary files (default: $bp_tmp): " answer;
307
308     bp_tmp=${answer:-"$bp_tmp"}
309
310     if [ ! -d "$bp_tmp" ]; then
311         while true; do
312             read -p "Create directory: $bp_tmp (yes/no)? " answer
313             case $answer in
314                 [Yy]* ) dir_create $bp_tmp && break;;
315                 [Nn]* ) exit_abort;;
316             esac
317         done
318     fi
319
320     dir_writable $bp_tmp
321 }
322
323 # Function to prompt the appending of a section to bashrc.
324 function prompt_append_bashrc
325 {
326     local skip=0
327     local section="
328
329 # >>>>>>>>>>>>>>>>>>>>>>> Enabling Biopieces <<<<<<<<<<<<<<<<<<<<<<<
330
331 export BP_DIR=\"$bp_code\"
332 export BP_DATA=\"$bp_data\"
333 export BP_TMP=\"$bp_tmp\"
334 export BP_LOG=\"$bp_log\"
335
336 source \"\$BP_DIR/bp_conf/bashrc\"
337
338 # >>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<
339
340 "
341
342     echo ""
343     echo "We need to append the below section to your .bashrc file."
344
345     echo_yellow "$section"
346
347     while true; do
348         read -p "Append (yes/no/abort)? " answer
349         case $answer in
350             [Yy]* ) skip=0 && break;;
351             [Nn]* ) skip=1 && break;;
352             [Aa]* ) exit_abort;;
353         esac
354     done
355
356     if [ $skip == 1 ]; then
357         echo "Skipping"
358     else
359         if [ -f "$HOME/.bashrc" ]; then
360             echo "Existing .bashrc file located: $HOME/.bashrc"
361             echo -n "Creating backup: "
362
363             if ! cp "$HOME/.bashrc" "$HOME/.bashrc_biopieces"; then
364                 echo_red "FAIL"
365                 abort
366             else
367                 echo_green "OK"
368                 echo "   Backup is $HOME/.bashrc_biopieces"
369             fi
370         fi
371
372         echo -n "Appending $HOME/.bashrc: "
373
374         if ! echo "$section" >> "$HOME/.bashrc"; then
375             echo_red "FAIL"
376             abort
377         else
378             echo_green "OK"
379         fi
380
381         echo -n "Sourcing $HOME/.bashrc: "
382
383         if ! source "$HOME/.bashrc"; then
384             echo_red "FAIL"
385             abort
386         else
387             echo_green "OK"
388         fi
389
390         echo ""
391         echo "   \$BP_DIR is now set to: $BP_DIR"
392         echo "   \$BP_DATA is now set to: $BP_DATA"
393         echo "   \$BP_TMP is now set to: $BP_TMP"
394         echo "   \$BP_LOG is now set to: $BP_LOG"
395         echo ""
396     fi
397 }
398
399 # Function to test if we are running bash.
400 function test_bash
401 {
402     echo -n "   Testing if the running shell is bash: "
403
404     if [ `echo $SHELL | grep "bash"` ]; then
405         echo_green "OK"
406     else
407         echo_red "FAIL"
408         echo "      Biopieces requires bash shell not - $SHELL."
409         exit_abort
410     fi
411 }
412
413 # Function to test if subversion client is in $PATH.
414 function test_svn
415 {
416     local program="svn"
417
418     echo -n "   Testing subversion client - \"$program\": "
419
420     if command -v $program >/dev/null; then
421         echo_green "OK"
422     else
423         echo_red "FAIL"
424         echo "      Subversion client $program was not found."
425         exit_abort
426     fi
427 }
428
429 # Function to test if the required version of Perl is installed.
430 function test_perl
431 {
432     echo -n "   Testing Perl version: "
433
434     if error=$( perl -e 'use 5.8.0;' 2>&1 ); then
435         echo_green "OK"
436     else
437         echo_red "FAIL"
438         echo "   $error" | sed "s/, stopped.*//"
439         exit_abort
440     fi  
441 }
442
443 # Function to test if a given Perl module is installed.
444 function test_perl_module
445 {
446     local module=$1
447
448     echo -n "   Testing required Perl module - \"$module\": "
449
450     if ! error=$( perl -M$module -e '' 2>&1 > /dev/null ); then
451         echo_red "FAIL"
452         echo "   Try: perl -MCPAN -e 'install $module'"
453         exit_abort
454     else
455         echo_green "OK"
456     fi
457 }
458
459 # Function to test if the required version of Ruby is installed.
460 function test_ruby
461 {
462     echo -n "   Testing Ruby version: "
463
464     if error=$( ruby -e 'raise "Ruby version 1.9 required--this is only #{RUBY_VERSION}" if RUBY_VERSION < "1.9"' 2>&1 ); then
465         echo_green "OK"
466     else
467         echo_red "FAIL"
468         exit_abort
469     fi  
470 }
471
472 # Function to test if a given Ruby gem is installed.
473 function test_ruby_gem
474 {
475     local gem=$1
476
477     echo -n "   Testing required Ruby gem - \"$gem\": "
478
479     if error=$( gem list --local | grep $gem ); then
480         echo_green "OK"
481     else
482         echo_red "FAIL"
483         echo "      Try: gem install $gem"
484         exit_abort
485     fi
486 }
487
488 # Function to test is a given auxillary program is in $PATH.
489 function test_aux_program
490 {
491     local program=$1
492
493     echo -n "   Testing auxiliary program - \"$program\": "
494
495     if command -v $program >/dev/null; then
496         echo_green "OK"
497     else
498         echo_yellow "WARNING"
499     fi
500 }
501
502 # Function to checkout the Biopieces code from subversion.
503 function checkout_code
504 {
505     echo -n "Downloading Biopieces code from repository (please wait): "
506
507     if error=$( svn checkout http://biopieces.googlecode.com/svn/trunk/ $bp_code ); then
508         echo_green "OK"
509     else
510         echo_red "FAIL"
511         echo "   $error"
512         exit_abort
513     fi  
514 }
515
516 # Function to checkout the Biopieces wiki from subversion.
517 function checkout_wiki
518 {
519     echo -n "Downloading Biopieces wiki from repository (please wait): "
520
521     if error=$( svn checkout http://biopieces.googlecode.com/svn/wiki/ "$bp_code/bp_usage" ); then
522         echo_green "OK"
523     else
524         echo_red "FAIL"
525         echo "   $error"
526         exit_abort
527     fi  
528 }
529
530 # Function to run the Biopieces tests.
531 function prompt_test_biopieces
532 {
533     echo "Running the Biopieces test suite:"
534     echo ""
535
536     "$BP_DIR/bp_test/test_all"
537
538     echo ""
539     echo "   Any FAIL indicates broken Biopieces. This is probably because of"
540     echo "   missing prerequisites (see WARNINGs). You can verify this later by"
541     echo "   inspecting the documentation for those Biopieces. Otherwise - seek help!"
542
543     while true; do
544         read -p "Continue (yes/no)? " answer
545         case $answer in
546             [Yy]* ) break;;
547             [Nn]* ) exit_abort;;
548         esac
549     done
550 }
551
552 prompt_install
553 prompt_install_existing
554 prompt_test_prerequisites
555 prompt_install_dir_code
556 prompt_install_dir_data
557 prompt_install_dir_log
558 prompt_install_dir_tmp
559
560 checkout_code
561 checkout_wiki
562
563 prompt_append_bashrc
564 prompt_test_biopieces
565
566 exit_success
567