#!/bin/bash
set -e

HERE=$(dirname "$(readlink --canonicalize "$BASH_SOURCE")")
. "$HERE/../scripts/_env"
. "$HERE/_env"
. "$ROOT/scripts/_trap"

PROVIDER=${PROVIDER:-simple}
NAMESPACE=${NAMESPACE:-workspace}
REPOSITORY=${REPOSITORY:-catalog}

if [ "$PROVIDER" == openshift ]; then
	REPOSITORY=$NAMESPACE
fi

m "provider: $PROVIDER"

mkdir --parents "$WORKSPACE"

m 'cleaning up...'

kubectl create namespace "$NAMESPACE" || true
kubectl config set-context --current --namespace="$NAMESPACE"

kubectl delete --ignore-not-found=true pods hello-world
kubectl delete --ignore-not-found=true secret reposure-simple-htpasswd
kubectl delete --ignore-not-found=true secret reposure-simple-authorization
reposure operator uninstall --wait -v
reposure simple uninstall --wait -v
kubectl delete events --all

if [ "$1" == '-b' ]; then
	"$ROOT/scripts/build-container-images"
	"$ROOT/scripts/publish-container-images"
fi

m 'installing operator...'

if [ "$PROVIDER" == minikube ] || [ "$PROVIDER" == openshift ]; then
	reposure operator install --role=view --wait -v
else
	reposure operator install --wait -v
fi

if [ "$PROVIDER" == simple ]; then
	m 'creating htpasswd secret...'

	rm --recursive --force "$WORKSPACE/htpasswd"
	# Note: Docker registry requires bcrypt
	# https://docs.docker.com/registry/configuration/#htpasswd
	htpasswd -cbB "$WORKSPACE/htpasswd" username password
	kubectl create secret generic reposure-simple-htpasswd \
		--from-file="$WORKSPACE/htpasswd"

	m 'installing simple registry...'

	# Authentication requires Cert-Manager
	# Authorization requires "reposure-simple-htpasswd" secret 
	reposure simple install --authentication --authorization --wait

	m 'creating authorization secret...'

	HOST=$(reposure simple host)
	kubectl create secret docker-registry reposure-simple-authorization \
		--docker-server="$HOST" \
		--docker-username=username \
		--docker-password=password
fi

m 'creating registry...'

if [ "$PROVIDER" == simple ]; then
	reposure registry create default --provider="$PROVIDER" --authorization-secret=reposure-simple-authorization --wait -v
else
	reposure registry create default --provider="$PROVIDER" --wait -v
fi

m "pushing text file to \"$REPOSITORY/hello\""

echo 'hello world' > "$WORKSPACE/hello.txt"
reposure image push default "$REPOSITORY/hello" "$WORKSPACE/hello.txt" -v

sleep 1

m "pulling text file from \"$REPOSITORY/hello\""

reposure image pull default "$REPOSITORY/hello" --unpack -v

m 'creating container image tarball'

HOST=$(reposure registry info default host)
PUBLIC_IMAGE=docker.io/paulbouwer/hello-kubernetes:1.8
PRIVATE_IMAGE=$HOST/$REPOSITORY/myimage
IMAGE_FILE=$WORKSPACE/myimage.tar

podman rmi "$PRIVATE_IMAGE" || true
podman rmi "$PUBLIC_IMAGE" || true
podman pull "$PUBLIC_IMAGE"
podman tag "$PUBLIC_IMAGE" "$PRIVATE_IMAGE"
rm --force "$IMAGE_FILE"
podman save "$PRIVATE_IMAGE" --output "$IMAGE_FILE"

m "pushing container image tarball to \"$REPOSITORY/myimage\""

reposure image push default "$REPOSITORY/myimage" "$IMAGE_FILE" -v

sleep 8

m "pulling container image tarball from \"$REPOSITORY/myimage\""

reposure image pull default "$REPOSITORY/myimage" -v > "$WORKSPACE/myimage-pulled.tar"

m "creating \"hello-world\" pod"

if [ "$PROVIDER" == simple ]; then
	IMAGE=$PRIVATE_IMAGE SECRET=reposure-simple-authorization envsubst < "$ROOT/assets/kubernetes/hello-world-with-secret.yaml" | \
	kubectl apply -f -
else
	IMAGE=$PRIVATE_IMAGE envsubst < "$ROOT/assets/kubernetes/hello-world.yaml" | \
	kubectl apply -f -
fi

m 'listing images'

# Not permitted on OpenShift
if [ "$PROVIDER" != openshift ]; then
	reposure image list default -v
fi

