FROM eclipse-temurin:17-jdk

# Evitar interactividad durante la instalación
ENV DEBIAN_FRONTEND=noninteractive

# Instalar dependencias necesarias para Android SDK
RUN apt-get update && apt-get install -y \
    wget \
    unzip \
    git \
    python3 \
    python3-pip \
    ncurses-bin \
    build-essential \
    lib32z1 \
    lib32ncurses6 \
    lib32stdc++6 \
    zlib1g-dev \
    && rm -rf /var/lib/apt/lists/*

# Instalar Android SDK
ENV ANDROID_SDK_ROOT=/opt/android-sdk
ENV SDKMANAGER="$ANDROID_SDK_ROOT/cmdline-tools/latest/bin/sdkmanager"

RUN mkdir -p $ANDROID_SDK_ROOT/cmdline-tools && \
    wget -q https://dl.google.com/android/repository/commandlinetools-linux-9477386_latest.zip -O tools.zip && \
    unzip -q tools.zip && \
    mv cmdline-tools $ANDROID_SDK_ROOT/cmdline-tools/latest && \
    rm tools.zip

# Aceptar licencias
RUN yes | $SDKMANAGER --licenses

# Instalar componentes necesarios
RUN $SDKMANAGER "platform-tools" "platforms;android-33" "build-tools;33.0.2" "platforms;android-31"

# Instalar Gradle
ENV GRADLE_HOME=/opt/gradle
RUN wget -q https://services.gradle.org/distributions/gradle-8.2-bin.zip -O gradle.zip && \
    unzip -q gradle.zip && \
    mv gradle-8.2 $GRADLE_HOME && \
    rm gradle.zip

ENV PATH=$PATH:$GRADLE_HOME/bin:$ANDROID_SDK_ROOT/cmdline-tools/latest/bin:$ANDROID_SDK_ROOT/platform-tools

# Copiar proyecto
COPY . /app
WORKDIR /app

# Dar permisos de ejecución a gradlew
RUN chmod +x ./gradlew

# Construir APK
RUN ./gradlew assembleRelease

# Comando para copiar APK a un volumen montado
CMD ["cp", "/app/app/build/outputs/apk/release/app-release.apk", "/output/StreamPlayer-v10.0.apk"]