1#! /bin/sh
2# Copyright 2017 The Kyua Authors.
3# All rights reserved.
4#
5# Redistribution and use in source and binary forms, with or without
6# modification, are permitted provided that the following conditions are
7# met:
8#
9# * Redistributions of source code must retain the above copyright
10#   notice, this list of conditions and the following disclaimer.
11# * Redistributions in binary form must reproduce the above copyright
12#   notice, this list of conditions and the following disclaimer in the
13#   documentation and/or other materials provided with the distribution.
14# * Neither the name of Google Inc. nor the names of its contributors
15#   may be used to endorse or promote products derived from this software
16#   without specific prior written permission.
17#
18# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30# \file admin/build-bintray-dist.sh
31# Builds a full Kyua installation under /usr/local for Ubuntu.
32#
33# This script is used to create the bintray distribution packages in lieu
34# of real Debian packages for Kyua.  The result of this script is a
35# tarball that provides the contents of /usr/local for Kyua.
36
37set -e -x
38
39err() {
40    echo "${@}" 1>&2
41    exit 1
42}
43
44install_deps() {
45    sudo apt-get update -qq
46
47    local pkgsuffix=
48    local packages=
49    packages="${packages} autoconf"
50    packages="${packages} automake"
51    packages="${packages} clang"
52    packages="${packages} g++"
53    packages="${packages} gdb"
54    packages="${packages} git"
55    packages="${packages} libtool"
56    packages="${packages} make"
57    if [ "${ARCH?}" = i386 ]; then
58         pkgsuffix=:i386
59         packages="${packages} gcc-multilib"
60         packages="${packages} g++-multilib"
61    fi
62    packages="${packages} liblua5.2-0${pkgsuffix}"
63    packages="${packages} liblua5.2-dev${pkgsuffix}"
64    packages="${packages} libsqlite3-0${pkgsuffix}"
65    packages="${packages} libsqlite3-dev${pkgsuffix}"
66    packages="${packages} pkg-config${pkgsuffix}"
67    packages="${packages} sqlite3"
68    sudo apt-get install -y ${packages}
69}
70
71install_from_github() {
72    local name="${1}"; shift
73    local release="${1}"; shift
74
75    local distname="${name}-${release}"
76
77    local baseurl="https://github.com/jmmv/${name}"
78    wget --no-check-certificate \
79        "${baseurl}/releases/download/${distname}/${distname}.tar.gz"
80    tar -xzvf "${distname}.tar.gz"
81
82    local archflags=
83    [ "${ARCH?}" != i386 ] || archflags=-m32
84
85    cd "${distname}"
86    ./configure \
87        --disable-developer \
88        --without-atf \
89        --without-doxygen \
90        CC="${CC?}" \
91        CFLAGS="${archflags}" \
92        CPPFLAGS="-I/usr/local/include" \
93        CXX="${CXX?}" \
94        CXXFLAGS="${archflags}" \
95        LDFLAGS="-L/usr/local/lib -Wl,-R/usr/local/lib" \
96        PKG_CONFIG_PATH="/usr/local/lib/pkgconfig"
97    make
98    sudo make install
99    cd -
100
101    rm -rf "${distname}" "${distname}.tar.gz"
102}
103
104main() {
105    [ "${ARCH+set}" = set ] || err "ARCH must be set in the environment"
106    [ "${CC+set}" = set ] || err "CC must be set in the environment"
107    [ "${CXX+set}" = set ] || err "CXX must be set in the environment"
108
109    [ ! -f /root/local.tgz ] || err "/root/local.tgz already exists"
110    tar -czf /root/local.tgz /usr/local
111    restore() {
112        rm -rf /usr/local
113        tar -xz -C / -f /root/local.tgz
114        rm /root/local.tgz
115    }
116    trap restore EXIT
117    rm -rf /usr/local
118    mkdir /usr/local
119
120    install_deps
121    install_from_github atf 0.21
122    install_from_github lutok 0.4
123    install_from_github kyua 0.13
124
125    local version="$(lsb_release -rs | cut -d . -f 1-2 | tr . -)"
126    local name="$(date +%Y%m%d)-usr-local-kyua"
127    name="${name}-ubuntu-${version}-${ARCH?}-${CC?}.tar.gz"
128    tar -czf "${name}" /usr/local
129}
130
131main "${@}"
132