#!/usr/bin/env bash
# TA-Proxmenu - Proxmox Scripts for TA Use


[ "${2}" != "q" ] && source /opt/idssys/defaults/colors.inc
source /opt/idssys/defaults/default.inc
source /opt/idssys/ta-proxmenu/defaults.inc

INSTALL_PULSE() {
  read -n 1 -p "Are you sure you wish to install Pulse (Y/n)?" choice
  case "$choice" in
  [Nn]) MAIN_MENU;;
  * )
    echo
	curl -fsSL https://github.com/rcourtman/Pulse/releases/latest/download/install.sh | bash
	echo
	echo -e "\n${idsCL[Green]}Pulse has been installed${idsCL[Default]}"
	[ ${action-x} ] && exit 0 || ENTER2CONTINUE;;
  esac
}

INSTALL_ACRONIS() {
  read -n 1 -p "Are you sure you wish to install Acronis (Y/n)?" choice
  case "$choice" in
  [Nn]) MAIN_MENU;;
  * )
    echo
		cd /tmp
    wget "https://us5-cloud.acronis.com/bc/api/ams/links/agents/redirect?language=multi&channel=CURRENT&system=linux&architecture=64&productType=enterprise&login=010180ae-63c4-4495-bed0-4ec934c25af9&white_labeled=0" -O ./acronisinstall
    chmod +x ./acronisinstall
    ./acronisinstall
    rm -f ./acronisinstall
    echo
		echo -e "\n${idsCL[Green]}Acronis has been installed${idsCL[Default]}"
		[ ${action-x} ] && exit 0 || ENTER2CONTINUE
  esac
}


INSTALL_PROXMENUX() {
#	read -n 1 -p "Are you sure you wish to install ProxMenux (Y/n)?" choice
#	case "$choice" in 
#	[Nn]) MAIN_MENU;;
#	* )
#		echo
		bash -c "$(wget -qLO - https://raw.githubusercontent.com/MacRimi/ProxMenux/main/install_proxmenux.sh)"
		systemctl disable --now proxmenux-monitor
#		echo -e "\n${idsCL[Green]}ProxMenux has been installed${idsCL[Default]}"
#		[ ${action-x} ] && exit 0 || ENTER2CONTINUE
#	esac
}

PROXMENUX_POST_INSTALL() {
	PMFLDR='/usr/local/share/proxmenux/scripts/post_install'
	echo 1
	[ ! -f ${PMFLDR}/customizable_post_install.sh ] && INSTALL_PROXMENUX
	echo 2
	bash ${PMFLDR}/customizable_post_install.sh
	
	[ ${action-x} ] && exit 0 || ENTER2CONTINUE
	touch /opt/.PROXMENUX_POST_INSTALL
}

INSTALL_GLANCES() {
	read -n 1 -p "Are you sure you wish to install Glances (Y/n)?" choice
	case "$choice" in 
	[Nn]) MAIN_MENU;;
	* )
		echo
		apt install glances -y
		echo -e "\n${idsCL[Green]}Glances has been installed${idsCL[Default]}"
		[ ${action-x} ] && exit 0 || ENTER2CONTINUE
	esac
}

INSTALL_SCREENCONNECT() {
	read -n 1 -p "Are you sure you wish to install ScreenConnect (Y/n)?" choice
	case "$choice" in 
	[Nn]) MAIN_MENU;;
	* )
		echo
    
		echo -en "\n${idsCL[LightYellow]}Paste the URL provided from the Build Installer: ${idsCL[Default]}"
		read -e SCURL    
    wget "${SCURL}" -O /tmp/scinstall
    dpkg -i /tmp/scinstall
    apt install --fix-broken -y
    apt remove "connectwis*" -y
    dpkg -i /tmp/scinstall
    rm -f /tmp/scinstall
    systemctl disable --now proxmenux-monitor
		echo -e "\n${idsCL[Green]}ScreenConnect has been installed${idsCL[Default]}"
		[ ${action-x} ] && exit 0 || ENTER2CONTINUE
	esac
}

