#!/bin/sh

set -x
set -e

if printf '%s\n' "$PWD" | grep -- travis >/dev/null 2>/dev/null; then
   true "travis detected, ok."
elif [ ! "$CI" = "true" ]; then
   true "ERROR: You probably do not want to run this outside of a CI \
environment, because it uses virsh to undefine virtual machines."
   exit 1
fi

mydir="$( cd "$( dirname "$0" )" && pwd )"
cd "$mydir"
cd ..
cd ..
cd ..

## Debugging.
cat /etc/apt/sources.list || true
ls -la /etc/apt/sources.list.d/* || true
cat /etc/apt/sources.list.d/* || true
ls -la /etc/apt/apt.conf.d/ || true
cat /etc/apt/apt.conf.d/* || true
ls -la /etc/apt/preferences.d/ || true
cat /etc/apt/preferences.d/* || true

## Version closer to Debian release.
# sudo --non-interactive rm -f /etc/apt/sources.list || true
# echo "deb http://us.archive.ubuntu.com/ubuntu/ disco main universe" | sudo --non-interactive tee -a /etc/apt/sources.list.d/disco.list
# echo "deb http://us.archive.ubuntu.com/ubuntu/ disco-security main universe" | sudo --non-interactive tee -a /etc/apt/sources.list.d/disco.list

## Debugging.
# cat /etc/apt/sources.list.d/disco.list

apt-get update

## Sanity tests.
apt-get --yes dist-upgrade
dpkg --configure -a

apt-get --yes install sudo virt-what lintian

## insserv: Service libvirtd has to be enabled to start service libvirt-guests
apt-get install --no-install-recommends -y qemu-kvm libvirt-daemon-system libvirt-clients virt-manager || true

apt-get install --no-install-recommends -y kmod || true

service libvirtd start || true
service virtlockd start || true
service virtlogd start || true
service virtlockd-admin start || true
service virtlogd-admin start || true

true "------------------------------------------------------------"
true "------------------------------------------------------------"
true "------------------------------------------------------------"

dpkg-query --show --showformat='${Package} ${Version} \n' qemu-kvm libvirt-daemon-system libvirt-clients virt-manager || true

true "------------------------------------------------------------"
true "------------------------------------------------------------"
true "------------------------------------------------------------"

#sudo --non-interactive chmod o+rwx /run/libvirt/libvirt-sock

true "------------------------------------------------------------"
true "------------------------------------------------------------"
true "------------------------------------------------------------"

test -f ./usr/share/libvirt-dist/xml/Whonix-External.xml

## /usr/bin/virt-xml-validate: cannot determine schema type for ./usr/share/libvirt-dist/xml/Whonix-External.xml
virt-xml-validate ./usr/share/libvirt-dist/xml/Whonix-External.xml || true

virsh -c qemu:///system net-define ./usr/share/libvirt-dist/xml/Whonix-External.xml
virsh -c qemu:///system net-autostart Whonix-External
virsh -c qemu:///system net-start Whonix-External || true

true "------------------------------------------------------------"
true "------------------------------------------------------------"
true "------------------------------------------------------------"

test -f ./usr/share/libvirt-dist/xml/Whonix-Internal.xml

virt-xml-validate ./usr/share/libvirt-dist/xml/Whonix-Internal.xml || true

virsh -c qemu:///system net-define ./usr/share/libvirt-dist/xml/Whonix-Internal.xml
virsh -c qemu:///system net-autostart Whonix-Internal
virsh -c qemu:///system net-start Whonix-Internal || true

true "------------------------------------------------------------"
true "------------------------------------------------------------"
true "------------------------------------------------------------"

## Debugging.

virsh -c qemu:///system net-dumpxml default
virsh -c qemu:///system net-dumpxml default --inactive

virsh -c qemu:///system net-dumpxml Whonix-External
virsh -c qemu:///system net-dumpxml Whonix-External --inactive

virsh -c qemu:///system net-dumpxml Whonix-Internal
virsh -c qemu:///system net-dumpxml Whonix-Internal --inactive

true "------------------------------------------------------------"
true "------------------------------------------------------------"
true "------------------------------------------------------------"

test -f ./usr/share/libvirt-dist/xml/Whonix-Gateway.xml
test -f ./usr/share/libvirt-dist/xml/Whonix-Workstation.xml
test -f ./usr/share/libvirt-dist/xml/Whonix-Custom-Workstation.xml
test -f ./usr/share/libvirt-dist/xml/Kicksecure.xml

true "------------------------------------------------------------"
true "------------------------------------------------------------"
true "------------------------------------------------------------"

## Travis CI issue:
## + virsh domxml-to-native qemu-argv ./usr/share/libvirt-dist/xml/Whonix-Gateway.xml
## error: invalid argument: could not find capabilities for domaintype=kvm
search="<domain type='kvm'>"
replace="<domain type='qemu'>"
sed -i -e "s/$search/$replace/g" ./usr/share/libvirt-dist/xml/Whonix-Gateway.xml
sed -i -e "s/$search/$replace/g" ./usr/share/libvirt-dist/xml/Whonix-Workstation.xml
sed -i -e "s/$search/$replace/g" ./usr/share/libvirt-dist/xml/Whonix-Custom-Workstation.xml
sed -i -e "s/$search/$replace/g" ./usr/share/libvirt-dist/xml/Kicksecure.xml

## Travis CI issue:
## + virsh domxml-to-native qemu-argv ./usr/share/libvirt-dist/xml/Whonix-Gateway.xml
## error: unsupported configuration: CPU mode 'host-passthrough' for x86_64 qemu domain on x86_64 host is not supported by hypervisor
search="<cpu mode='host-passthrough'/>"
replace=""
sed -i -e "s#$search#$replace#g" ./usr/share/libvirt-dist/xml/Whonix-Gateway.xml
sed -i -e "s#$search#$replace#g" ./usr/share/libvirt-dist/xml/Whonix-Workstation.xml
sed -i -e "s#$search#$replace#g" ./usr/share/libvirt-dist/xml/Whonix-Custom-Workstation.xml
sed -i -e "s#$search#$replace#g" ./usr/share/libvirt-dist/xml/Kicksecure.xml

## Travis CI issue:
## + virsh domxml-to-native qemu-argv ./usr/share/libvirt-dist/xml/Whonix-Gateway.xml
## error: Unable to open /dev/net/tun, is tun module loaded?: No such file or directory
modprobe tun || true

true "------------------------------------------------------------"
true "------------------------------------------------------------"
true "------------------------------------------------------------"

## TODO: virt-xml-validate does not work in Travis CI.
## /usr/bin/virt-xml-validate: cannot determine schema type for ./usr/share/libvirt-dist/xml/Whonix-Gateway.xml
virt-xml-validate ./usr/share/libvirt-dist/xml/Whonix-Gateway.xml || true
virt-xml-validate ./usr/share/libvirt-dist/xml/Whonix-Workstation.xml || true
virt-xml-validate ./usr/share/libvirt-dist/xml/Whonix-Custom-Workstation.xml || true
virt-xml-validate ./usr/share/libvirt-dist/xml/Kicksecure.xml || true

true "------------------------------------------------------------"
true "------------------------------------------------------------"
true "------------------------------------------------------------"

## Not working in nested VM such as CI.
## error: invalid argument: could not find capabilities for domaintype=kvm
virsh domxml-to-native qemu-argv ./usr/share/libvirt-dist/xml/Whonix-Gateway.xml || true
true "-----"
virsh domxml-to-native qemu-argv ./usr/share/libvirt-dist/xml/Whonix-Workstation.xml || true
true "-----"
virsh domxml-to-native qemu-argv ./usr/share/libvirt-dist/xml/Whonix-Custom-Workstation.xml || true
true "-----"
virsh domxml-to-native qemu-argv ./usr/share/libvirt-dist/xml/Kicksecure.xml || true
true "-----"

true "------------------------------------------------------------"
true "------------------------------------------------------------"
true "------------------------------------------------------------"

virsh -c qemu:///system define ./usr/share/libvirt-dist/xml/Whonix-Gateway.xml
virsh -c qemu:///system undefine Whonix-Gateway

virsh -c qemu:///system define ./usr/share/libvirt-dist/xml/Whonix-Workstation.xml
virsh -c qemu:///system undefine Whonix-Workstation

virsh -c qemu:///system define ./usr/share/libvirt-dist/xml/Whonix-Custom-Workstation.xml
virsh -c qemu:///system undefine Whonix-Custom-Workstation

virsh -c qemu:///system define ./usr/share/libvirt-dist/xml/Kicksecure.xml
virsh -c qemu:///system undefine Kicksecure

true "------------------------------------------------------------"
true "------------------------------------------------------------"
true "------------------------------------------------------------"

true "$0: All tests passed."
