#!/bin/bash

mac=$(cat /sys/class/net/$(ip route show default | awk '/default/ {print $5}')/address)
hostname=$(cat /proc/sys/kernel/hostname)
username=$(whoami)

echo "Please enter your pin."
read pin
echo "Thank you for entering the pin: $pin. Beginning Privatise activation"
echo "Updating Files"
sudo apt-get update
echo "$mac"
echo "Installing OpenVPN"
sudo apt install openvpn -y
echo "Creating additional OpenVPN directories"
sudo mkdir -p /etc/openvpn/scripts
sudo mkdir -p /etc/openvpn/client
echo "Getting updated DNS Script"
sudo wget https://raw.githubusercontent.com/jonathanio/update-systemd-resolved/master/update-systemd-resolved -P /etc/openvpn/scripts/
echo "Installing updated DNS Script"
sudo chmod +x /etc/openvpn/scripts/update-systemd-resolved
echo "Installing curl"
sudo apt install curl
echo "Installing JQ"
sudo apt install jq
echo "Getting certs directory"
mainurl=$(curl -d '{"device_name":"'$hostname'", "device_username":"'$username'", "mac_address":"'$mac'"}' -H "Content-Type: application/json" -X POST https://users.privatise.com/api/linux-activation/$pin | jq ".url" -r)
echo "Getting ROC IP"
rocip=$(curl -d '{"device_name":"'$hostname'", "device_username":"'$username'", "mac_address":"'$mac'"}' -H "Content-Type: application/json" -X POST https://users.privatise.com/api/linux-activation/$pin | jq ".ip" -r)

echo "Saving specific certs..."
echo $mainurl
clientovpn=$(curl $mainurl | jq '."CLIENT.OVPN"' -r)
echo $clientovpn
clientkey=$(curl $mainurl | jq '."CLIENT.KEY"' -r)
echo $clientkey
clientfile=$(curl -k $mainurl | jq '."CLIENT.CRT"' -r)
echo $clientfile
cacertificate=$(curl -k $mainurl | jq '."CA.CRT"' -r)
echo $cacertificate
login=$(curl $mainurl | jq '."LOGIN.CONF"' -r)
echo $login
takey=$(curl $mainurl | jq '."TA.KEY"' -r)
echo $takey

sudo curl -l $clientovpn -H "Accept: application/octet-stream" > /etc/openvpn/client/client.ovpn.backup
sudo curl -l $clientkey -H "Accept: application/octet-stream" > /etc/openvpn/client/client.key
curl -k -l $clientfile -H "Accept: application/octet-stream" > /etc/openvpn/client/client.crt
curl -k -l $cacertificate -H "Accept: application/octet-stream" > /etc/openvpn/client/ca.crt
sudo curl -l $login -H "Accept: application/octet-stream" > /etc/openvpn/client/login.conf
sudo curl -l $takey -H "Accept: application/octet-stream" > /etc/openvpn/client/ta.key

sudo rm /etc/openvpn/client/client.ovpn
sudo cp /etc/openvpn/client/client.ovpn.backup /etc/openvpn/client/client.ovpn

echo "Updating config file"
sudo echo -e "\nscript-security 2" >> /etc/openvpn/client/client.ovpn
sudo echo -e "\nup /etc/openvpn/update-resolv-conf" >> /etc/openvpn/client/client.ovpn
sudo echo -e "\ndown /etc/openvpn/update-resolv-conf" >> /etc/openvpn/client/client.ovpn
sudo sed -i "s|139.28.216.82|$rocip|" /etc/openvpn/client/client.ovpn 

echo "Update directory location of files"
sudo sed -i "s|client.key|/etc/openvpn/client/client.key|" /etc/openvpn/client/client.ovpn 
sudo sed -i "s|client.crt|/etc/openvpn/client/client.crt|" /etc/openvpn/client/client.ovpn 
sudo sed -i "s|ca.crt|/etc/openvpn/client/ca.crt|" /etc/openvpn/client/client.ovpn 
sudo sed -i "s|login.conf|/etc/openvpn/client/login.conf|" /etc/openvpn/client/client.ovpn 
sudo sed -i "s|ta.key|/etc/openvpn/client/ta.key|" /etc/openvpn/client/client.ovpn 

sudo chmod -R 400 /etc/openvpn/client
sudo chmod 775 /etc/openvpn/client/client.ovpn
