#!/bin/bash
#descrição: Script de instalação do TEF Nuvem versão 2.1.3
#Atualizado por: CRISTIANO RODRIGUES SIQUEIRA SOARES

erro(){
	clear
	echo "╔═════════════════════════════════════════════════╗"
	echo "║ Erro ao executar a instalação das dependências. ║"
	echo "║                 Tente novamente!                ║"
	echo "╚═════════════════════════════════════════════════╝"
	exit 1
}
reperro(){
	clear
	echo "╔════════════════════════════════════════════════════════╗"
	echo "║ Erro no repositório. Verifique se status está 'enable' ║"
	echo "╚════════════════════════════════════════════════════════╝"
	exit 1
}

aptreperro(){
	clear
	echo "╔════════════════════════════════════════════╗"
	echo "║ Repositório apt com erro. Favor verificar! ║"
	echo "╚════════════════════════════════════════════╝"
	exit 1
}

downloadpack(){
	echo "╔════════════════════════════════╗"
	echo "║ Efetuando download dos pacotes ║"
	echo "╚════════════════════════════════╝"
	sleep 2
}

installpacks(){
	echo "╔════════════════════╗"
	echo "║ Instalando Pacotes ║"
	echo "╚════════════════════╝"
	sleep 2
}

archnotsupported(){
	clear
	echo "╔═══════════════════════════╗"
	echo "║ Sem suporte para o 'Arch' ║"
	echo "╚═══════════════════════════╝"
	exit 0
}

#Verifica se é usuário Root
user=`whoami`

#Define as Dependências para fazer o update

pkgrpmarray=("bash" "mawk" "procps" "sed" "grep" "coreutils" "net-tools" "wget" "perl" "crontabs" "chkconfig" )
pkgdebarray=("bash" "mawk" "procps" "sed" "grep" "coreutils" "net-tools" "wget" "cron" )
pkgtgzarray=("bash" "mawk" "procps" "sed" "grep" "coreutils" "net-tools" "wget" "dcron" )

#Define as Dependências para realizar a instalação

instarrayrpm=("bash" "mawk" "procps" "sed" "grep" "coreutils" "net-tools" "wget" "perl" "crontabs" "chkconfig" )
instarraydeb=("bash" "mawk" "procps" "sed" "grep" "coreutils" "net-tools" "wget" "cron" )
instarraytgz=("bash" "mawk" "procps" "sed" "grep" "coreutils" "net-tools" "wget" "dcron" )

#Encontra o 'arch' e o 'system'
cont=0
larch=`arch`
s1=`type -p dpkg | wc -l` 2>/dev/null
s2=`type -p installpkg | wc -l` 2>/dev/null
s3=`type -p rpm | wc -l` 2>/dev/null


define_platform()
{
if [ -d /etc/apt/ ]; then

sysid=deb
rel=`cat /etc/os-release | grep -w "VERSION_ID" | awk -F"=" '{ print $2 }' | sed -e 's/"//g'` 2>/dev/null
did=`cat /etc/os-release | grep -w "ID" | awk -F"=" '{ print $2 }' | sed -e 's/"//g'` 2>/dev/null
didlike=`cat /etc/os-release | grep -w "ID_LIKE" | awk -F"=" '{ print $2 }' | sed -e 's/"//g'` 2>/dev/null
relint=${rel%.*}

	case "$did" in
		debian)
				if [  $relint -ge 7 ]; then
					cont=1
				fi
		;;
		ubuntu)
				if [  $relint -ge 14 ]; then
					cont=2
				fi
		;;
		raspbian)
				if [  $relint -ge 7 ]; then
					cont=3
				fi
		;;
		linuxmint)
				if [  $relint -ge 10 ]; then
					cont=7
				fi
		;;
		*)
			    if [ `echo $didlike | grep -c 'debian\|ubuntu'` -gt 0  ]; then
				cont=9
				fi
		;;
	esac
		
	case "$larch" in
		x86_64)
			parch=amd64
		;;
		i686)
			parch=i386
		;;
		armv7*)
			parch=armhf
		;;
		i386)
			parch=$larch
		;;
	esac
	
		sysd=`ls -la /sbin/init | grep -c systemd`
		if [ $sysd -eq 1 ]; then
			svcdown1="systemctl stop wnbmonitor"
			svcdup1="systemctl start wnbmonitor"
			svcdrel1="systemctl restart wnbmonitor"
			svcdown2="systemctl stop wnbtlscli"
			svcdup2="systemctl start wnbtlscli"
			svcdrel2="systemctl restart wnbtlscli"
		else
			svcdown1="service wnbmonitor stop"
			svcdup1="service wnbmonitor start"
			svcdrel1="service wnbmonitor restart"
			svcdown2="service wnbtlscli stop"
			svcdup2="service wnbtlscli start"
			svcdrel2="service wnbtlscli restart"
		fi

