#!/usr/bin/env bash
set -euox pipefail

# Check for root
if [[ $EUID -ne 0 ]]; then
    echo "Please run this script as root"
    exit 1
fi

# Global vars to be used
# shellcheck source=vars
source "$(dirname "${BASH_SOURCE[0]}")"/vars

pushd "$ARCHIVE_PATH"

# Untar the bundle
tar xfvz "$ARCHIVE"
pushd "$BUNDLE"

# Install and prepare config
make install

# Start CRI-O
systemctl daemon-reload
systemctl start crio
systemctl is-active --quiet crio && echo CRI-O is running

# Run a workload
crictl version
crictl run \
    "$GIT_ROOT/test/testdata/container_redis.json" \
    "$GIT_ROOT/test/testdata/sandbox_config.json"

# Test the workloads status
if [[ ! $(crictl pods -s Ready -q) ]]; then
    echo "No running pod found"
    exit 1
fi

if [[ ! $(crictl ps --state Running -q) ]]; then
    echo "No running container found"
    exit 1
fi

# Cleanup
crictl rmp -fa
systemctl stop crio
make uninstall
