Add 'CAN-binder/libs/openxc-message-format/' from commit 'd9f54f97578429773421abce98d...
[apps/agl-service-can-low-level.git] / CAN-binder / libs / openxc-message-format / script / bootstrap.sh
1 #!/usr/bin/env bash
2
3 set -e
4
5 DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
6 pushd $DIR/..
7
8 # TODO this is kind of a hacky way of determining if root is required -
9 # ideally we wouuld set up a little virtualenv in the dependencies folder
10 SUDO_CMD=
11 if command -v sudo >/dev/null 2>&1; then
12     SUDO_CMD="sudo -E"
13
14     echo "The bootstrap script needs to install a few packages to your system as an admin, and we will use the 'sudo' command - enter your password to continue"
15     $SUDO_CMD ls > /dev/null
16 fi
17
18 KERNEL=`uname`
19 if [ ${KERNEL:0:7} == "MINGW32" ]; then
20     OS="windows"
21 elif [ ${KERNEL:0:6} == "CYGWIN" ]; then
22     OS="cygwin"
23 elif [ $KERNEL == "Darwin" ]; then
24     OS="mac"
25 else
26     OS="linux"
27     if ! command -v lsb_release >/dev/null 2>&1; then
28         # Arch Linux
29         if command -v pacman>/dev/null 2>&1; then
30             $SUDO_CMD pacman -S lsb-release
31         fi
32     fi
33
34     DISTRO=`lsb_release -si`
35 fi
36
37 die() {
38     echo >&2 "${bldred}$@${txtrst}"
39     exit 1
40 }
41
42 _cygwin_error() {
43     echo
44     echo "${bldred}Missing \"$1\"${txtrst} - run the Cygwin installer again and select the base package set:"
45     echo "    $CYGWIN_PACKAGES"
46     echo "After installing the packages, re-run this bootstrap script."
47     die
48 }
49
50 if ! command -v tput >/dev/null 2>&1; then
51     if [ $OS == "cygwin" ]; then
52         echo "OPTIONAL: Install the \"ncurses\" package in Cygwin to get colored shell output"
53     fi
54 else
55     txtrst=$(tput sgr0) # reset
56     bldred=${txtbld}$(tput setaf 1)
57     bldgreen=${txtbld}$(tput setaf 2)
58 fi
59
60 _pushd() {
61     pushd $1 > /dev/null
62 }
63
64 _popd() {
65     popd > /dev/null
66 }
67
68 _wait() {
69     if [ -z $CI ]; then
70         echo "Press Enter when done"
71         read
72     fi
73 }
74
75 _install() {
76     if [ $OS == "cygwin" ]; then
77         _cygwin_error $1
78     elif [ $OS == "mac" ]; then
79         # brew exists with 1 if it's already installed
80         set +e
81         brew install $1
82         set -e
83     else
84         if [ -z $DISTRO ]; then
85             echo
86             echo "Missing $1 - install it using your distro's package manager or build from source"
87             _wait
88         else
89             if [ $DISTRO == "arch" ]; then
90                 $SUDO_CMD pacman -S $1
91             elif [ $DISTRO == "Ubuntu" ]; then
92                 $SUDO_CMD apt-get update -qq
93                 $SUDO_CMD apt-get install $1 -y
94             else
95                 echo
96                 echo "Missing $1 - install it using your distro's package manager or build from source"
97                 _wait
98             fi
99         fi
100     fi
101 }
102
103 CYGWIN_PACKAGES="make curl, libsasl2, ca-certificates, ncurses, python-setuptools"
104
105 download() {
106     url=$1
107     filename=$2
108     curl $url -L --O $filename
109 }
110
111 if [ `id -u` == 0 ]; then
112     die "Error: running as root - don't use 'sudo' with this script"
113 fi
114
115 if ! command -v curl >/dev/null 2>&1; then
116     if [ $OS == "cygwin" ]; then
117         _cygwin_error "curl"
118     else
119         _install curl
120     fi
121 fi
122
123 if [ $OS == "windows" ]; then
124     die "Sorry, the bootstrap script for compiling from source doesn't support the Windows console - try Cygwin."
125 fi
126
127 if [ $OS == "mac" ] && ! command -v brew >/dev/null 2>&1; then
128     echo "Installing Homebrew..."
129     ruby -e "$(curl -fsSkL raw.github.com/mxcl/homebrew/go)"
130 fi
131
132 if ! command -v make >/dev/null 2>&1; then
133     if [ $OS == "cygwin" ]; then
134         _cygwin_error "make"
135     elif [ $OS == "mac" ]; then
136             die "Missing 'make' - install the Xcode CLI tools"
137     else
138         if [ $DISTRO == "arch" ]; then
139             $SUDO_CMD pacman -S base-devel
140         elif [ $DISTRO == "Ubuntu" ]; then
141             $SUDO_CMD apt-get update -qq
142             $SUDO_CMD apt-get install build-essential -y
143         fi
144     fi
145 fi
146
147 if ! command -v python >/dev/null 2>&1; then
148     echo "Installing Python..."
149     _install "python"
150 fi
151
152 if ! command -v pip >/dev/null 2>&1; then
153     echo "Installing Pip..."
154     if ! command -v easy_install >/dev/null 2>&1; then
155         die "easy_install not available, can't install pip"
156     fi
157
158     $SUDO_CMD easy_install pip
159 fi
160
161 $SUDO_CMD pip install -U pip
162 $SUDO_CMD pip install --pre -Ur pip-requirements.txt
163
164 if ! command -v protoc >/dev/null 2>&1; then
165     if [ $OS == "cygwin" ]; then
166         _cygwin_error "protobuf"
167     elif [ $OS == "mac" ] || [ $OS == "linux" ]; then
168         if [ $DISTRO == "Ubuntu" ]; then
169             _install protobuf-compiler
170         else
171             _install protobuf
172         fi
173     fi
174 fi
175
176 popd
177
178 echo
179 echo "${bldgreen}All developer dependencies installed, ready to compile.$txtrst"