#!/bin/sh

con=hvc0
tty -s && con=hvc2

img={{#qn_image}}{{qn_image}}{{/qn_image}}{{^qn_image}}infix-x86_64.img{{/qn_image}}
imgsz=$((($(stat -Lc %s $img) + 1023) >> 10))
imgdir=./.$(realpath $img | sed -e s:/:-:g)

unsquashfs -n -f -d $imgdir $img boot/bzImage >/dev/null


{{> inc/infix-disk}}
{{> inc/infix-usb}}

truncate -s 0 $imgdir/{{name}}.mactab
{{#links}}
echo "{{qn_name}}	{{qn_mac}}" >>$imgdir/{{name}}.mactab
{{/links}}

exec qemu-system-x86_64 -M pc,accel=kvm:tcg -cpu max \
  -m {{#qn_mem}}{{qn_mem}}{{/qn_mem}}{{^qn_mem}}384M{{/qn_mem}} \
  -kernel $imgdir/boot/bzImage -initrd $img \
  -append "root=/dev/ram0 ramdisk_size=$imgsz console=$con,115200 $append {{qn_append}}" \
  -drive file={{name}}.disk,if=virtio,format=raw,bus=0,unit=1  \
{{> inc/qemu-links}}
  -fw_cfg name=opt/mactab,file=$imgdir/{{name}}.mactab \
   $usb_cmd \
{{> inc/infix-common}}