INSTALL_RMM() {
	read -n 1 -p "Are you sure you wish to install RMM (Y/n)?" choice
	case "$choice" in 
	[Nn]) MAIN_MENU;;
	* )
		echo
    
		echo -en "\n${idsCL[LightYellow]}Paste the Linux Server URL provided from the Download Agent screen: ${idsCL[Default]}"
		read -e RMMURL
		wget "${RMMURL}" -O /tmp/rmminstall
		TOKEN="$(echo ${RMMURL} | awk -F 'TKN' '{print $2}' | awk -F '/RUN' '{print $1}')"
		CMD="TOKEN=${TOKEN} bash /tmp/rmminstall"
		eval ${CMD}
		systemctl restart ITSPlatform
		# rm -f /tmp/rmminstall
    
		echo -e "\n${idsCL[Green]}RMM has been installed${idsCL[Default]}"
		[ ${action-x} ] && exit 0 || ENTER2CONTINUE
	esac
}

INSTALL_S1() {
	read -n 1 -p "Are you sure you wish to install the SentinelOne Agent (Y/n)?" choice
	case "$choice" in 
	[Nn]) MAIN_MENU;;
	* )
		echo
		
		echo -en "${idsCL[LightYellow]}Paste the customers SentinelOne Site Token: ${idsCL[Default]}"
		read -e s1token
		cd /tmp
		wget "https://git.schroedercity.com/TAI/TA-ProxMenu/raw/branch/main/SentinelAgent_linux_x86_64_v25_3_2_11.deb"
		dpkg -i ./SentinelAgent_linux_x86_64*.deb
		/opt/sentinelone/bin/sentinelctl management token set ${s1token}
		/opt/sentinelone/bin/sentinelctl control start
		rm -f ./SentinelAgent_linux_x86_64*.deb
		
		echo -e "\n${idsCL[Green]}SentinelOne Agent has been installed. Make sure its added to a \"DETECT ONLY\" policy${idsCL[Default]}"
		[ ${action-x} ] && exit 0 || ENTER2CONTINUE
	esac
}

