###############################################################################
# Dockerfile used to make gitlab.linphone.org:4567/bc/public/linphone-sdk/bc-dev-android-r25b:20230706_add_deps_for_av1
###############################################################################

FROM debian:11

MAINTAINER Peio Rigaux <peio.rigaux@belledonne-communications.com>

# Enable backport repository and switch on a french mirror


# Configure locale
RUN apt-get update && \
    apt-get install -y locales && \
    apt-get clean && \
    echo "en_US.UTF-8 UTF-8" > /etc/locale.gen && \
    locale-gen
ENV LANG='en_US.UTF-8' LANGUAGE='en_US:en' LC_ALL='en_US.UTF-8'

ENV SHELL=/bin/bash

# Define environment
ENV ANDROID_NDK r25b
ENV ANDROID_HOME /opt/android-sdk-linux

# Install common general tools
RUN apt-get update && \
    apt-get install -y curl nano sudo unzip vim wget rsync && \
    apt-get clean

# Install development tools
RUN apt-get update && \
    apt-get install -y ccache doxygen git make nasm ninja-build meson perl openssh-client pkg-config yasm && \
    apt-get install -y openjdk-17-jdk-headless && \
    apt-get clean

# Install python3 and python modules /!\ There is a build issue with pystache 0.6.0, hence the ==0.5.4
RUN apt-get install -y python3 python3-pip && \
    python3 -m pip install six && \
    python3 -m pip install pystache==0.5.4 && \
    python3 -m pip install graphviz && \
    python3 -m pip install jsonschema && \
    python3 -m pip install jinja2

# Install CMake (version from the debian repository is too old)
ENV CMAKE3_HOME /opt/cmake3
RUN mkdir $CMAKE3_HOME && \
    curl --fail --silent --show-error \
         --location https://cmake.org/files/v3.22/cmake-3.22.6-linux-x86_64.tar.gz | \
    tar --extract --gzip --no-same-owner --file - --strip-components=1 --directory $CMAKE3_HOME

# Get the Android NDK
RUN wget https://dl.google.com/android/repository/android-ndk-$ANDROID_NDK-linux.zip && \
    unzip android-ndk-$ANDROID_NDK-linux.zip -d /opt && \
    rm -f android-ndk-$ANDROID_NDK-linux.zip
ENV ANDROID_NDK_HOME /opt/android-ndk-$ANDROID_NDK
ENV PATH $ANDROID_NDK_HOME:$CMAKE3_HOME/bin/:$PATH

# Get latest Android command line tools, otherwise won't work with JDK 17
RUN mkdir -p $ANDROID_HOME/cmdline-tools/latest
RUN wget https://dl.google.com/android/repository/commandlinetools-linux-9477386_latest.zip && \
    unzip commandlinetools-linux-9477386_latest.zip && \
    cp -R ./cmdline-tools/* $ANDROID_HOME/cmdline-tools/latest/ && \
    rm -rf ./cmdline-tools && \
    rm -rf commandlinetools-linux-9477386_latest.zip

# Update path to include all Android SDK tools
ENV PATH $ANDROID_HOME/cmdline-tools/latest/bin:$ANDROID_HOME/tools:$ANDROID_HOME/tools/bin:$ANDROID_HOME/platform-tools:ANDROID_NDK_HOME:$PATH

# Get the Android SDK
RUN echo "y" | $ANDROID_HOME/cmdline-tools/latest/bin/sdkmanager "platform-tools" "tools" "platforms;android-31"

# Accept Android SDK licenses
RUN yes | $ANDROID_HOME/cmdline-tools/latest/bin/sdkmanager --licenses

# Give write rights
RUN chmod -R ugo+rwx $ANDROID_HOME

# Configure user bc
RUN useradd -ms /bin/bash bc && \
    echo 'bc:cotcot' | chpasswd && \
    echo 'bc ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers

USER bc
WORKDIR /home/bc
ENV PS1='\[\e[34m\]\u@bc-dev-android-r25b>\[\e[0m\] '
CMD bash
