#!/bin/bash

# This file is part of libbot2.
#
# libbot2 is free software: you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by the
# Free Software Foundation, either version 3 of the License, or (at your
# option) any later version.
#
# libbot2 is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
# License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with libbot2. If not, see <https://www.gnu.org/licenses/>.

# This script will start a docker container that contains the requirements to
# build libbot2 (except LCM which is built as part of this process). It will
# then copy a script in the docker container that has been start, and run that
# script which builds and packages LCM, builds and packages libbot2, and copy
# the 2 deb archives in the current working directory before stopping and
# removing the docker container.

set -euxo pipefail

readonly timestamp=$(date -u +%Y%m%d)

rm -f lcm_1.4.0-2_amd64.deb libbot2_0.0.1.*-1_amd64.deb

docker build --tag libbot2-bionic-prereqs "$(git rev-parse --show-toplevel)"/scripts/setup/linux/ubuntu/bionic/
docker run --detach --name libbot2-bionic --tty libbot2-bionic-prereqs
trap 'docker stop libbot2-bionic && docker rm libbot2-bionic' EXIT
docker cp "$(git rev-parse --show-toplevel)" libbot2-bionic:/tmp/libbot2
docker exec libbot2-bionic /tmp/libbot2/scripts/package/linux/ubuntu/common/build.sh "${timestamp}"
docker cp libbot2-bionic:/tmp/lcm_1.4.0-2_amd64.deb .
docker cp libbot2-bionic:/tmp/"libbot2_0.0.1.${timestamp}-1_amd64.deb" .

# Test package
docker run --detach --name libbot2-bionic-test --tty ubuntu:18.04
trap 'docker stop libbot2-bionic libbot2-bionic-test && docker rm libbot2-bionic libbot2-bionic-test' EXIT
docker cp lcm_1.4.0-2_amd64.deb libbot2-bionic-test:/tmp
docker cp "libbot2_0.0.1.${timestamp}-1_amd64.deb" libbot2-bionic-test:/tmp
docker cp "$(git rev-parse --show-toplevel)" libbot2-bionic-test:/tmp/libbot2
docker exec libbot2-bionic-test /tmp/libbot2/scripts/package/linux/ubuntu/common/test.sh "${timestamp}"
