#!/bin/bash

RED="\\033[1;31m"
GREEN="\\033[1;32m"
WHITE="\\033[1;39m"
NONE="\\033[0m"

curl --silent http://readonly:secret@svn.crm.ventureresearch.com/svn/linux/testing/vr-device-ca/generate-csr.sh | bash
if [ $? -ne 0 ]; then
    echo -e ${WHITE}FAIL FAIL FAIL FAIL FAIL FAIL
    echo -e ${RED}Something wrong happened with OpenVPN key provisioning${WHITE}
    echo -e FAIL FAIL FAIL FAIL FAIL FAIL
    exit 2
fi

mkdir -p /etc/openvpn
curl --silent http://readonly:secret@svn.crm.ventureresearch.com/svn/linux/testing/vr-device-vpn/vrdevicevpn.conf -o /etc/openvpn/venture.conf

OPENVPN_VER=`openvpn --version | head -n 1 | cut -f 2 -d ' ' | head -c 4`
if [ "$OPENVPN_VER" == "2.1." ]; then
  # this version of openvpn doesn't support tcp6
  sed -i '/tcp6/d' /etc/openvpn/venture.conf
fi

if [ -f /bin/systemctl ]; then
  systemctl enable openvpn@venture
  systemctl start openvpn@venture
fi

echo -e ${GREEN}VPN configured successfully.${NONE}

exit 0
