#!/bin/bash

latest="v4.43-9799-beta-2023.08.31"
lateststable="v4.42-9798-rtm-2023.06.30"
#Release Date: 2013-08-30
initfile="vpnserver"

echo "--------------------------------------------------------------------"
echo "SoftEther VPN Server Install script"
echo "By Ammar"
echo "https://thenoobstribe.com"
echo "In case of any problem, email Ammar at: ammar@thenoobstribe.com"
echo "--------------------------------------------------------------------"
echo "--------------------------------------------------------------------"
echo
echo "Select Architecture"
echo
echo " 1. Arm EABI (32bit)"
echo " 2. Intel x86 (32bit)"
echo " 3. Intel x64/AMD64 (64bit)"
echo
echo "Please choose architecture: "
read tmp
echo

if test "$tmp" = "3"
then
	arch="64bit_-_Intel_x64_or_AMD64"
	arch2="x64-64bit"
	echo "Selected : 3 " $arch
elif test "$tmp" = "2"
then
	arch="32bit_-_Intel_x86"
	arch2="x86-32bit"
	echo "Selected : 2 " $arch
elif test "$tmp" = "1"
then
	arch="32bit_-_ARM_EABI"
	arch2="arm_eabi-32bit"
	echo "Selected : 3 " $arch
else #default if non selected
	arch="32bit_-_Intel_x86"
	arch2="x86-32bit"
	echo "Selected : 1 " $arch
fi

echo "--------------------------------------------------------------------"
echo
echo "Select OS"
echo
echo " 1. Debian/Ubuntu"
echo " 2. CentOS/Fedora"
echo
echo "Please choose OS: "
read tmp
echo

if test "$tmp" = "2"
then
	os="cent"
	echo "Selected : 2 CentOS/Fedora"
else
	os="deb"
	echo "Selected : 1 Debian/Ubuntu"
fi

echo "--------------------------------------------------------------------"
echo
echo "Select build"
echo
echo " 1. latest(might include beta/rc)"
echo " 2. latest stable"
echo
echo "Please choose build: "
read tmp
echo

if test "$tmp" = "2"
then
	version="$lateststable"
	echo "Latest stable selected: 2 "$lateststable
else
	version="$latest"
	echo "Latest build(stable/beta) selected: 1 "$latest
fi

echo "Stoping VPN Server using systemctl stop vpnserver command"
echo
systemctl stop vpnserver
echo

file="softether-vpnserver-"$version"-linux-"$arch2".tar.gz"
link="http://www.softether-download.com/files/softether/"$version"-tree/Linux/SoftEther_VPN_Server/"$arch"/"$file

if [ ! -s "$file" ]||[ ! -r "$file" ];then
	#remove and redownload empty or unreadable file
	rm -f "$link"
	wget "$link"
elif [ ! -f "file" ];then
	#download if not exist
	wget "$file"
fi

if [ -f "$file" ];then
	tar xzf "$file"
	dir=$(pwd)
	echo "current dir " $dir
	cd vpnserver
	dir=$(pwd)
	echo "changed to dir " $dir
else
	echo "Archive not found. Please rerun this script or check permission."
	break
fi

if [ "$os" -eq "cent" ];then
	yum upgrade
	yum groupinstall "Development Tools" gcc
else
	apt-get update && apt-get upgrade -y && apt-get dist-upgrade -y
	apt-get install htop nload -y
	apt-get install whiptail -y
	apt-get install build-essential -y
fi

# making the executeable and pressing '1' a few times to accept the license agreement.
printf '1\n1\n1\n' | make
cp -r * /usr/local/vpnserver
dir=$(pwd)
echo "current dir " $dir
cd /usr/local/vpnserver/
dir=$(pwd)
echo "changed to dir " $dir
chmod 600 *
chmod 700 vpnserver
chmod 700 vpncmd
echo "Starting VPN Server using systemctl start vpnserver command"
systemctl start vpnserver
echo "VPN server started successfully"
process_id=$!
echo "PID: $process_id"
wait $process_id
echo "Exit status: $?"
rm -r /root/vpnserver
echo "Done..."
	
# echo "Waiting for 30 seconds to make sure everything has started and is ready to be configured..."
# sleep 30 &