INSTALL_OMSA() {
	read -n 1 -p "Are you sure you wish to install Dell OpenManage Administrator (Y/n)?" choice
	case "$choice" in 
	[Nn]) MAIN_MENU;;
	* )
		echo

    mkdir /tmp/omsa
    cd /tmp/omsa
    apt install gnupg libcurl4t64 libncurses6 libxslt1.1 libgpm2 libtinfo6
    mkdir -p /etc/apt/keyrings
    wget -qO - https://linux.dell.com/repo/pgp_pubkeys/0x1285491434D8786F.asc | gpg --dearmor -o /etc/apt/keyrings/linux.dell.com.gpg
    chmod +r /etc/apt/keyrings/linux.dell.com.gpg
    echo "deb [signed-by=/etc/apt/keyrings/linux.dell.com.gpg] http://linux.dell.com/repo/community/openmanage/11000/jammy jammy main" > /etc/apt/sources.list.d/linux.dell.com.list
    apt update
    wget -c http://archive.ubuntu.com/ubuntu/pool/universe/o/openwsman/libwsman-curl-client-transport1_2.6.5-0ubuntu16_amd64.deb
    wget -c http://archive.ubuntu.com/ubuntu/pool/universe/o/openwsman/libwsman-client4t64_2.6.5-0ubuntu16_amd64.deb
    wget -c http://archive.ubuntu.com/ubuntu/pool/universe/o/openwsman/libwsman1t64_2.6.5-0ubuntu16_amd64.deb
    wget -c http://http.us.debian.org/debian/pool/main/libx/libxml2/libxml2-16_2.15.1+dfsg-2+b1_amd64.deb
    wget -c http://archive.ubuntu.com/ubuntu/pool/universe/o/openwsman/libwsman-server1t64_2.6.5-0ubuntu16_amd64.deb
    wget -c http://archive.ubuntu.com/ubuntu/pool/universe/s/sblim-sfcc/libcimcclient0_2.2.8-0ubuntu2_amd64.deb
    wget -c http://archive.ubuntu.com/ubuntu/pool/universe/o/openwsman/openwsman_2.6.5-0ubuntu16_amd64.deb
    wget -c http://archive.ubuntu.com/ubuntu/pool/multiverse/c/cim-schema/cim-schema_2.48.0-0ubuntu1_all.deb
    wget -c http://archive.ubuntu.com/ubuntu/pool/universe/s/sblim-sfc-common/libsfcutil0_1.0.1-0ubuntu4_amd64.deb
    wget -c http://archive.ubuntu.com/ubuntu/pool/multiverse/s/sblim-sfcb/sfcb_1.4.9-0ubuntu7_amd64.deb
    wget -c http://archive.ubuntu.com/ubuntu/pool/universe/s/sblim-cmpi-devel/libcmpicppimpl0_2.0.3-0ubuntu2_amd64.deb
    wget -c http://ftp.us.debian.org/debian/pool/main/o/openssl/libssl1.1_1.1.1w-0+deb11u1_amd64.deb
    dpkg -i libwsman-curl-client-transport1_2.6.5-0ubuntu16_amd64.deb
    dpkg -i libwsman-client4t64_2.6.5-0ubuntu16_amd64.deb
    dpkg -i libxml2-16_2.15.1+dfsg-2+b1_amd64.deb
    dpkg -i libwsman1t64_2.6.5-0ubuntu16_amd64.deb
    dpkg -i libwsman-server1t64_2.6.5-0ubuntu16_amd64.deb
    dpkg -i libcimcclient0_2.2.8-0ubuntu2_amd64.deb
    dpkg -i openwsman_2.6.5-0ubuntu16_amd64.deb
    dpkg -i cim-schema_2.48.0-0ubuntu1_all.deb
    dpkg -i libsfcutil0_1.0.1-0ubuntu4_amd64.deb
    dpkg -i sfcb_1.4.9-0ubuntu7_amd64.deb
    dpkg -i libcmpicppimpl0_2.0.3-0ubuntu2_amd64.deb
    dpkg -i libssl1.1_1.1.1w-0+deb11u1_amd64.deb
    apt install srvadmin-all
    /opt/dell/srvadmin/sbin/srvadmin-services.sh start
    rm -Rf /tmp/omsa
    
		echo -e "\n${idsCL[Green]}Dell OMSA has been installed${idsCL[Default]}"
		echo -e "\n${idsCL[LightCyan]}Available at: ${idsCL[LightGreen]}https://${RNIP}:1311${idsCL[Default]}"
		[ ${action-x} ] && exit 0 || ENTER2CONTINUE
	esac
}

DOWNLOAD_VIRTIO() {
	echo -e "\n${idsCL[LightCyan]}Current \"Stable\" version available for download: ${idsCL[White]}${VIRTIO_FILE}${idsCL[Default]}"
  if [ -f /var/lib/vz/template/iso/${VIRTIO_FILE} ]; then
  	echo -en "\n${idsCL[LightRed]}Removing existing download ... "
    rm -f /var/lib/vz/template/iso/${VIRTIO_FILE}
    echo -e "${idsCL[Red]}Done${idsCL[Default]}"
  fi
  echo -en "\n${idsCL[LightCyan]}Downloading '${idsCL[White]}${VIRTIO_FILE}${idsCL[LightCyan]}' ... "
  wget -q -F -P /var/lib/vz/template/iso ${VIRTIO_DOWNLOAD_URL}
  echo -e "${idsCL[LightGreen]}Done${idsCL[Default]}"
  
	echo -e "\n${idsCL[Green]}VirtIO drivers for Windows have been downloaded to the local storage${idsCL[Default]}"
	[ ${action-x} ] && exit 0 || ENTER2CONTINUE
}

