# Use the official image as a parent image.
FROM ubuntu:20.04

# Set the working directory.
WORKDIR /linaro-android

RUN dpkg --add-architecture amd64 \
    # Modify sources.list to explicitly define arm64 repositories
    && sed -i 's|^deb |deb [arch=arm64] |g' /etc/apt/sources.list \
    && sed -i 's|http://archive.ubuntu.com/ubuntu|http://ports.ubuntu.com/ubuntu-ports|g' /etc/apt/sources.list \
    # Add a separate list for amd64 repositories without duplication
    && cp /etc/apt/sources.list /etc/apt/sources.list.d/amd64.list \
    && sed -i 's|\[arch=arm64\]|\[arch=amd64\]|g' /etc/apt/sources.list.d/amd64.list \
    && sed -i 's|http://ports.ubuntu.com/ubuntu-ports|http://archive.ubuntu.com/ubuntu|g' /etc/apt/sources.list.d/amd64.list \
    # Update and install necessary packages
    && apt-get update \
    && DEBIAN_FRONTEND=noninteractive apt-get install -y \
        aapt \
        adb \
        apt-utils \
        binfmt-support \
        coreutils \
        curl \
        git \
        inetutils-ping \
        iproute2 \
        libcurl4 \
        libc6:amd64 \
        libc6:arm64 \
        libz1:amd64 \
        libz1:arm64 \
        openjdk-11-jdk-headless \
        openjdk-17-jdk-headless \
        protobuf-compiler \
        psmisc \
        python3 \
        python-is-python3 \
        python-lxml \
        python-pexpect \
        python-protobuf \
        python-setuptools \
        qemu-user-static \
        sudo \
        tar \
        unzip \
        usbutils \
        wget \
        xz-utils \
        zip \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* /etc/apt/sources.list.d/*.key

###################################################################
## adb could not be worked around with this method
## $ adb devices
##  * daemon not running; starting now at tcp:5037
##  adb: execl returned -1: Exec format error
##  ADB server didn't ACK
##  Full server startup log: /tmp/adb.0.log
##  Server had pid: 668
##  * failed to start daemon
##  adb: failed to check server version: cannot connect to daemon
##
### update-binfmts --enable qemu-x86_64 should be necessary
###################################################################
###################################################################
## Work around the fastboot format cache issue ####################
## mke2fs 1.47.0 (5-Feb-2023)
## /tmp/TemporaryFile-55jZkm: Unimplemented ext2 library function while setting up superblock
## /usr/lib/android-sdk/platform-tools/mke2fs failed with status 1
## fastboot: error: Cannot generate image for cache
###################################################################
# https://developer.android.com/studio/releases/platform-tools
ADD https://android-git.linaro.org/platform/prebuilts/linaro-prebuilts.git/plain/host/bin/wrapper.sh?h=wrapper /usr/bin/wrapper.sh
RUN curl -SL https://dl.google.com/android/repository/platform-tools_r34.0.5-linux.zip -o platform-tools_r34.0.5-linux.zip \
    && unzip platform-tools_r34.0.5-linux.zip \
    && rm -f platform-tools_r34.0.5-linux.zip \
    && cd platform-tools/ \
    && mv adb adb_x86_64 \
    && mv fastboot fastboot_x86_64 \
    && mv mke2fs mke2fs_x86_64 \
    && mv /usr/bin/wrapper.sh /linaro-android/platform-tools/wrapper.sh && chmod +x /linaro-android/platform-tools/wrapper.sh \
    && sed -i '/bin_path=/c\bin_path="/linaro-android/platform-tools/${bin_name}_x86_64"' /linaro-android/platform-tools/wrapper.sh \
    && ln -s /linaro-android/platform-tools/wrapper.sh /linaro-android/platform-tools/adb \
    && ln -s /linaro-android/platform-tools/wrapper.sh /linaro-android/platform-tools/e2fsdroid\
    && ln -s /linaro-android/platform-tools/wrapper.sh /linaro-android/platform-tools/fastboot \
    && ln -s /linaro-android/platform-tools/wrapper.sh /linaro-android/platform-tools/mke2fs \
    && rm -f /usr/bin/fastboot && ln -s /linaro-android/platform-tools/wrapper.sh /usr/bin/fastboot
