#!/bin/bash
#
#  Copyright (c) 2018, The OpenThread Authors.
#  All rights reserved.
#
#  Redistribution and use in source and binary forms, with or without
#  modification, are permitted provided that the following conditions are met:
#  1. Redistributions of source code must retain the above copyright
#     notice, this list of conditions and the following disclaimer.
#  2. Redistributions in binary form must reproduce the above copyright
#     notice, this list of conditions and the following disclaimer in the
#     documentation and/or other materials provided with the distribution.
#  3. Neither the name of the copyright holder nor the
#     names of its contributors may be used to endorse or promote products
#     derived from this software without specific prior written permission.
#
#  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
#  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
#  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
#  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
#  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
#  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
#  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
#  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
#  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
#  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
#  POSSIBILITY OF SUCH DAMAGE.
#

set -e
set -x

TOOLS_HOME=$HOME/.cache/tools

main()
{
    IMAGE_NAME=$(basename "${IMAGE_URL}" .zip)
    STAGE_DIR=/tmp/raspbian
    IMAGE_DIR=/media/rpi
    IMAGE_FILE="$TOOLS_HOME"/images/"$IMAGE_NAME".img

    [ -d "$STAGE_DIR" ] || mkdir -p "$STAGE_DIR"
    cp -v "$IMAGE_FILE" "$STAGE_DIR"/raspbian.img

    python3 -m git_archive_all "$STAGE_DIR"/repo.tar.gz

    cat >"$STAGE_DIR"/check.sh <<EOF
set -ex

export LC_ALL=C
export DEBIAN_FRONTEND=noninteractive

echo "127.0.0.1 \$(hostname)" >> /etc/hosts
chown -R pi:pi /home/pi/repo
cd /home/pi/repo
echo 1 | sudo tee /proc/sys/net/ipv6/conf/all/disable_ipv6
apt-get update --allow-releaseinfo-change
apt-get install -y --no-install-recommends git python3-pip
su -c 'RELEASE=1 script/bootstrap' pi

# Pin CMake version to 3.10.3 for issue https://github.com/openthread/ot-br-posix/issues/728.
# For more background, see https://gitlab.kitware.com/cmake/cmake/-/issues/20568.
apt-get purge -y cmake
pip3 install scikit-build
pip3 install cmake==3.10.3
cmake --version

su -c 'RELEASE=1 NETWORK_MANAGER=0 script/setup' pi
EOF

    (
        cd docker-rpi-emu/scripts \
            && sudo mkdir -p "$IMAGE_DIR" \
            && sudo ./mount.sh "$STAGE_DIR"/raspbian.img "$IMAGE_DIR" \
            && sudo mount --bind /dev/pts "$IMAGE_DIR"/dev/pts \
            && sudo mkdir -p "$IMAGE_DIR"/home/pi/repo \
            && sudo tar xzf "$STAGE_DIR"/repo.tar.gz --strip-components 1 -C "$IMAGE_DIR"/home/pi/repo \
            && sudo cp -v "$STAGE_DIR"/check.sh "$IMAGE_DIR"/home/pi/check.sh \
            && sudo ./qemu-setup.sh "$IMAGE_DIR" \
            && sudo chroot "$IMAGE_DIR" /bin/bash /home/pi/check.sh
        sudo umount -lf "$IMAGE_DIR"/dev/pts || true
        sudo ./qemu-cleanup.sh "$IMAGE_DIR" || true
        sudo umount -lf "$IMAGE_DIR"/dev || true
        sudo ./unmount.sh "$IMAGE_DIR"
    )
}

main "$@"
