# Copyright 2020 The Kubernetes Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

ARG BASEIMAGE
from $BASEIMAGE as prep

ENV CURL_VERSION=7.57.0
WORKDIR /curl
ADD https://skanthak.homepage.t-online.de/download/curl-$CURL_VERSION.cab curl.cab
ADD https://github.com/kubernetes-sigs/windows-testing/raw/master/images/busybox/busybox.exe /bin/busybox.exe

USER ContainerAdministrator
RUN FOR /f "tokens=*" %i IN ('C:\bin\busybox --list') DO mklink C:\bin\%i.exe C:\bin\busybox.exe
RUN expand /R curl.cab /F:* . &\
    del C:\curl\curl.cab &\
    setx /M PATH "C:\bin;C:\curl\;%PATH%"

# download bind and prepare a folder just for dig.
RUN powershell -Command "\
      curl.exe 'https://downloads.isc.org/isc/bind9/9.14.10/BIND9.14.10.x64.zip' -o /bind.zip; \
      Expand-Archive -Path C:\bind.zip -DestinationPath C:\bind; \
      $s = [System.Diagnostics.Process]::Start('C:\bind\vcredist_x64.exe', '/quiet'); \
      $s.WaitForExit(); \
      mkdir C:\dig; \
      cp C:\bind\dig.exe C:\dig\; \
      cp C:\bind\nslookup.exe C:\dig\; \
      cp C:\bind\*.dll C:\dig\; \
      cp C:\Windows\System32\vcruntime140.dll C:\dig\; \
      rm C:\bind.zip;"

FROM $BASEIMAGE

COPY --from=prep /curl/AMD64 /curl/CURL.LIC /curl/
COPY --from=prep /bin /bin
COPY --from=prep /dig /dig

ENV chocolateyUseWindowsCompression false
RUN powershell -Command "\
    iex ((new-object net.webclient).DownloadString('https://chocolatey.org/install.ps1')); \
    choco feature disable --name showDownloadProgress;
