summaryrefslogtreecommitdiff
path: root/utils.sh
blob: 0e0335cfb04d9f36fde94f228826d60fda5b6e49 (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
#!/bin/sh

# Shell library for service installation and general tools
# Author: Christoph Helma, Andreas Maunz

# Functions to install ruby and install services with bundler.
# Ensures presence of ~/.opentox (CONFIG) and OT_PREFIX.

# create a temporary directory in OT_PREFIX and a log directory in .opentox
check_dest() 
{
  [ -d "$OT_PREFIX/tmp" ] || mkdir -p "$OT_PREFIX/tmp"
  if ! [ -d "$OT_PREFIX/tmp" ]; then
    echo "Could not create OT_PREFIX directory! Aborting..."
    exit 1
  fi
  [ -d "$HOME/.opentox/log" ] || mkdir -p "$HOME/.opentox/log"
  if ! [ -d "$HOME/.opentox/log" ]; then
    echo "Could not create CONFIG (~/.opentox) directory! Aborting..."
    exit 1
  fi
}

# export a LOG file name
check_log()
{
  local name="$1"
  if [ -z "$LOG" ]; then
    LOG="$OT_PREFIX/tmp/$name.log"
    echo
    printf "\033[1m%s\033[m\n" "$name ('$LOG'):"
    echo
  fi
}

# run a shell command
# Signal exit status by formatted string
# @param cmd command to execute 
# @param title textual description of command
# @example {
#   run_cmd "ls" "list dir"
# }
run_cmd ()
{
  local cmd="$1"; local title="$2"
  printf "%50s" "$title"
  if ! eval "$cmd" >>$LOG 2>&1 ; then  
    printf "                              [ \033[31m%s\033[m ]\n" "FAIL"
    echo "Last 10 lines of log:"
    tail -10 "$LOG"
    exit 1
  fi
  printf "                              [  \033[32m%s\033[m  ]\n" "OK"
}


# stop if required binaries not in path
# @param list of binaries
# @example {
#   check_utils "git curl"
# }
check_utils() {
  for u in $1; do
    UPATH=`which $u`
    if [ "$?" -eq 1 ]; then 
      echo "'$u' missing. Install '$u' first." 1>&2 && exit 1
    fi
    eval `echo $u | tr "[:lower:]" "[:upper:]" | tr "-" "_"`=$UPATH
  done
}


# install ruby using rbenv
# configure the version in config.sh
install_ruby() {
  printf "\n%50s\n" "RUBY (v. '$RUBY_NUM_VER')"
  local DIR=`pwd`
  check_utils "rbenv curl make tar"
  if ! $RBENV versions $RUBY_NUM_VER | grep $RUBY_NUM_VER>/dev/null 2>&1; then
    [ -d $OT_PREFIX/tmp ] || mkdir -p $OT_PREFIX/tmp && cd $OT_PREFIX/tmp
    ([ -d $OT_PREFIX/tmp/ruby-$RUBY_NUM_VER ] || $CURL $RUBY_DWL/ruby-$RUBY_NUM_VER.tar.gz 2>/dev/null | $TAR xz) && cd ruby-$RUBY_NUM_VER
    cmd="./configure --prefix=$RUBY_DIR" && run_cmd "$cmd" "Configure"
    cmd="$MAKE -j2" && run_cmd "$cmd" "Make"
    cmd="$MAKE install" && run_cmd "$cmd" "Install"
  fi
  cd $DIR
  cmd="$RBENV rehash" && run_cmd "$cmd" "Rbenv rehash"
  if [ "$1" = "global" ]; then
    cmd="$RBENV global $RUBY_NUM_VER" && run_cmd "$cmd" "Rbenv set ruby global"
  else
    if ! $RBENV global | grep $RUBY_NUM_VER>/dev/null 2>&1; then
      cmd="$RBENV local $RUBY_NUM_VER" && run_cmd "$cmd" "Rbenv set ruby local"
    fi
  fi
}

# install a ruby gem using bundler
install_with_bundler() {
  printf "\n%50s\n" "INSTALL"
  check_utils "gem rbenv"
  $GEM list | grep bundler >/dev/null 2>&1 || (cmd="$GEM install bundler" && run_cmd "$cmd" "Install bundler")
  cmd="$RBENV rehash" && run_cmd "$cmd" "Rbenv rehash"
  check_utils "gem rbenv bundle"
  cmd="$BUNDLE install" && run_cmd "$cmd" "Install using bundler"
  cmd="$RBENV rehash" && run_cmd "$cmd" "Rbenv rehash"
}

# download opentox git repo
ot_git_download(){
  printf "\n%50s\n" "GIT DOWNLOAD"
  check_utils "git"
  cd $OT_PREFIX
  cmd="$GIT clone git@github.com:opentox/$SERVICE.git" && run_cmd "$cmd" "Downloading $SERVICE git repository"
}

# emit notification if caller was the shell (the user), see http://goo.gl/grCOk
notify() {
  echo
  echo "Installation finished (check above for errors)"
  echo
  if ps -o stat= -p $PPID | grep "s" >/dev/null 2>&1; then
    echo "IMPORTANT: How to configure your system if everything went fine:"
    echo "IMPORTANT: bash shell only: '. $OT_TOOLS_CONF; otconfig' (convenient ot-tools)"
    echo "IMPORTANT: other shells: '. $OT_UI_CONF' (no ot-tools available)"
    echo "IMPORTANT: More information: 'http://opentox.github.com/General/2012/08/09/install-opentox-development-environment'"
    echo "IMPORTANT: Thank you for your attention."
  fi
}

# Force loading configuration from local, if we are installing for the first time
if [ -z "$OT_PREFIX" ]; then
  . ./config.sh
else
  . $HOME/.opentox/config/install/config.sh
fi
check_dest
touch "$OT_UI_CONF"
. "$OT_UI_CONF" 2>/dev/null