#!/bin/bash

workpath=/usr/share/runX

containerid="$1"
crundir="$2"
bundle=$( cat "$crundir"/bundle )
pidfile=$( cat "$crundir"/pidfile )
configfile="$bundle"/config.json
mountpoint=$( cat "$crundir"/rootfs )
appname=`cat $configfile | jq '.["Path"]'`
cmdline=\"`cat $configfile | jq  -c -r '.["process"]["args"] | join("\" \"")'`\"
env=`cat $configfile | jq  -c -r '.["process"]["env"] | join("\" \"")'`
xlconf=""
kernel="$workpath/kernel"
ramdisk="$workpath/initrd"
for i in $env
do
    i=$(echo $i | tr -d \")
    if [[ $i = XLCONF=* ]]
    then
        xlconf=${i#XLCONF=}
        if [[ $xlconf = $mountpoint* ]]
        then
            # Don't allow the container to use XLCONF
            xlconf=""
        fi
    fi
    if [[ $i = RUNX_KERNEL=* ]]
    then
        kernel=${i#RUNX_KERNEL=}
        kernel="$mountpoint"/"$kernel"
    fi
    if [[ $i = RUNX_RAMDISK=* ]]
    then
        ramdisk=${i#RUNX_RAMDISK=}
        ramdisk="$mountpoint"/"$ramdisk"
    fi
done

# netconf is file,type[,ip]
netconf=`cat $configfile | jq  -c -r  '.["process"]["env"][] | select(contains("NETCONF"))'`
netconf=`echo "$netconf" | awk -F "=" '{print $2}'`
if test "$netconf"
then
    netfile=`echo "$netconf" | awk -F "," '{print $1}'`
    netname=`echo "$netconf" | awk -F "," '{print $2}'`
    netaddr=`echo "$netconf" | awk -F "," '{print $3}'`
    nettype=`cat $netfile | jq -c -r "select(.[\"name\"] == \"$netname\") | .[\"type\"]"`

    if test "$nettype" = "bridge"
    then
        pvcalls=0
        bridge=`cat $netfile | jq -c -r "select(.[\"name\"] == \"$netname\") | .[\"bridge\"]"`
        gw=`cat $netfile | jq -c -r "select(.[\"name\"] == \"$netname\") | .[\"ipam\"][\"gateway\"]"`
        route=`cat $netfile | jq -c -r "select(.[\"name\"] == \"$netname\") | .[\"ipam\"][\"subnet\"]"`
    else
        #shouldn't get here, but if we do assume pvcalls
        pvcalls=1
    fi
else
    pvcalls=1
fi

outconfig=$crundir/xen_vm.cfg
rm $outconfig &> /dev/null

echo "kernel='$kernel'" >> $outconfig
if test "$ramdisk"
then
    echo "ramdisk='$ramdisk'" >> $outconfig
fi
echo "memory = 1024" >> $outconfig
echo "vcpus = 2" >> $outconfig
echo "serial='pty'" >> $outconfig
echo "boot='c'" >> $outconfig
if test $pvcalls -eq 0
then
    echo "vif=['bridge="$bridge"']" >> $outconfig
    if test "$netaddr"
    then
        echo extra=\'console=hvc0 root=9p ip=$netaddr gw=$gw route=$route\' >> $outconfig
    else
        echo extra=\'console=hvc0 root=9p ip=dhcp\' >> $outconfig
    fi
else
    echo "pvcalls=['']" >> $outconfig
    echo extra=\'console=hvc0 root=9p pvcalls=1\' >> $outconfig
fi
echo "vfb=['vnc=1']" >> $outconfig
echo "p9=[ 'tag=share_dir,security_model=none,path=$mountpoint' ]" >> $outconfig
echo "name=\"$containerid\"" >> $outconfig
if test -f "$xlconf"
then
    cat "$xlconf" >> $outconfig
fi

echo $cmdline > $mountpoint/cmdline
xl create -p $outconfig > /dev/null 2>&1

pid=$( ps | grep -v grep | grep "xl create -p $outconfig" | awk '{print $1}' )
echo -n "$pid" > "$pidfile"
