#!/bin/bash

set -e

if [ ! -d ~/.wifichallengelab-docker ]; then
    mkdir -p ~/.wifichallengelab-docker
    cp -rd /usr/share/wifichallengelab-docker/* ~/.wifichallengelab-docker
fi

if ! modinfo vboxdrv; then
    echo "The module virtualbox is not built for your kernel"
    echo ""
    echo "Please install the package"
    echo "Run 'sudo apt install linux-headers-ARCH' (ARCH is the architecture of your machine)"
    echo ""
    echo "Then run 'sudo apt reinstall virtualbox-dkms'"
    exit 1
fi

if VBoxManage --version | grep WARNING; then
    echo ""
    echo "Run 'sudo apt reinstall virtualbox-dkms'"
    exit 1
fi

systemctl start libvirtd

echo ""
echo "Moving to ~/.wifichallengelab-docker/vagrant"
echo "It will create and start the VM with 'vagrant up --provider virtualbox virtualbox_vm'"
echo ""
echo "If this is the first time, it may be long"
echo ""
cd  ~/.wifichallengelab-docker/vagrant
vagrant up --provider virtualbox virtualbox_vm || true

echo "Now you can run wifichallengelab-docker"
echo "Run 'wifichallengelab-docker-start'"
