#!/bin/bash
##SIG FRENTE 3.1 NFCE####################################################################Ubuntu18###
##By: RAFAEL RIBEIRO################################################################################
##Revision: RAFAEL BORGES E CAIO SANTOS#############################################################
##Atualizado: CRISTIANO RODRIGUES SIQUEIRA SOARES###################################################

user=`whoami`

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

	# Versão atualizada dos pacotes
	mkdir /tmp/install_sigfrente/
	chmod 777 /tmp/install_sigfrente/
	cd /tmp/install_sigfrente/
	touch install_log_sigfrente.log
	rm -rf update
	apt-get update -y

	log='/tmp/install_sigfrente/install_log_sigfrente.log'

	# Variáveis
	#sigfrente=`cat /tmp/install_sigfrente/update | grep -i sigfrente | cut -d "=" -f2`
	#sigutils=`cat /tmp/install_sigfrente/update | grep -i sigutils | cut -d "=" -f2`
	#pythonnfe=`cat /tmp/install_sigfrente/update | grep -i nfe | cut -d "=" -f2`
	
	sigfrente='sigpdvnfce-3.1.21L3.deb'	
	sigutils='sigutils-2.4.6.tar.gz'
	pythonnfe='nfe-4.51.13.tar.gz'
	geraldo='geraldo_0.4.10-stable-sig.tar.gz'
	kiwi='python-kiwi_1.9.27-nmu1_all.deb'
	stoqdrivers='stoqdrivers_0.9.11.9i18_all.deb'
	pyxmlsec='pyxmlsec_0.3.1.orig.tar.gz'
	pyxmlsecdeb='python-pyxmlsec_0.3.1-1openhouse2_amd64.deb'
	xmlsec='xmlsec-0.3.1.tar.gz'
	pysqlalchemy='python-sqlalchemy_0.8.4-nmu1build1_all.deb'
	pysqlalchemydoc='python-sqlalchemy-doc_0.8.4-nmu1build1_all.deb'
	pyordereddict='python-ordereddict_1.1-2chl1~trusty1_all.deb'
	pyserial='python-serial_2.6-nmu1build1_all.deb'
	errorhandler='python-errorhandler_1.1.1-1_all.deb'
	xlutils='python-xlutils_1.5.1-1_all.deb'
	elixir='python-elixir_0.7.1-2_all.deb'
	vpostgresql='13'

	# Baixando pacotes
	#wget -c --progress=bar:force download.sigsolucoes.net.br/produtos/sigpdv/3.1/$sigfrente/sigpdvnfce-3.1.$sigfrente.deb >> $log
	#wget -c --progress=bar:force download.sigsolucoes.net.br/produtos/sigutils/${sigutils%\.*}/sigutils-$sigutils.tar.gz >> $log
	#wget -c --progress=bar:force download.sigsolucoes.net.br/produtos/python-nfe/${pythonnfe%\.*}/nfe-$pythonnfe.tar.gz >> $log
	#wget -c --progress=bar:force download.sigsolucoes.net.br/produtos/_deps/geraldo/0.4.10/$geraldo >> $log
	#wget -c --progress=bar:force download.sigsolucoes.net.br/apt/pool/bionic/main/amd64/k/kiwi/$kiwi >> $log
	#wget -c --progress=bar:force download.sigsolucoes.net.br/apt/pool/bionic/main/amd64/s/stoqdrivers/$stoqdrivers >> $log
	#wget -c --progress=bar:force download.sigsolucoes.net.br/apt/pool/bionic/main/amd64/p/pyxmlsec/$pyxmlsec >> $log
	#wget -c --progress=bar:force download.sigsolucoes.net.br/apt/pool/bionic/main/amd64/p/pyxmlsec/$pyxmlsecdeb >> $log
	#wget -c --progress=bar:force download.sigsolucoes.net.br/apt/pool/bionic/main/amd64/p/pyxmlsec/$xmlsec >> $log
	#wget -c --progress=bar:force download.sigsolucoes.net.br/apt/pool/bionic/main/amd64/p/python-errorhandler/$errorhandler >> $log
	#wget -c --progress=bar:force download.sigsolucoes.net.br/apt/pool/bionic/main/amd64/p/python-xlutils/$xlutils >> $log
	#wget -c --progress=bar:force download.sigsolucoes.net.br/apt/pool/bionic/main/amd64/e/elixir/$elixir >> $log
	#wget -c --progress=bar:force download.sigsolucoes.net.br/apt/pool/bionic/main/amd64/p/pyserial/$pyserial >> $log
	#wget -c --progress=bar:force download.sigsolucoes.net.br/apt/pool/bionic/main/amd64/s/sqlalchemy/$pysqlalchemy >> $log
	#wget -c --progress=bar:force download.sigsolucoes.net.br/apt/pool/bionic/main/amd64/s/sqlalchemy/python-sqlalchemy-doc_0.8.4-nmu1build1_all.deb >> $log
	#wget -c --progress=bar:force download.sigsolucoes.net.br/apt/pool/bionic/main/amd64/p/python-ordereddict/python-ordereddict_1.1-2chl1~trusty1_all.deb >> $log

	#wget -c --progress=bar:force download.sigsolucoes.net.br/produtos/sigpdv/3.1/update >> $log
	#wget -c --progress=bar:force download.sigsolucoes.net.br/produtos/sigpdv/atualizapdv/atua >> $log
	wget -c --progress=bar:force download.sigsolucoes.net.br/produtos/crss/PDV/sig.jpg >> $log
	wget -c --progress=bar:force download.sigsolucoes.net.br/produtos/crss/PDV/menu-sig.png >> $log
	mv sig.jpg /usr/share/xfce4/backdrops/ && chmod 777 /usr/share/xfce4/backdrops/sig.jpg
	mv menu-sig.png /usr/share/pixmaps/ && chmod 777 /usr/share/pixmaps/menu-sig.png

	# Baixando pacotes
	wget -c --progress=bar:force download.sigsolucoes.net.br/produtos/crss/PDV/$sigfrente >> $log
	wget -c --progress=bar:force download.sigsolucoes.net.br/produtos/crss/PDV/$sigutils >> $log
	wget -c --progress=bar:force download.sigsolucoes.net.br/produtos/crss/PDV/$pythonnfe >> $log
	wget -c --progress=bar:force download.sigsolucoes.net.br/produtos/crss/PDV/$geraldo >> $log
	wget -c --progress=bar:force download.sigsolucoes.net.br/produtos/crss/PDV/$kiwi >> $log
	wget -c --progress=bar:force download.sigsolucoes.net.br/produtos/crss/PDV/$stoqdrivers >> $log
	#wget -c --progress=bar:force download.sigsolucoes.net.br/produtos/crss/PDV/$pyxmlsec >> $log
	wget -c --progress=bar:force download.sigsolucoes.net.br/produtos/crss/PDV/$pyxmlsecdeb >> $log
	wget -c --progress=bar:force download.sigsolucoes.net.br/produtos/crss/PDV/$xmlsec >> $log
	wget -c --progress=bar:force download.sigsolucoes.net.br/produtos/crss/PDV/$errorhandler >> $log
	wget -c --progress=bar:force download.sigsolucoes.net.br/produtos/crss/PDV/$xlutils >> $log
	wget -c --progress=bar:force download.sigsolucoes.net.br/produtos/crss/PDV/$elixir >> $log
	wget -c --progress=bar:force download.sigsolucoes.net.br/produtos/crss/PDV/$pyserial >> $log
	wget -c --progress=bar:force download.sigsolucoes.net.br/produtos/crss/PDV/$pysqlalchemy >> $log
	wget -c --progress=bar:force download.sigsolucoes.net.br/produtos/crss/PDV/$pysqlalchemydoc >> $log
	wget -c --progress=bar:force download.sigsolucoes.net.br/produtos/crss/PDV/$pyordereddict >> $log
	chmod -R 777 /tmp/install_sigfrente

	if  [ -e $sigfrente       ] && [ -e $kiwi          ] &&
		[ -e $sigutils        ] && [ -e $pythonnfe     ] &&
		[ -e $geraldo         ] && [ -e $stoqdrivers   ] &&
		#[ -e $pyxmlsec       ] && [ -e $xmlsec        ] &&
		[ -e $pyxmlsecdeb     ] && [ -e $errorhandler  ] &&
		[ -e $xlutils         ] && [ -e $elixir        ] &&
		[ -e $pyserial        ] && [ -e $pysqlalchemy  ] && 
		[ -e $pysqlalchemydoc ] && [ -e $pyordereddict ];then
		echo "╔════════════════════════════════╗"
		echo "║ Download efetuado com sucesso! ║"
		echo "╚════════════════════════════════╝"
		echo "Download efetuado com sucesso!" >> $log
	else
		echo "╔════════════════════════════════════════╗"
		echo "║ Não foi possível efetuar o download... ║"
		echo "╚════════════════════════════════════════╝"
		echo "Não foi possível efetuar o download..." >> $log
		echo "╔════════════════════════════════════════════════════╗"
		echo "║ Verifique se os links de downloads estão corretos. ║"
		echo "╚════════════════════════════════════════════════════╝"
		echo "Verifique se os links de downloads estão corretos." >> $log
		exit
	fi

	# Instalando Pacotes Necessários
	echo "╔═════════════════════════════╗"
	echo "║ Obtendo pacotes Necessários ║"
	echo "╚═════════════════════════════╝"
	echo 'Obtendo pacotes Necessários...' >> $log
	apt-get update -y
	apt-get -f install vim -y
	apt-get -f install openssh-server -y
	apt-get -f install ssh -y
	apt-get -f install terminator -y

	# Instalando dependencias dos pacotes SigPDV3.1-NFC-e
	echo "Obtendo pacotes para instalação SigPDV3.1-NFC-e..." >> $log
	echo -e "╔════════════════════════════════════════════════════╗"
	echo -e "║ Obtendo pacotes para instalação SigPDV3.1-NFC-e... ║"
	echo -e "╚════════════════════════════════════════════════════╝"
	apt-get update -y
	dpkg --add-architecture i386
	apt-get -f install xmlsec1 -y
	apt-get -f install python-libxml2 -y
	apt-get -f install python-libxmlsec -y
	apt-get -f install python-openssl -y
	apt-get -f install python-lxml -y
	apt-get -f install libxmlsec1-openssl -y
	apt-get -f install libxmlsec1-dev -y
	apt-get -f install libxslt1-dev -y
	apt-get -f install zlib1g-dev -y
	apt-get -f install libxml2-dev -y
	apt-get -f install libxml2:amd64 -y
	apt-get -f install libxml2:i386 -y
	apt-get -f install lib32stdc++6 -y
	apt-get -f install libusb-0.1-4 -y
	apt-get -f install libtool -y
	apt-get -f install libqtgui4 -y
	apt-get -f install libqtcore4 -y
	apt-get -f install libqt4-xml -y
	apt-get -f install libqt4-test -y
	apt-get -f install libqt4-script -y
	apt-get -f install libqt4-network -y
	apt-get -f install libqt4-dbus -y
	apt-get -f install libgle3 -y
	apt-get -f install libssl-dev -y
	apt-get -f install libffi-dev -y
	apt-get -f install python3-smbc -y
	apt-get -f install python-opengl -y
	apt-get -f install python-setuptools -y
	apt-get -f install pyside-tools -y
	apt-get -f install python-requests -y
	apt-get -f install python-docutils -y
	apt-get -f install python2.7-zope -y
	apt-get -f install python-reportlab -y
	apt-get -f install python-psycopg2 -y
	apt-get -f install python-dateutil -y
	apt-get -f install python-keybinder -y
	apt-get -f install python-pil -y
	apt-get -f install python-pyside -y
	apt-get -f install python-pyside.qtopengl -y
	apt-get -f install python-dev -y
	apt-get -f install swig -y
	apt-get -f install default-libmysqlclient-dev -y
	apt-get -f install libmysqlclient-dev -y
	apt-get -f install python-usb -y
	apt-get -f install python-rex -y
	apt-get -f install python-qrcode -y
	apt-get -f install python-dnspython -y
	apt-get -f install python-crypto -y
	apt-get -f install python-m2crypto -y
	apt-get -f install python-mysqldb -y
	apt-get -f install python-beautifulsoup -y
	apt-get -f install python-qt4 -y
	apt-get -f install python-qt4-gl -y
	apt-get -f install qt4-designer -y
	apt-get -f install qt4-dev-tools -y
	apt-get -f install build-essential -y
	apt-get -f install autoconf -y
	apt-get -f install pkg-config -y
	apt-get -f install idle-python2.7 -y
	apt-get -f install postgresql -y
	apt-get -f install pgadmin3 -y
	apt-get -f install python-pip -y
	apt-get -f install --reinstall cups-filters -y
	pip install segno
	pip install pytz
	pip install "cython<0.23"


	# Instalado Pacote Python-Kiwi

	echo -e "\n\nInstalando pacote Python-Kiwi" >> $log
	echo -e "╔═══════════════════════════════╗"
	echo -e "║ Instalando pacote Python-Kiwi ║"
	echo -e "╚═══════════════════════════════╝"
	cd /tmp/install_sigfrente/ >> $log
	dpkg -i $kiwi >> $log
	sleep 3

	# Instalando Pacote Geraldo-Stable

	echo -e "\n\nInstalando pacote Geraldo-Stable" >> $log
	echo -e "╔══════════════════════════════════╗"
	echo -e "║ Instalando pacote Geraldo-Stable ║"
	echo -e "╚══════════════════════════════════╝"
	cd /tmp/install_sigfrente/ >> $log
	tar xfvz $geraldo >> $log
	cd /tmp/install_sigfrente/Geraldo-0.4.10-stable-sig/ >> $log
	python setup.py install
	sleep 3

	# Instalado Pacote PyXmlSec

	#echo -e "\n\nInstalando pacote PyXmlSec" >> $log
	#echo -e "╔═══════════════════════════════════╗"
	#echo -e "║ Instalando pacote PyXmlSec.tar.gz ║"
	#echo -e "╚═══════════════════════════════════╝"
	#cd /tmp/install_sigfrente/ >> $log
	#tar xfvz $pyxmlsec >> $log
	#cd pyxmlsec-0.3.1 >> $log
	#python setup.py install >> $log
	#sleep 3

	# Instalado Pacote PyXmlSec.deb

	echo -e "\n\nInstalando pacote PyXmlSec.deb" >> $log
	echo -e "╔════════════════════════════════╗"
	echo -e "║ Instalando pacote PyXmlSec.deb ║"
	echo -e "╚════════════════════════════════╝"
	cd /tmp/install_sigfrente/ >> $log
	dpkg -i $pyxmlsecdeb >> $log
	sleep 3
	
	# Instalado Pacote XmlSec

	#echo -e "\n\nInstalando pacote PyXmlSec" >> $log
	#echo -e "╔══════════════════════════╗"
	#echo -e "║ Instalando pacote XmlSec ║"
	#echo -e "╚══════════════════════════╝"
	#cd /tmp/install_sigfrente/ >> $log
	#tar xfvz $xmlsec >> $log
	#cd xmlsec-0.3.1 >> $log
	#python setup.py install >> $log
	#sleep 3

	# Instalado Pacote Python-Serial

	echo -e "\n\nInstalando pacote Python-Serial" >> $log
	echo -e "╔═════════════════════════════════╗"
	echo -e "║ Instalando pacote Python-Serial ║"
	echo -e "╚═════════════════════════════════╝"
	cd /tmp/install_sigfrente/ >> $log
	dpkg -i $pyserial >> $log
	sleep 3

	# Instalado Pacote StoqDrivers

	echo -e "\n\nInstalando pacote StoqDrivers" >> $log
	echo -e "╔═══════════════════════════════╗"
	echo -e "║ Instalando pacote StoqDrivers ║"
	echo -e "╚═══════════════════════════════╝"
	cd /tmp/install_sigfrente/ >> $log
	dpkg -i $stoqdrivers >> $log
	sleep 3

	# Instalando Pacote ErrorHandler

	echo -e "\n\nInstalando pacote ErrorHandler" >> $log
	echo -e "╔════════════════════════════════╗"
	echo -e "║ Instalando pacote ErrorHandler ║"
	echo -e "╚════════════════════════════════╝"
	cd /tmp/install_sigfrente/ >> $log
	dpkg -i $errorhandler >> $log
	sleep 3

	# Instalado Pacote XlUtils

	echo -e "\n\nInstalando pacote XlUtils" >> $log
	echo -e "╔═══════════════════════════╗"
	echo -e "║ Instalando pacote XlUtils ║"
	echo -e "╚═══════════════════════════╝"
	cd /tmp/install_sigfrente/ >> $log
	dpkg -i $xlutils >> $log
	sleep 3

	# Instalado Pacote Python-Sqlalchemy

	echo -e "\n\nInstalando pacote Python-Sqlalchemy" >>$log
	echo -e "╔═════════════════════════════════════╗"
	echo -e "║ Instalando pacote Python-Sqlalchemy ║"
	echo -e "╚═════════════════════════════════════╝"
	cd /tmp/install_sigfrente/ >> $log
	dpkg -i $pysqlalchemy >> $log
	dpkg -i $pysqlalchemydoc >> $log
	sleep 3

	# Instalado Pacote Python-Elixir

	echo -e "\n\nInstalando pacote Python-Elixir" >> $log
	echo -e "╔════════════════════════════════╗"
	echo -e "║ Instalando pacote Python-Elixr ║"
	echo -e "╚════════════════════════════════╝"
	cd /tmp/install_sigfrente/ >> $log
	dpkg -i $elixir >> $log
	sleep 3

	# Instalado Pacote Python-Ordereddict

	echo -e "\n\nInstalando pacote Python-Ordereddict" >> $log
	echo -e "╔══════════════════════════════════════╗"
	echo -e "║ Instalando pacote Python-Ordereddict ║"
	echo -e "╚══════════════════════════════════════╝"
	cd /tmp/install_sigfrente/ >> $log
	dpkg -i $pyordereddict >> $log
	sleep 3

	# Instalado Pacote SigUtils
	echo -e "\n\nInstalando pacote sigutils-$sigutils" >> $log
	echo -e "╔══════════════════════════════════╗"
	echo -e "║ Instalando pacote sigutils-2.4.6 ║"
	echo -e "╚══════════════════════════════════╝"
	cd /tmp/install_sigfrente/ >> $log
	tar xfvz $sigutils >> $log
	cd /tmp/install_sigfrente/sigutils-2.4.6/ >> $log
	python setup.py install
	sleep 3


	# Instalado Pacote Python-NF-e
	echo -e "\n\nInstalando pacote pythonfe-$pythonnfe" >> $log
	echo -e "╔═════════════════════════════════════╗"
	echo -e "║ Instalando pacote pythonnfe 4.51.13 ║"
	echo -e "╚═════════════════════════════════════╝"
	cd /tmp/install_sigfrente/
	tar xfvz $pythonnfe >> $log
	cd /tmp/install_sigfrente/nfe-4.51.13/ >> $log
	python setup.py install >> $log
	sleep 3


	# Instalado Pacote SigPDV3.1-NFC-e
	echo -e "\n\nInstalando pacote sigpdvnfce-$sigfrente" >> $log
	echo -e "╔═══════════════════════════════════════╗"
	echo -e "║ Instalando pacote Sig Frente 3.1.21L3 ║"
	echo -e "╚═══════════════════════════════════════╝"
	cd /tmp/install_sigfrente/ >> $log
	dpkg -i $sigfrente >> $log
	sleep 3

	# Bloqueando Atualização dos Pacotes Necessários para o SigFrente
	apt-mark hold python-sqlalchemy >> $log
	apt-mark hold python-sqlalchemy-doc >> $log
	apt-mark hold python-sqlalchemy-ext >> $log
	apt-mark hold python-elixir >> $log
	apt-mark hold python-serial >> $log

	# Configurações Postgres

	echo -e "\n\nConfiguração do postgres alterado para utilização do SigFrente" >> $log
	echo -e "╔════════════════════════════════════════════════════════════════╗"
	echo -e "║ Configuração do postgres alterado para utilização do SigFrente ║"
	echo -e "╚════════════════════════════════════════════════════════════════╝"
	cd /etc/postgresql/$vpostgresql/main/ >> $log
	sed -i "s/#listen_addresses = 'localhost'/listen_addresses = '*'/g" postgresql.conf
	sed -i "s/127.0.0.1\/32/0.0.0.0\/0/g" pg_hba.conf
	sed -i "s/md5/trust/g" pg_hba.conf
	sed -i "s/peer/trust/g" pg_hba.conf
	/etc/init.d/postgresql reload
	/etc/init.d/postgresql restart
	createdb -U postgres -E utf8 sigpdv -h localhost

