#!/bin/sh

IMG="$HOME/.local/share/windows10.img"

if ! which qemu-system-x86_64 >/dev/null; then
    echo qemu not installed
    exit 1
fi

mkdir -p $(dirname "$IMG")

win10() {
    qemu-system-x86_64 \
        -enable-kvm \
        -m size=4G,maxmem=4G \
        -cpu host \
        -smp $(nproc) \
        -display sdl,gl=on \
        -audiodev pa,id=snd0 \
        -device ich9-intel-hda \
        -device hda-output,audiodev=snd0 \
        -drive file="$IMG",format=qcow2 \
        "$@"
}

win10_install() {
    test -f "$HOME/Downloads/win10.iso" || {
        echo "No installation media" >&2
        exit 1
    }

    win10 \
        -cdrom "$HOME/Downloads/win10.iso" \
        -boot order=d \
        -nic none \
        "$@"
}

win10_format() {
    qemu-img create -f qcow2 "$IMG" 100G

    win10_install
}

if test "$#" -eq 0; then
    if ! test -f "$IMG"; then
        win10_format
    else
        win10
    fi
else
    case "$1" in
        install)
            shift
            win10_install
            ;;
        format)
            shift
            win10_format
            ;;
        run)
            shift
            win10
            ;;
    esac
fi
