#!/bin/bash
export TEXTDOMAINDIR='/usr/share/locale'
export TEXTDOMAIN='podsec-inotify'

export U7S_TRIVY_SERVER

export cmd=$0
source podsec-inotify-functions
getTrivyServer

function format() {
  echo "$(gettext 'Format'): $cmd <image_name> [ <docker file> [ <context directory> [ <podman build options> ] ] ]"
  exit 1
}

if [ $# -eq 0 ]
then
    format
fi

export PROGNAME=$(basename $0)
export container_name=$1
dockerfile=''

if [ $# -gt 1 ]
then
    dockerfile=$2
    shift; shift
    if [ $# -gt 0 ]
    then
        context=$1
        shift
    else
        context='.'
    fi
    # Создание контейнера из Dockerfile, в $@ список переданных параметров для podman build
    if podman build "$@" -f "$dockerfile" -t "${container_name}" "$context"; then :;
    else
        message="$(gettext 'Building image') ${container_name} $(gettext 'failed'). $(gettext 'Image analysis is skipped'.)"
        echo "$message" >&2
        priority=6
        prefix='Fatal'
        logger -p "$priority" -t "${PROGNAME}" "$prefix: $message"
        exit 2
    fi
fi

trivyCheckImage ${PROGNAME} ${container_name} $dockerfile
retCode=$?
exit $retCode
