#!/usr/bin/env bash
# Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
#
# 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.

set -e
set -o pipefail

SCRIPT_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)"
source $SCRIPT_ROOT/common_vars.sh

# The base image is the kind-minimal image with a /etc/passwd file
# based from the minimal base, which is setup manually.  The root
# user's shell is configured as /sbin/nologin
# This doesnt work for the builder-base usage in Codebuild which runs 
# certain commands specifically as root.  We need the shell to be bash.
usermod --shell /bin/bash root

# directory setup
mkdir -p /go/src/github.com/aws/eks-distro

yum install -y \
    bc \
    bind-utils \
    bzip2 \
    cpio \
    curl \
    diffutils \
    docker \
    gettext \
    git-core \
    jq \
    less \
    make \
    net-tools \
    openssh-clients \
    openssl \
    patch \
    procps-ng \
    rsync \
    tar \
    unzip \
    vim \
    wget \
    which \
    yum-utils

time upx --best --no-lzma /usr/libexec/docker/cli-plugins/docker-buildx /usr/bin/{containerd,ctr,docker,dockerd}

# We see issues in fargate when installing on top of these images
# including this plugin appears to fix it
# ref: https://unix.stackexchange.com/questions/348941/rpmdb-checksum-is-invalid-trying-to-install-gcc-in-a-centos-7-2-docker-image    
if [ "$IS_AL23" = "false" ]; then 
    yum install -y yum-plugin-ovl
fi

if [ "${FINAL_STAGE_BASE}" = "full-copy-stage" ]; then
    yum install -y \
        gcc \
        openssl-devel \
        pkgconfig

    # for building containerd
    yum install -y \
        glibc-static \
        libseccomp-static


    if [ "$IS_AL23" = "false" ]; then
        # headers for btrfs do not exist in al23. well need to address this in the future
        # if we want to build containerd with btrfs support on al23
        yum install -y btrfs-progs-devel
    fi

    ############ IMAGE BUILDER ###################

    # user for goss/imagebuilder
    # to make sure the home dir is created correctly, tmp move the goss plugin
    mv /home/imagebuilder/.packer.d /tmp

    rm -rf /home/imagebuilder
    useradd -ms /bin/bash -u 1100 imagebuilder

    mv /tmp/.packer.d /home/imagebuilder/

    # https://github.com/kubernetes-sigs/image-builder/blob/main/images/capi/hack/ensure-ansible.sh

    ansible-galaxy collection install -p /home/imagebuilder/.ansible/collections \
        community.general \
        ansible.posix \
        'ansible.windows:>=1.7.0' \
        community.windows

    chown -R imagebuilder:imagebuilder /home/imagebuilder

    if [ "$IS_AL23" = "true" ]; then
        yum install -y python3-pip python
    else
        # we built python3.9 from source, ensure libs are all installed
        yum install -y zlib bzip2 readline sqlite openssl11 tk libffi xz
    fi
    ##############################################
fi

#################### CLEANUP ####################
# truncate logs
find /var/log -type f | while read file; do echo -ne '' > $file; done

# Removing doc and man files
# to get all symlinks run twice
for i in {1..2}; do
    find /usr/share/{doc,man} \( -xtype l -o -type f \) \
        ! \( -iname '*lice*' -o -iname '*copy*' -o -iname '*gpl*' -o -iname '*not*' -o -iname "*credits*" \) \
        -delete
done
find /usr/share/{doc,man} -type d -empty -delete

rm -rf /root/.cache
