#!/bin/bash

SETUP_URL="https://alexmib.ru/z/LinuxSetup"
SETUP_SUBDIR="scripts"
VBOX_ADD_URL="$SETUP_URL/$SETUP_SUBDIR/VirtualBoxAdd_7.0.0r153978.zip"


KERNELS_URL="https://alexmib.ru/z/kernels"
KERNEL_VERS=`uname -r`
KERNEL_DOWNLOAD_URL="$KERNELS_URL/kernel-uek-devel-$KERNEL_VERS.rpm"


GREEN=$'\e[0;32m'
RED=$'\e[0;31m'
NC=$'\e[0m'

function echo_red {
	echo "${RED}$1${NC}"
}

function echo_green {
	echo "${GREEN}$1${NC}"
}

function check_res {
	if [[ $? == 0 ]] 
	then
		echo_green "OK."
	else
		echo_red "Fail."
		ask_exit;
	fi   
}

function ask_exit {
	read -p "Continue ? (y/n)" ansv;
	if [[ $ansv == 'y' ]]
	then
		return 0;
	else
		echo_red "Exit script."
		exit 1;
	fi
}



 echo_green "Install Virtual BOX Tools"

 echo_green "Download kernel RPM."

 #wget https://alexmib.ru/z/linux-setup/kernels/kernel-uek-devel-5.4.17-2102.201.3.el7uek.x86_64.rpm
 curl -s $KERNEL_DOWNLOAD_URL --output /tmp/kernel-devel.rpm
 check_res
      
 echo_green "Install kernel RPM."
 yum install -y /tmp/kernel-devel.rpm
 check_res     

 rm /tmp/kernel-devel.rpm

 echo_green "Install needed RPMs."
 yum install -y bzip2.x86_64 libX11.x86_64 libXt.x86_64 libXext.x86_64 libXmu.x86_64 gcc.x86_64 make.x86_64 perl.x86_64 > /dev/null
 check_res     


 echo_green "Download VirtualBoxAdditions" 
 curl -s $VBOX_ADD_URL --output /tmp/VBoxAdd.zip
 check_res     
  
 echo_green "Unpack VirtualBoxAdditions"
 mkdir /tmp/VBoxAdd
 unzip -o /tmp/VBoxAdd.zip -d /tmp/VBoxAdd > /dev/null
 check_res     

 /tmp/VBoxAdd/VBoxLinuxAdditions.run

 check_res     













