#!/bin/bash

containerid="$1"
crundir="$2"
bundle=$( cat "$crundir"/bundle )
pidfile=$( cat "$crundir"/pidfile )
domid="$(xl domid "$containerid")"
mountpoint=$( cat "$crundir"/rootfs )

if test "$domid"
then
    xenstate="$(xl list "$domid" 2> /dev/null | tail -n 1 | awk '{print$5}')"

    if [[ $xenstate =~ p ]]
    then
         state="created"
    elif [[ $xenstate =~ r|b ]]
    then
        state="running"
    else
        state="stopped"
    fi
else
    domid="$(xl list | tail -n 1 | awk '{print$2}')"
    domid="$(expr $domid + 1)"

    state="stopped"
fi

if test -f "$pidfile"
then
    pid=$( cat "$pidfile" )
else
    pid=$domid
fi

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