elif [ -d /etc/yum.repos.d/ ]; then

sysid=rpm
rel=`cat /etc/os-release | grep -w "VERSION_ID" | awk -F"=" '{ print $2 }' | sed -e 's/"//g' ` 2>/dev/null
did=`cat /etc/os-release | grep -w "ID" | awk -F"=" '{ print $2 }' | sed -e 's/"//g'` 2>/dev/null
relint=${rel%.*}

	if [ $did = "centos" -a $relint -ge 7 ]; then
		cont=4
	fi
	if [ $did = "fedora" -a $relint -gt 16 ]; then
		cont=5
	fi
	case "$larch" in
		i686)
			parch=i386
		;;
		armv7*)	
			archnotsupported
		;;
		x86_64)
			parch=$larch
		;;
		i386)
			parch=$larch
		;;
	esac
		sysd=`ls -la /sbin/init | grep -c systemd`
		if [ $sysd -eq 1 ]; then
			svcdown1="systemctl stop wnbmonitor"
			svcdup1="systemctl start wnbmonitor"
			svcdrel1="systemctl restart wnbmonitor"
			svcdown2="systemctl stop wnbtlscli"
			svcdup2="systemctl start wnbtlscli"
			svcdrel2="systemctl restart wnbtlscli"
		else
			svcdown1="service wnbmonitor stop"
			svcdup1="service wnbmonitor start"
			svcdrel1="service wnbmonitor restart"
			svcdown2="service wnbtlscli stop"
			svcdup2="service wnbtlscli start"
			svcdrel2="service wnbtlscli restart"
		fi

else

sysid=tgz	
rel=`cat /etc/os-release | grep -w "VERSION_ID" | awk -F"=" '{ print $2 }' | sed -e 's/"//g' ` 2>/dev/null
did=`cat /etc/os-release | grep -w "ID" | awk -F"=" '{ print $2 }' | sed -e 's/"//g'` 2>/dev/null
relint=${rel%.*}

	if [ $did = "slackware" -a $relint -ge 14 ]; then
		cont=6
	fi
	case "$larch" in
		x86_64)	
			archnotsupported
		;;
		i686)
			parch=i386
		;;
		arm*)	
			archnotsupported
		;;
		i386)
			parch=$larch
		;;
	esac
		sysd=`ls -la /sbin/init | grep -c systemd`
		if [ $sysd -eq 1 ]; then
			svcdown1="systemctl stop wnbmonitor"
			svcdup1="systemctl start wnbmonitor"
			svcdrel1="systemctl restart wnbmonitor"
			svcdown2="systemctl stop wnbtlscli"
			svcdup2="systemctl start wnbtlscli"
			svcdrel2="systemctl restart wnbtlscli"
		else
			svcdown1="/etc/rc.d/rc.wnbmonitor stop"
			svcdup1="/etc/rc.d/rc.wnbmonitor start"
			svcdrel1="/etc/rc.d/rc.wnbmonitor restart"
			svcdown2="/etc/rc.d/rc.wnbtlscli stop"
			svcdup2="/etc/rc.d/rc.wnbtlscli start"
			svcdrel2="/etc/rc.d/rc.wnbtlscli restart"
		fi
fi
}

