1# shellcheck disable=SC1113
2#/usr/bin/env bash
3# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
4
5set -e
6
7function log() {
8  echo "[+] $1"
9}
10
11function fatal() {
12  echo "[!] $1"
13  exit 1
14}
15
16function platform() {
17  local  __resultvar=$1
18  if [[ -f "/etc/yum.conf" ]]; then
19    eval $__resultvar="centos"
20  elif [[ -f "/etc/dpkg/dpkg.cfg" ]]; then
21    eval $__resultvar="ubuntu"
22  else
23    fatal "Unknwon operating system"
24  fi
25}
26platform OS
27
28function package() {
29  if [[ $OS = "ubuntu" ]]; then
30    if dpkg --get-selections | grep --quiet $1; then
31      log "$1 is already installed. skipping."
32    else
33      # shellcheck disable=SC2068
34      apt-get install $@ -y
35    fi
36  elif [[ $OS = "centos" ]]; then
37    if rpm -qa | grep --quiet $1; then
38      log "$1 is already installed. skipping."
39    else
40      # shellcheck disable=SC2068
41      yum install $@ -y
42    fi
43  fi
44}
45
46function detect_fpm_output() {
47  if [[ $OS = "ubuntu" ]]; then
48    export FPM_OUTPUT=deb
49  elif [[ $OS = "centos" ]]; then
50    export FPM_OUTPUT=rpm
51  fi
52}
53detect_fpm_output
54
55function gem_install() {
56  if gem list | grep --quiet $1; then
57    log "$1 is already installed. skipping."
58  else
59    # shellcheck disable=SC2068
60    gem install $@
61  fi
62}
63
64function main() {
65  if [[ $# -ne 1 ]]; then
66    fatal "Usage: $0 <rocksdb_version>"
67  else
68    log "using rocksdb version: $1"
69  fi
70
71  if [[ -d /vagrant ]]; then
72    if [[ $OS = "ubuntu" ]]; then
73      package g++-4.8
74      export CXX=g++-4.8
75
76      # the deb would depend on libgflags2, but the static lib is the only thing
77      # installed by make install
78      package libgflags-dev
79
80      package ruby-all-dev
81    elif [[ $OS = "centos" ]]; then
82      pushd /etc/yum.repos.d
83      if [[ ! -f /etc/yum.repos.d/devtools-1.1.repo ]]; then
84        wget http://people.centos.org/tru/devtools-1.1/devtools-1.1.repo
85      fi
86      package devtoolset-1.1-gcc --enablerepo=testing-1.1-devtools-6
87      package devtoolset-1.1-gcc-c++ --enablerepo=testing-1.1-devtools-6
88      export CC=/opt/centos/devtoolset-1.1/root/usr/bin/gcc
89      export CPP=/opt/centos/devtoolset-1.1/root/usr/bin/cpp
90      export CXX=/opt/centos/devtoolset-1.1/root/usr/bin/c++
91      export PATH=$PATH:/opt/centos/devtoolset-1.1/root/usr/bin
92      popd
93      if ! rpm -qa | grep --quiet gflags; then
94        rpm -i https://github.com/schuhschuh/gflags/releases/download/v2.1.0/gflags-devel-2.1.0-1.amd64.rpm
95      fi
96
97      package ruby
98      package ruby-devel
99      package rubygems
100      package rpm-build
101    fi
102  fi
103  gem_install fpm
104
105  make static_lib
106  make install INSTALL_PATH=package
107
108  cd package
109
110  LIB_DIR=lib
111  if [[ -z "$ARCH" ]]; then
112      ARCH=$(getconf LONG_BIT)
113  fi
114  if [[ ("$FPM_OUTPUT" = "rpm") && ($ARCH -eq 64) ]]; then
115      mv lib lib64
116      LIB_DIR=lib64
117  fi
118
119  fpm \
120    -s dir \
121    -t $FPM_OUTPUT \
122    -n rocksdb \
123    -v $1 \
124    --prefix /usr \
125    --url http://rocksdb.org/ \
126    -m rocksdb@fb.com \
127    --license BSD \
128    --vendor Facebook \
129    --description "RocksDB is an embeddable persistent key-value store for fast storage." \
130    include $LIB_DIR
131}
132
133# shellcheck disable=SC2068
134main $@
135