#!/bin/bash

workpath="$1"
containerid="$2"
crundir="$3"

bundle=$( cat "$crundir"/bundle )
pidfile=$( cat "$crundir"/pidfile )
mountpoint=$( cat "$crundir"/rootfs )
vm_uuid_file="$crundir"/vm_uuid

vm_uuid=""
state="halted"

if test -f "$vm_uuid_file"
then
  vm_uuid=$( cat "$vm_uuid_file" )
  if test "$vm_uuid"
  then
    state=`xe vm-list uuid="$vm_uuid" params=power-state --minimal`
  fi
fi

pid="0"
if test -f "$pidfile"
then
    pid=$( cat "$pidfile" )
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
