#!/usr/bin/bash

set -euo pipefail

sudo -u svc-teststation cp ~/hwsi_tests/master.runfiles/zoox/experimental/hwsd/deploy/setup.sh ~/hwsi_tests/master.runfiles/zoox/scripts/setup/

cd ~
sudo -u svc-teststation wget "https://bootstrap.pypa.io/get-pip.py" -P "hwsi_tests/master.runfiles/zoox/scripts/setup/"
sudo -u svc-teststation mkdir ~/hwsi_tests/master.runfiles/zoox/scripts/setup/clang
sudo -u svc-teststation wget "https://llvm.org/svn/llvm-project/cfe/trunk/tools/clang-format/git-clang-format" -P "hwsi_tests/master.runfiles/zoox/scripts/setup/clang/"
chmod 755 hwsi_tests/master.runfiles/zoox/scripts/setup/clang/git-clang-format
sudo -u svc-teststation ./hwsi_tests/master.runfiles/zoox/experimental/hwsd/deploy/symlink_setup.sh

cd ~
sudo -u svc-teststation ~/hwsi_tests/master.runfiles/zoox/scripts/setup/setup.sh dev

cd ~
sudo -u svc-teststation echo '' >> .bashrc
sudo -u svc-teststation echo "source ~/hwsi_tests/master.runfiles/zoox/scripts/shell/zooxrc.sh" >> .bashrc

pip install saleae
pip install pyusb
pip install pyvisa-py
pip install pyvisa
pip install python-usbtmc
pip install pyudev

pip3 install saleae
pip3 install pyusb
pip3 install pyvisa-py
pip3 install pyvisa
pip3 install python-usbtmc
pip3 install pyudev

echo 'SUBSYSTEMS=="usb", ACTION=="add", ATTRS{idVendor}=="ffff", ATTRS{idProduct}=="9130", GROUP="dialout", MODE="0660"' >> /etc/udev/rules.d/usbtmc.rules
echo 'KERNEL=="usbtmc/*", MODE="0660", GROUP="dialout"' >> /etc/udev/rules.d/usbtmc.rules
echo 'KERNEL=="usbtmc[0-9]", MODE="0660", GROUP="dialout"' >> /etc/udev/rules.d/usbtmc.rules

sudo cp ~/hwsi_tests/master.runfiles/zoox/third_party/stlink/st-flash /usr/local/bin/

sudo -u svc-teststation mkdir /etc/zoox/current_config
sudo -u svc-teststation cp ~/hwsi_tests/master.runfiles/zoox/experimental/hwsd/deploy/test_station.json /etc/zoox/current_config/vehicle.json

echo '' >> /etc/network/interfaces
echo 'auto can0' >> /etc/network/interfaces
echo 'allow-hotplug can0' >> /etc/network/interfaces
echo 'iface can0 inet manual' >> /etc/network/interfaces
echo '  pre-up ip link set can0 type can bitrate 500000' >> /etc/network/interfaces

cd ~
sudo -u svc-teststation wget "https://downloads.saleae.com/logic/1.2.18/Logic%201.2.18%20(64-bit).zip"
sudo -u svc-teststation unzip Logic\ 1.2.18\ \(64-bit\).zip

cd Logic\ 1.2.18\ \(64-bit\)/Drivers/
./install_driver.sh

apt-get install isc-dhcp-server

echo '' >> /etc/network/interfaces
echo 'auto eth0' >> /etc/network/interfaces
echo 'iface eth0 inet dhcp' >> /etc/network/interfaces
DEFAULTGW=$(route | awk '/default/ {print $2}')
echo "up route add default gw $DEFAULTGW eth0" >> /etc/network/interfaces

echo '' >> /etc/network/interfaces
echo 'auto eth1' >> /etc/network/interfaces
echo 'iface eth1 inet static' >> /etc/network/interfaces
echo 'address 192.168.0.1' >> /etc/network/interfaces
echo 'netmask 255.255.255.0' >> /etc/network/interfaces
echo 'network 192.168.0.0' >> /etc/network/interfaces
echo 'gateway 192.168.0.1' >> /etc/network/interfaces
echo 'up route del default gw 192.168.0.1 eth1' >> /etc/network/interfaces

echo '' >> /etc/network/interfaces
echo 'auto eth2' >> /etc/network/interfaces
echo 'iface eth2 inet dhcp' >> /etc/network/interfaces

echo 'subnet 192.168.0.0 netmask 255.255.255.0 {' >> /etc/dhcp/dhcpd.conf
echo 'option routers 192.168.0.1;' >> /etc/dhcp/dhcpd.conf
echo 'option subnet-mask 255.255.255.0;' >> /etc/dhcp/dhcpd.conf
echo 'range 192.168.0.2 192.168.0.200;' >> /etc/dhcp/dhcpd.conf
echo '}' >> /etc/dhcp/dhcpd.conf

sed -i 's/INTERFACES=""/INTERFACES="eth1"/g' /etc/default/isc-dhcp-server

sed -i 's/sleep [0-9]*/sleep 1/g' /etc/init/failsafe.conf

echo '*** Please reboot your machine when ready ***'