#    psql -c "ALTER USER postgres WITH PASSWORD ''" -d template1

	# Criando Link Simbólico da SigPdv
	ln -s /usr/local/sigpdv /usr/local/lib/python2.7/dist-packages/sigpdv >> $log

	# Criando Atalho Desktop
	cp /usr/share/applications/SigPDV.desktop /home/caixa/Área\ de\ Trabalho/ && chmod 777 /home/caixa/Área\ de\ Trabalho/SigPDV.desktop >> $log
	cp /usr/share/applications/PdvConfig.desktop /home/caixa/Área\ de\ Trabalho/ && chmod 777 /home/caixa/Área\ de\ Trabalho/PdvConfig.desktop >> $log
	cp /tmp/install_sigfrente/sig.jpg /usr/share/xfce4/backdrops/ && chmod 777 /usr/share/xfce4/backdrops/sig.jpg >> $log
	cp /tmp/install_sigfrente/menu-sig.png /usr/share/pixmaps/ && chmod 777 /usr/share/pixmaps/menu-sig.png >> $log
	cp /tmp/install_sigfrente/atua /usr/local/bin && chmod 777 /usr/local/bin/atua >> $log

	# Criando Arquivos Necessários
	cd /var/cache >> $log
	mkdir ChavesCliSiTef >> $log
	chmod 1777 ChavesCliSiTef/ >> $log
	cd / >> $log
	mkdir recebe >> $log
	chmod 777 recebe/ >> $log
	echo /usr/local/sigpdv/sigtef/lib_x86_64/ >> /etc/ld.so.conf
	ldconfig >> $log
	echo UseDNS no >> /etc/ssh/sshd_config

	# Criando Arquivo de Configuração para "TMP"
	cd /etc/tmpfiles.d/ >> $log
	touch clisitef.conf >> $log
	echo "L /tmp/ChavesCliSiTef 1777 - - - /var/cache/ChavesCliSiTef" >> clisitef.conf


	# Alterando o Network-Time do Sistema
	timedatectl set-local-rtc 0 >> $log
	timedatectl set-timezone America/Sao_Paulo >>$log
	timedatectl set-ntp true >> $log

	# Adicionando usuário caixa ao grupo dialout
	adduser caixa dialout >> $log
	sudo usermod -aG dialout caixa


	echo "                             ╔══════════════════════╗"
	echo "                             ║ INSTALAÇÃO CONCLUIDA ║"
	echo "                             ╚══════════════════════╝"
	exit

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

#apt-mark hold python-sqlalchemy
#apt-mark hold python-sqlalchemy-doc
#apt-mark hold python-sqlalchemy-ext
#apt-mark hold python-elixir
#apt-mark hold python-serial

#apt-mark unhold python-sqlalchemy
#apt-mark unhold python-sqlalchemy-doc
#apt-mark unhold python-sqlalchemy-ext
#apt-mark unhold python-elixir
#apt-mark unhold python-serial