# check basic dependencies
check_depend()
{
	echo "╔══════════════════════════╗"
	echo "║ Verificando Dependencias ║"
	echo "╚══════════════════════════╝"
	case $sysid in
		deb)
			pkginst=() 
			dep=0
			for p in ${instarraydeb[*]}; do
			s=${#p}
				pok=`dpkg --get-selections | grep -w install | grep -w $p | awk '{ print length($0)==$s}' | wc -l`
				if [ $pok -eq 0 ]; then
					pkginst+=( $p )
				fi
			done
			if [ ${#pkginst[@]} -gt 0 ]; then
				dep=1
				echo "╔═════════════════════════╗"
				echo "║ Instalando dependencias ║ "
				echo "╚═════════════════════════╝"
				echo  "${pkginst[@]}"
				echo " "
				echo  "Aguarde ..."
				apt-get upgrade
				if [ $? -eq 0 ]; then
					apt-get -f -y install --show-progress "${pkginst[@]}" 
					if [ $? -eq 0 ]; then
						dep=0
					else
						apt --fix-broken install
						erro
					fi
				else
					aptreperro
				fi
			fi
		;;
		rpm)
			pkginst=()
			dep=0
			for p in ${instarrayrpm[*]}; do
				s=${#p}
				pok=`yum list installed | grep -w $p | awk '{ print length($0)==$s}' | wc -l`
				if [ $pok -eq 0 ]; then
					pkginst+=( $p )
				fi
			done
			if [ ${#pkginst[@]} -gt 0 ]; then
				dep=1
				echo "╔═════════════════════════╗"
				echo "║ Instalando dependencias ║ "
				echo "╚═════════════════════════╝"
				echo  "${pkginst[@]}" 
				echo  "Aguarde ..."
				yum upgrade
				if [ $? -eq 0 ]; then
					yum -y install "${pkginst[@]}"
					if [ $? -eq 0 ]; then
						dep=0
					else
						erro
					fi
				else
					aptreperro
				fi
			fi
		;;
		tgz)
		en=`slackpkg update | grep -c uncomment`
		if [ $en -eq 0 ]; then
			pkginst=() 
			dep=0
			for p in ${pkgtgzarray[*]}; do
				echo .
				pok=`slackpkg search $p | grep -w $p | grep -wc installed`
				if [ $pok -eq 0 ]; then
					pkginst+=( $p )
				fi
			done
			if [ ${#pkginst[@]} -gt 0 ]; then
				dep=1
				echo "╔═════════════════════════╗"
				echo "║ Instalando dependencias ║ "
				echo "╚═════════════════════════╝"
				echo  "${pkginst[@]}" 
				echo  "Aguarde ..."
				slackpkg install "${pkginst[@]}" 
					if [ $? -eq 0 ]; then
						dep=0
					else
						erro
					fi
			fi
		else
			reperro
		fi
		;;
	esac
}


#MAIN CODE #
echo > /tmp/wnbinstall.log

if [[ $user == "root" ]]; then

	case $1 in
		-i)
			if [ -d /etc/wnbtlscli/ ]; then
				echo "╔══════════════════════════════════╗"
				echo "║ Software wnbtlscli ja instalado. ║"
				echo "╚══════════════════════════════════╝"
			else
			define_platform
				if [ $cont -gt 0 ] && [ $cont -lt 10 ]; then
					echo "╔══════════════════════════════════════════════════════════════════════════════╗"
					echo "║ Sistema operacional $did ,versao $relint e arquitetura de processador $parch     ║"
					echo "║                                                                              ║"
					echo "║                   Validando e instalando as dependencias                     ║"
					echo "╚══════════════════════════════════════════════════════════════════════════════╝"
					check_depend

					if [ $dep -eq 0 ]; then
						echo "╔═════════════════════════════════════╗"
						echo "║ Dependencias instaladas com sucesso ║"
						echo "╚═════════════════════════════════════╝"

						case "$sysid" in
							deb)
								downloadpack
								if [ -d /tmp/wnbtlscli/ ]; then
									echo 1 >/dev/null
								else
									mkdir /tmp/wnbtlscli/
								fi
								wget -P /tmp/wnbtlscli/ -q --no-check-certificate --progress=dot  https://portal.comnect.com.br/downloads/tls/linux/wnbtlscli_$parch.$sysid
								sleep 3

								installpacks
								dpkg -i /tmp/wnbtlscli/wnbtlscli_$parch.$sysid
							;;
							rpm)
								downloadpack
								if [ -d /tmp/wnbtlscli/ ]; then
									echo 1 >/dev/null
								else
									mkdir /tmp/wnbtlscli/
								fi
								wget -P /tmp/wnbtlscli/ -q --no-check-certificate --progress=dot  https://portal.comnect.com.br/downloads/tls/linux/wnbtlscli_$parch.$sysid
								sleep 3

								installpacks
								rpm -ih /tmp/wnbtlscli/wnbtlscli_$parch.$sysid
							;;
							tgz)
								downloadpack
								if [ -d /tmp/wnbtlscli/ ]; then
									echo 1 >/dev/null
								else
									mkdir /tmp/wnbtlscli/
								fi
								wget -P /tmp/wnbtlscli/ -q --no-check-certificate --progress=dot  https://portal.comnect.com.br/downloads/tls/linux/wnbtlscli_$parch.$sysid
								sleep 3

								installpacks
								installpkg /tmp/wnbtlscli/wnbtlscli_$parch.$sysid
							;;
						esac
					else
						echo "╔═══════════════════════════╗"
						echo "║ Dependencias com problema ║"
						echo "╚═══════════════════════════╝"
					fi
				else
					echo "╔════════════════════════════════════════════════════════════════════════════════════════════╗"
					echo "║ Sistema operacional nao suportado $did ,versao $relint e arquitetura de processador $parch     ║"
					echo "╚════════════════════════════════════════════════════════════════════════════════════════════╝"
				fi
			fi
				if [ -d /tmp/wnbtlscli/ ]; then
					rm -rf /tmp/wnbtlscli/
				fi
		;;
		-v)
			echo "wnbinstall-version: 2.1.3"
		;;
		*)
			echo "  wnbinstall options "
			echo " -i to install on-line software"
			echo " -v to version "
			echo " -h to help"
		;;
	esac

else
	clear
	echo " "
    echo "╔══════════════════════════════════════════════╗"
    echo "║ Execute este aplicatvo como Root, por favor. ║"
    echo "╚══════════════════════════════════════════════╝"
    exit
fi



