FROM ubuntu:focal

RUN export DEBIAN_FRONTEND=noninteractive && \
  apt-get update && \
  apt-get dist-upgrade --yes && \
  apt-get install --yes curl sudo jq squashfs-tools tzdata patch wget

ENV KDE_FRAMEWORK="kde-frameworks-5-99-qt-5-15-7-core20"

 # Core
RUN mkdir -p /snap/core && curl -L $(curl -H 'X-Ubuntu-Series: 16' 'https://api.snapcraft.io/api/v1/snaps/details/core' | jq '.download_url' -r) --output core.snap && unsquashfs -d /snap/core/current core.snap && rm core.snap

# Core18
RUN curl -L $(curl -H 'X-Ubuntu-Series: 16' 'https://api.snapcraft.io/api/v1/snaps/details/core18' | jq '.download_url' -r) --output core18.snap && \
  mkdir -p /snap/core18 && unsquashfs -d /snap/core18/current core18.snap && rm core18.snap

# Core20
RUN  curl -L $(curl -H 'X-Ubuntu-Series: 16' 'https://api.snapcraft.io/api/v1/snaps/details/core20' | jq '.download_url' -r) --output core20.snap && \
  mkdir -p /snap/core20 && unsquashfs -d /snap/core20/current core20.snap && rm core20.snap

# Snapcraft
RUN curl -L $(curl -H 'X-Ubuntu-Series: 16' 'https://api.snapcraft.io/api/v1/snaps/details/snapcraft' | jq '.download_url' -r) --output snapcraft.snap && \
  mkdir -p /snap/snapcraft && unsquashfs -d /snap/snapcraft/current snapcraft.snap && rm snapcraft.snap

# gtk-common-themes
RUN curl -L $(curl -H 'X-Ubuntu-Series: 16' 'https://api.snapcraft.io/api/v1/snaps/details/gtk-common-themes' | jq '.download_url' -r) --output gtk-common-themes.snap && \
  mkdir -p /snap/gtk-common-themes && unsquashfs -d /snap/gtk-common-themes/current gtk-common-themes.snap && rm gtk-common-themes.snap

# ${KDE_FRAMEWORK}
RUN curl -L $(curl -H 'X-Ubuntu-Series: 16' "https://api.snapcraft.io/api/v1/snaps/details/${KDE_FRAMEWORK}" | jq '.download_url' -r) --output ${KDE_FRAMEWORK}.snap && \
  mkdir -p /snap/${KDE_FRAMEWORK} && unsquashfs -d /snap/${KDE_FRAMEWORK}/current ${KDE_FRAMEWORK}.snap && rm ${KDE_FRAMEWORK}.snap

# ${KDE_FRAMEWORK}-sdk
RUN curl -L $(curl -H 'X-Ubuntu-Series: 16' "https://api.snapcraft.io/api/v1/snaps/details/${KDE_FRAMEWORK}-sdk" | jq '.download_url' -r) --output ${KDE_FRAMEWORK}-sdk.snap && \
  mkdir -p /snap/${KDE_FRAMEWORK}-sdk && unsquashfs -d /snap/${KDE_FRAMEWORK}-sdk/current ${KDE_FRAMEWORK}-sdk.snap && rm ${KDE_FRAMEWORK}-sdk.snap

# Patch snapcraftctl script to use python environment
COPY ./snapcraft-core20.patch /tmp/snapcraft-core20.patch
RUN patch -b -i /tmp/snapcraft-core20.patch /snap/snapcraft/current/bin/scriptlet-bin/snapcraftctl

# Cleanup unused packages
RUN apt remove --yes --purge curl jq squashfs-tools patch && \
  apt-get autoclean --yes && \
  apt-get clean --yes

# Generate locale and install dependencies
RUN apt update && apt dist-upgrade --yes && apt install --yes sudo locales snapd && locale-gen en_US.UTF-8

# Create a snapcraft runner (TODO: move version detection to the core of snapcraft)
RUN mkdir -p /snap/bin
RUN echo "#!/bin/sh" > /snap/bin/snapcraft
RUN snap_version="$(awk '/^version:/{print $2}' /snap/snapcraft/current/meta/snap.yaml)" && echo "export SNAP_VERSION=\"$snap_version\"" >> /snap/bin/snapcraft
RUN echo 'exec "$SNAP/usr/bin/python3" "$SNAP/bin/snapcraft" "$@"' >> /snap/bin/snapcraft
RUN chmod +x /snap/bin/snapcraft

ENV LANG="en_US.UTF-8"
ENV LANGUAGE="en_US:en"
ENV LC_ALL="en_US.UTF-8"
ENV PATH="/snap/bin:$PATH"
ENV SNAP="/snap/snapcraft/current"
ENV SNAP_NAME="snapcraft"
ENV SNAP_ARCH="amd64"
ENV SNAPRAFT_BUILD_INFO=1
ENV SNAPCRAFT_SETUP_CORE=1
ENV PYTHONPATH="$SNAP/lib/python3.8/site-packages"
