#!/bin/bash

# Dynamic Scan for Machine Learning Models version 0.1.
#
# Copyright (c) 2025 Oracle, Inc.
# Licensed under the Universal Permissive License v 1.0 as shown at 
# https://oss.oracle.com/licenses/upl/

sudo chown "${USER}:${USER}" /var/run/docker.sock

MYLOC="$(dirname "$0")"
if [ '/' != "$(echo "${MYLOC}" | cut -c1)" ]; then
  if [ '.' = "${MYLOC}" ]; then
    MYLOC="$PWD"
  else
    MYLOC="$PWD/${MYLOC}"
  fi
fi

cd "${MYLOC}"

PYTHON_EXEC="$(cat config.ini | grep '^PYTHON_PATH' | /usr/bin/awk -F'=' '{print $2}' | /usr/bin/awk -F'#' '{print $1}' | /usr/bin/sed 's/^[ \t]*//;s/[ \t]*$//' | /usr/bin/envsubst)"
# Generate the Dockerfile
rm -f "prepare/Dockerfile"
"${PYTHON_EXEC}" -c "from env_config import ScanConfig; config = ScanConfig('config.ini'); config.generate_dockerfile()"

# Build the Docker image
image_name=`"${PYTHON_EXEC}" -c "from env_config import ScanConfig; config = ScanConfig('config.ini'); print(config.get_value('DockerEnv', 'image_name'))"`
docker build --build-arg USER_UID=$(id -u) -t "${image_name}" prepare
#docker build --no-cache -t "${image_name}" prepare

echo y | docker system prune > /dev/null 2>&1