DETECT_CPU(){
  echo
  if ! command -v proxclmc &> /dev/null || [ ! -f /etc/apt/sources.list.d/gyptazy.list ]; then
    curl https://git.gyptazy.com/api/packages/gyptazy/debian/repository.key -o /etc/apt/keyrings/gyptazy.asc
    echo "deb [signed-by=/etc/apt/keyrings/gyptazy.asc] https://packages.gyptazy.com/api/packages/gyptazy/debian trixie main" | sudo tee -a /etc/apt/sources.list.d/gyptazy.list
    apt update && apt -y install proxclmc
    echo
  fi
  proxclmc
  echo
  
	echo -en "${idsCL[LightCyan]}Would you like to set '${idsCL[LightGreen]}cpu: $(proxclmc --list-only)${idsCL[LightCyan]}' on all VMs (y/N)?${idsCL[Default]} "
	read -n 1 choice
	case "$choice" in 
  	[Yy])
      sed -i "/cpu:/c cpu: $(proxclmc --list-only)" /etc/pve/nodes/*/qemu-server/*.conf
      echo
  		echo -e "\n${idsCL[Green]}All VM's have been reconfigured\n${idsCL[LightCyan]}This will require the VM's to be powered off and then turned back on in order to take effect${idsCL[Default]}"
    ;;
    *) echo;;
  esac
    
	[ ${action-x} ] && exit 0 || ENTER2CONTINUE
}

MAIN_MENU() {
	while :
	do
		clear
		echo
		echo -e " ${idsCL[Green]}TA-Proxmenu - Proxmox Scripts${idsCL[Default]}                              ${idsCL[Default]}(ver-${VERS})"
		echo -e "${idsCL[Green]}---------------------------------------------------------------------------${idsCL[Default]}"
		echo -e "Hostname:    ${idsCL[Cyan]}$(hostname -s)${idsCL[Default]}"
		echo -e "IP Address:  ${idsCL[Cyan]}${RNIP}${idsCL[Default]}"
		echo -e "---------------------------------------------------------------------------"
		echo
    
		echo -en " [${idsCL[LightYellow]}1${idsCL[Default]}] ${idsCL[White]}Run Post-Install Script${idsCL[Default]}"
		[ -f /opt/.PROXMENUX_POST_INSTALL ] && echo -e "${idsCL[Cyan]} - Has been ran prevously${idsCL[Default]}" || echo
		if ! pvesh get /cluster/resources | grep -i pulse &> /dev/nul ; then
			echo -e " [${idsCL[LightYellow]}2${idsCL[Default]}] ${idsCL[White]}Install Pulse Monitoring${idsCL[Default]}"
		else
			echo -e "${idsCL[DarkGray]} [2] Pulse Monitoring is already installed${idsCL[Default]}"
		fi
    
		echo
		if [ "$(systemctl is-active ITSPlatform)" != "active" ]; then
			echo -e " [${idsCL[LightYellow]}3${idsCL[Default]}] ${idsCL[White]}Install ConnectWise RMM Agent${idsCL[Default]}"
			echo -e " [${idsCL[LightYellow]}4${idsCL[Default]}] ${idsCL[White]}Install ScreenConnect Agent${idsCL[Default]}"
		else
			echo -e "${idsCL[DarkGray]} [3] ConnectWise RMM Agent is already installed${idsCL[Default]}"
		fi
		if [ "$(dpkg -l | awk '/cyberprotect/ {print }'|wc -l)" -eq 0 ]; then
			echo -e " [${idsCL[LightYellow]}5${idsCL[Default]}] ${idsCL[White]}Install Acronis Backup Agent${idsCL[Default]}"
		else
			echo -e "${idsCL[DarkGray]} [5] Acronis Backup is already installed${idsCL[Default]}"
		fi
		if [ "$(dpkg -l | awk '/sentinelagent/ {print }'|wc -l)" -eq 0 ]; then
			echo -e " [${idsCL[LightYellow]}6${idsCL[Default]}] ${idsCL[White]}Install SentinelOne Agent${idsCL[Default]}"
		else
			echo -e "${idsCL[DarkGray]} [6] SentinelOne is already installed${idsCL[Default]}"
		fi
    
		echo
		if ! command -v glances &> /dev/null; then
			echo -e " [${idsCL[LightYellow]}7${idsCL[Default]}] ${idsCL[White]}Install Glances (CLI Monitor)${idsCL[Default]}"
		else
			echo -e "${idsCL[DarkGray]} [7] Glances is already installed${idsCL[Default]}"
		fi
		if [ "$(dpkg -l | awk '/srvadmin-all/ {print }'|wc -l)" -eq 0 ]; then
			echo -e " [${idsCL[LightYellow]}8${idsCL[Default]}] ${idsCL[White]}Install Dell OpenManage Server Administrator${idsCL[Default]}"
		else
			echo -e "${idsCL[DarkGray]} [8] Dell OMSA is already installed - ${idsCL[Cyan]}https://${RNIP}:1311"
		fi
    
		echo
		if [ -f /var/lib/vz/template/iso/${VIRTIO_FILE} ]; then
			echo -e "${idsCL[DarkGray]} [9] Current VirtIO drivers already downloaded to 'local' on this host${idsCL[Default]}"
		elif [ -f /var/lib/vz/template/iso/virtio*.iso ]; then
			echo -e " [${idsCL[LightYellow]}9${idsCL[Default]}] ${idsCL[LightGreen]}**${idsCL[White]}Download the available updated Win-VirtIO drivers to 'local' on this host${idsCL[Default]}"
		else
			echo -e " [${idsCL[LightYellow]}9${idsCL[Default]}] ${idsCL[White]}Download the current Win-VirtIO drivers to 'local' on this host${idsCL[Default]}"
		fi
    
    echo
		echo -e " [${idsCL[LightYellow]}0${idsCL[Default]}] ${idsCL[White]}Detect CPU-Arch for Live Migrations${idsCL[Default]}"
		echo
		echo
		echo -e " [${idsCL[LightYellow]}Q${idsCL[Default]}] ${idsCL[White]}Quit${idsCL[Default]}"
		echo
		echo
		echo -e -n "${idsCL[Yellow]}Enter ${idsCL[LightYellow]}option${idsCL[Yellow]} from above:${idsCL[Default]} "
		read -n 1 opt
		echo
		case $opt in
      # [1]) [ ! -f /usr/local/bin/menu ] && INSTALL_PROXMENUX || /usr/local/bin/menu;;
		  [1]) PROXMENUX_POST_INSTALL;;
		  [2]) INSTALL_PULSE;;
		  [3]) INSTALL_RMM;;
		  [4]) INSTALL_SCREENCONNECT;;
		  [5]) INSTALL_ACRONIS;;
		  [6]) INSTALL_S1;;
		  [7]) INSTALL_GLANCES;;
		  [8]) INSTALL_OMSA;;
		  [9]) DOWNLOAD_VIRTIO;;
		  [0]) DETECT_CPU;;
		  [Qq]) EXIT1
				exit 0;;
			*)	echo "Thats an invaild option,";
				echo "please select a valid option only.";
				sleep 1;;
		esac
	done
}


if [ ${action-x} ]; then
	case $action in
		pulse) INSTALL_PULSE;;
		rmm) INSTALL_RMM;;
		omsa) INSTALL_OMSA;;
		glances) INSTALL_GLANCES;;
		acronis) INSTALL_ACRONIS;;
		proxmenux) [ ! -f /usr/local/bin/menu ] && INSTALL_PROXMENUX || /usr/local/bin/menu;;
		screenconnect) INSTALL_SCREENCONNECT;;
		cpu) DETECT_CPU;;
    *) MAIN_MENU;;
	esac
else
	MAIN_MENU
fi

exit 0





