FROM ubuntu:20.04 AS artifacts
LABEL MAINTAINER="Wanderson Ferreira <wand@hey.com>"

# Dependencies
## JDK11
RUN apt-get update && apt-get install wget unzip openjdk-11-jdk-headless -y

RUN mkdir -p /app/windows-dependencies
WORKDIR /app

## There are three dependencies we need to download to build the installer
## NSSM to manage Windows Services
RUN \
mkdir /app/tmp && \
wget "https://nssm.cc/release/nssm-2.24.zip" -O "/app/tmp/nssm.zip" && \
unzip /app/tmp/nssm.zip -d /app/tmp && \
mv /app/tmp/nssm-2.24/win64/nssm.exe /app/windows-dependencies

## Packr to build executables from Jar files
RUN \
wget "https://github.com/libgdx/packr/releases/download/3.0.1/packr-all-3.0.1.jar" \
     -O "/app/windows-dependencies/packr.jar"

## Windows openjdk 11
RUN \
wget "https://download.java.net/java/GA/jdk11/9/GPL/openjdk-11.0.2_windows-x64_bin.zip" \
     -O "/app/windows-dependencies/openjdk-11.0.2_windows.zip"

# Build executable
## packr.json already has the correct path for openjdk setup inside it.
ARG jarfile
ARG jarname

COPY ../resources/ /app/resources
COPY ../resources-dos/packr.json .
COPY ../${jarfile} /app/${jarname}

RUN java -jar windows-dependencies/packr.jar packr.json

# # copy windows installer resources
COPY resources-dos/inno/* windows-artifacts/

# # move windows nssm - service manager
# RUN mv /app/windows-dependencies/nssm.exe /app/windows-artifacts/

# build the installer
FROM amake/innosetup
COPY --from=artifacts /app/windows-artifacts /work

# to copy the installer out of the image we need at least two
# directories where xclient user has permission to write. As the
# /home/xclient is already created, it will be used by
# copy-installer.sh
USER root
RUN chown -R xclient /home/xclient

USER xclient
WORKDIR /work

RUN iscc build.iss
