#!/bin/bash
#
# Copyright 2014-2023 Security Onion Solutions, LLC
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

. /usr/sbin/so-common
. /usr/sbin/so-image-common

usage() {
	read -r -d '' message <<- EOM
		usage: so-image-pull [-h] IMAGE [IMAGE ...]

		positional arguments:
			IMAGE         One or more 'so-' prefixed images to download and verify.

		optional arguments:
			-h, --help    Show this help message and exit.
	EOM
	echo "$message"
	exit 1
}

for arg; do
	shift
	[[ "$arg" = "--quiet" || "$arg" = "-q" ]] && quiet=true && continue
	set -- "$@" "$arg"
done

if [[ $# -eq 0 || $# -gt 1 ]] || [[ $1 == '-h' || $1 == '--help' ]]; then
	usage
fi

TRUSTED_CONTAINERS=("$@")
set_version

for image in "${TRUSTED_CONTAINERS[@]}"; do
	if ! docker images | grep "$image" | grep ":5000" | grep -q "$VERSION"; then
		if [[ $quiet == true ]]; then
			update_docker_containers "$image" "" "" "/dev/null"
		else
			update_docker_containers "$image" "" "" "" 
		fi
	else
		echo "$image:$VERSION image exists."
	fi
done
