1#!/bin/bash
2
3DRY_RUN=""
4if [ $# -gt 0 ]; then
5    if [ "$1" == "--dry-run" ]; then
6        DRY_RUN="echo "
7    fi
8fi
9
10SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
11PARENT_DIR="$(echo ${SCRIPT_DIR} | rev | cut -d '/' -f 2- | rev )"
12
13TMP_DIR=$(mktemp -d)
14OUT_DIR="${TMP_DIR}/out"
15
16trap ctrl_c INT
17
18function ctrl_c() {
19    echo -n "Cleaning up..."
20    rm -rf "${TMP_DIR}"
21    echo "Done."
22    exit 1
23}
24
25# APT dependencies
26APT_REQUIRED="git curl wget flatbuffers-compiler flex g++-multilib gcc-multilib generate-ninja \
27gnupg gperf libc++-dev libdbus-1-dev libevent-dev libflatbuffers-dev libfmt-dev libflatbuffers1 \
28libgl1-mesa-dev libglib2.0-dev liblz4-tool libncurses5 libnss3-dev libprotobuf-dev libre2-9 \
29libssl-dev libtinyxml2-dev libx11-dev libxml2-utils ninja-build openssl protobuf-compiler unzip \
30x11proto-core-dev xsltproc zip zlib1g-dev libc++abi-dev cmake debmake ninja-build libgtest-dev \
31libgmock-dev libabsl-dev libre2-dev libdouble-conversion-dev bison clang llvm"
32
33${DRY_RUN} sudo apt install ${APT_REQUIRED}
34
35# Install rustup
36curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
37source $HOME/.cargo/env
38${DRY_RUN} rustup update
39
40${DRY_RUN} cargo install cxxbridge-cmd --version 1.0.42 || ctrl_c
41${DRY_RUN} cargo install cargo-proc-macro || ctrl_c
42
43echo "Building and installing modp-b64..."
44# dir name is different than package name :'(
45${DRY_RUN} pushd "${PARENT_DIR}/modp_b64/"
46${DRY_RUN} ./gen-src-pkg.sh "${OUT_DIR}" || crtl_c
47${DRY_RUN} sudo dpkg -i "${OUT_DIR}"/modp-b64*.deb || ctrl_c
48${DRY_RUN} popd
49
50echo "Building and installing libchrome..."
51${DRY_RUN} pushd "${PARENT_DIR}/libchrome/"
52${DRY_RUN} ./gen-src-pkg.sh "${OUT_DIR}" || crtl_c
53${DRY_RUN} sudo dpkg -i "${OUT_DIR}"/libchrome*.deb || ctrl_c
54${DRY_RUN} popd
55
56HAS_EXPORT="$(cat \$HOME/.bashrc|grep '.cargo/bin')"
57if [ "${HAS_EXPORT}" == "" ]; then
58    ${DRY_RUN} echo "export PATH=\$PATH:\$HOME/.cargo/bin" >> ~/.bashrc
59fi
60
61HAS_EXPORT="$(cat \$HOME/.bashrc|grep '$HOME/bin')"
62if [ "${HAS_EXPORT}" == "" ]; then
63    ${DRY_RUN} echo "export PATH=\$PATH:\$HOME/bin" >> ~/.bashrc
64fi
65
66# Put the GN binary in the bin...it isn't the right spot, but avoids adding a second directory
67# to the environmental PATH
68${DRY_RUN} mkdir -p ~/bin
69${DRY_RUN} wget -O ~/bin/gn http://commondatastorage.googleapis.com/chromeos-localmirror/distfiles/gn-3e43fac03281e2f5e5ae5f27c8e9a6bb45966ea9.bin
70${DRY_RUN} chmod +x ~/bin/gn
71
72rm -rf "${TMP_DIR}"
73echo "DONE"
74