#!/bin/sh
#
# podman-socat - execute podman while capturing API stream. runs sandboxed podman
#
# script will run an instance of podman sandboxed
# API stream will be captured and then formatted for readability

usr=$(/usr/bin/id -u)

if [ "${usr}" != 0 ]; then
  echo >&2 "$(basename "$0") must be run as root."
  exit 2
fi

if ! command -v socat >/dev/null 2>&1; then
  echo 1>&2 "socat not found on PATH"
fi

PODMAN="${PODMAN:-podman}"
if ! command -v "${PODMAN}" >/dev/null 2>&1; then
  echo 1>&2 "${PODMAN} not found on PATH"
fi

usage() {
  echo 1>&2 "$0" '[-h] [-x]'
}

while getopts 'hx' arg; do
  case "${arg}" in
    h)
      usage
      exit 0
      ;;
    x)
      VERBOSE='-v'
      export PODMAN_LOG_LEVEL=debug
      ;;
    \?)
      usage
      exit 2
      ;;
    *)
      usage
      exit 2
      ;;
  esac
done
shift "$((OPTIND - 1))"

cleanup() {
  set +xeu
  rm -r "$1"
  kill "${REAP_PIDS}"

  sed -e 's/^> /\nClient Request> /' -e 's/^< /\nServer Response< /' -i /tmp/podman-socat.log
}

# create temporary directory for storage
TMPDIR=$(mktemp -d /tmp/podman.XXXXXX)
trap cleanup "${TMPDIR}" EXIT

# need locations to store stuff
# mkdir -p "${TMPDIR}"/{podman,crio,crio-run,cni/net.d,ctnr,tunnel}
x='podman
crio
crio-run
cni
cni/net.d
ctnr
tunnel
'
printf '%s' "${x}" \
  | while IFS='' read -r word; do
    mkdir -p "${TMPDIR}"/"${word}.dat"
  done

export CONTAINERS_REGISTRIES_CONF="${TMPDIR}"/registry.conf
cat >"${CONTAINERS_REGISTRIES_CONF}" <<-EOT
  [registries.search]
    registries = ['docker.io']
  [registries.insecure]
    registries = []
  [registries.block]
    registries = []
EOT

export CNI_CONFIG_PATH="${TMPDIR}"/cni/net.d
cat >"${CNI_CONFIG_PATH}"/87-podman-bridge.conflist <<-EOT
{
  "cniVersion": "0.3.0",
  "name": "podman",
  "plugins": [{
      "type": "bridge",
      "bridge": "cni0",
      "isGateway": true,
      "ipMasq": true,
      "ipam": {
        "type": "host-local",
        "subnet": "10.88.0.0/16",
        "routes": [{
          "dst": "0.0.0.0/0"
        }]
      }
    },
    {
      "type": "portmap",
      "capabilities": {
        "portMappings": true
      }
    }
  ]
}
EOT

PODMAN_ARGS="--storage-driver=vfs \
  --root=${TMPDIR}/crio \
  --runroot=${TMPDIR}/crio-run \
  --network-config-dir=${CNI_CONFIG_PATH} \
  --cgroup-manager=systemd \
  "
if [ -n "${VERBOSE}" ]; then
  PODMAN_ARGS="${PODMAN_ARGS} --log-level=${PODMAN_LOG_LEVEL} --syslog=true"
fi
PODMAN="${PODMAN} ${PODMAN_ARGS}"

PODMAN_HOST="${TMPDIR}/podman/podman-socat.sock"
SOCAT_HOST="${TMPDIR}/podman/podman.sock"

cat <<-EOT
Podman service running at unix://${SOCAT_HOST}
See /tmp/podman-socat.log for API stream capture
See /tmp/podman-service.log for service logging

Usage: sudo bin/podman-remote --url unix://${SOCAT_HOST} images

^C to exit
EOT

${PODMAN} system service \
  --timeout=0 "unix://${PODMAN_HOST}" >/tmp/podman-service.log 2>&1 &
REAP_PIDS=$!

socat -v "UNIX-LISTEN:${SOCAT_HOST}",fork,reuseaddr,unlink-early \
  "UNIX-CONNECT:${PODMAN_HOST}" >/tmp/podman-socat.log 2>&1
