#!/bin/bash

containerid="$1"
root="$2"
domid=`xl list | grep "$containerid" | awk '{print$2}'`
state="$root"/"$containerid/state.json"
mountpoint=`cat $state | jq '.["config"]["rootfs"]'`
mountpoint=`sed -e 's/^"//' -e 's/"$//' <<<"$mountpoint"`
bundledir="`dirname $mountpoint`"

if test "$domid"
then
    state="running"
elif test "$mountpoint"
then
    state="created"
fi

# need to guess the next domid
if ! test "$domid"
then
    domid=`xl list | tail -n 1 | awk '{print$2}'`
    domid=`expr $domid + 1`
fi

cat << EOF
{
  "ociVersion": "1.0.1-dev",
  "id": "$containerid",
  "pid": $domid,
  "status": "$state",
  "bundle": "$bundledir",
  "rootfs": "$mountpoint",
  "created": "`date +"%Y-%m-%dT%H:%M:%SZ"`",
  "owner": ""
}
EOF
