#!/bin/bash
list="longhorn-images.txt"
CONTAINER_CLI=${CONTAINER_CLI:-docker}

POSITIONAL=()
while [[ $# -gt 0 ]]; do
	key="$1"
	case $key in
		-i|--images)
		images="$2"
		shift # past argument
		shift # past value
		;;
		-l|--image-list)
		list="$2"
		shift # past argument
		shift # past value
		;;
		-p|--platform)
		platform="$2"
		shift # past argument
		shift # past value
		;;
		-h|--help)
		help="true"
		shift
		;;
		*)
		echo "Error! invalid flag: ${key}"
		help="true"
		break
		;;
	esac
done

usage () {
	echo "USAGE: $0 [--image-list longhorn-images.txt] [--images longhorn-images.tar.gz] [--platform linux/amd64]"
	echo "  [-l|--images-list path] text file with list of images. 1 per line."
	echo "  [-p|--platform linux/arch] if using images-list path, pulls the image with the specified platform"
	echo "  [-i|--images path] tar.gz generated by docker/podman save. If this flag is empty, the script does not export images to a tar.gz file"
	echo "  [-h|--help] Usage message"
 	echo ""
	echo "To use podman instead of docker set the environment variable CONTAINER_CLI=podman"
}

if [[ $help ]]; then
	usage
	exit 0
fi

set -e -x

for i in $(cat ${list}); do
    if [ -n "$platform" ]; then
        $CONTAINER_CLI pull ${i} --platform $platform
    else
        $CONTAINER_CLI pull ${i}
    fi
done

if [[ $images ]]; then
	$CONTAINER_CLI save $(cat ${list} | tr '\n' ' ') | gzip -c > ${images}
fi
