#!/bin/bash
RED='\033[0;31m'
NC='\033[0m' # No Color
BLUE='\033[0;34m'
YELLOW='\033[0;33m'
if readlink /proc/$$/exe | grep -qs "dash"; then
echo -e "${YELLOW}This script needs to be run with ${RED}bash, ${YELLOW}not sh"
exit 1
fi
if [[ "$EUID" -ne 0 ]]; then
echo -e "${YELLOW}Sorry, you need to run this as ${RED}root${YELLOW}, try ${BLUE}sudo ./install"
exit 2
fi
if [[ -e /etc/debian_version ]]; then
OS=debian
GROUPNAME=nogroup
RCLOCAL='/etc/rc.local'
elif [[ -e /etc/redhat-release ]]; then
OS=redhat
GROUPNAME=nogroup
RCLOCAL='/etc/rc.local'
else
echo -e "${RED}Looks like you are not running this installer on a Debian or RedHat system"
echo -e "${YELLOW}Please check the install instructions at ${BLUE}https://zsvpn.com/downloads"
exit 2
fi

echo -e "Installing ZSVPN-Linux 2.1.8"

#remove old files
rm -rf /etc/zsvpn &> /dev/null
rm -rf ~/.cache/zsvpn-gui &> /dev/null
rm -rf ~/.config/zsvpn-gui &> /dev/null
rm /usr/share/applications/zsvpn.desktop &> /dev/null

#prepare dependencies
mkdir /etc/zsvpn
if [ $OS == "debian" ]; then
apt update &> /dev/null 
apt install openvpn curl libcanberra-gtk-module -y &> /dev/null
fi
if [ $OS == "redhat" ]; then
xhost si:localuser:root
yum install openvpn openresolv -y &> /dev/null
fi


#install files
cp zsvpn-2.1.8.tar.gz /etc/zsvpn
cd /etc/zsvpn
tar -xvzf zsvpn-2.1.8.tar.gz --strip-components 1 &> /dev/null
cp lib/bin/linux/org.freedesktop.policykit.pkexec.policy /usr/share/polkit-1/actions/org.freedesktop.policykit.pkexec.zsvpn.policy
cp zsvpn.desktop /usr/share/applications


if openvpn --version | grep -qw "2.3.*"; then
sed -i '/ncp-ciphers AES-128-CBC:AES-256-CBC:AES-128-GCM:AES-256-GCM/d' /etc/zsvpn/lib/config/default.ovpn
sed -i '/remote-cert-tls server/d' /etc/zsvpn/lib/config/default.ovpn
fi
chmod +x /etc/zsvpn/zsvpn
rm /etc/zsvpn/zsvpn-2.1.8.tar.gz
echo -e "${YELLOW}Installation finished, You can open from the ZSVPN shortcut in the menu, otherwise goto ${BLUE}/etc/zsvpn and type sudo ./zsvpn"
pkexec /etc/zsvpn/zsvpn &> /dev/null && exit 0
