#!/bin/bash

# generate a unique string from the full path to the USER.IMG
# this path is always unique for a given organization/domain/user/image combination, e.g.
# '/home/kylin-ksvd/ksvd-orgs/org-21/users/example.net/user/win7/USER.IMG'
# then md5sum it to generate a unique hash to be used as the BIOS Serial number
# then convert this md5 hash into a "Type 3 UUID" for use as the KVM System UUID

for parameter in "$@"; do
  if [[ $parameter =~ .*/home/kylin-ksvd/ksvd-orgs.*USER.IMG.* ]]; then
    # found the right parameter in the input string, now strip off everything around the actual filename
    temppath=${parameter##*file=}
    IMAGEPATH=${temppath%%,cache*}
  fi
done

SERIAL=`echo $IMAGEPATH | md5sum | cut -f 1 -d " "`
UUID=${SERIAL:0:8}-${SERIAL:8:4}-3${SERIAL:13:3}-${SERIAL:16:4}-${SERIAL:20:12}

exec /usr/lib/ksvd/bin/qemu-kvm.real -smbios type=1,serial=$SERIAL -uuid $UUID "$@"

