#!/bin/bash
###################################### SIGERP UBUNTU18 #############################################
##By: RAFAEL RIBEIRO################################################################################
##Modificado por: Christian Tosta em 14/12/2016 15:35 BRST.#########################################
##Revision: RAFAEL BORGES E CAIO SANTOS#############################################################

user=`whoami`

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

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

	log='/tmp/install_sigerp/install_log_sigerp.log'

	wget download.sigsolucoes.net.br/produtos/releases/sigerp/7.0/update >> $log
	wget download.sigsolucoes.net.br/produtos/releases/logo/sig.jpg >> $log
	wget download.sigsolucoes.net.br/produtos/releases/logo/menu-sig.png >> $log
	

	openerp=`cat /tmp/install_sigerp/update | grep -i openerp | cut -d"=" -f2`
	pythonnfe=`cat /tmp/install_sigerp/update | grep -i nfe | cut -d"=" -f2`
	pythonsped=`cat /tmp/install_sigerp/update | grep -i sped | cut -d"=" -f2`
	sigutils=`cat /tmp/install_sigerp/update | grep -i sigutils | cut -d"=" -f2`
	sigerp=`cat /tmp/install_sigerp/update | grep -i sigerp | cut -d"=" -f2`
	geraldo='geraldo_0.4.10-stable-sig.tar.gz'
	pyxmlsec='python-pyxmlsec_0.3.1-1openhouse2_amd64.deb'
	xlutils='python-xlutils_1.5.1-1_all.deb'
	vpostgresql='10'

	# Baixando pacotes
	wget download.sigsolucoes.net.br/produtos/releases/openerp/7.0/$openerp/openerp-7.0-$openerp.tar.xz >> $log
	wget download.sigsolucoes.net.br/produtos/releases/sigutils/${sigutils%\.*}/sigutils-$sigutils.tar.gz >> $log
	wget download.sigsolucoes.net.br/produtos/releases/python-sped/${pythonsped%\.*}/sped-$pythonsped.tar.gz >> $log
	wget download.sigsolucoes.net.br/produtos/releases/python-nfe/${pythonnfe%\.*}/nfe-$pythonnfe.tar.gz >> $log
	wget download.sigsolucoes.net.br/produtos/releases/sigerp/7.0/$sigerp/sigaddons-7.0.$sigerp.tar.gz >> $log
	wget download.sigsolucoes.net.br/produtos/releases/geraldo/0.4.10/$geraldo >> $log
	wget download.sigsolucoes.net.br/apt/pool/bionic/main/amd64/p/pyxmlsec/$pyxmlsec >> $log
	wget download.sigsolucoes.net.br/apt//pool/bionic/main/amd64/p/python-xlutils/$xlutils >> $log
	wget download.sigsolucoes.net.br/produtos/releases/sigerp/openerp-server.conf >> $log
	wget download.sigsolucoes.net.br/produtos/releases/sigerp/openerp-server >> $log
	chmod -R 777 /tmp/install_sigerp

	if [ -e openerp-7.0-$openerp.tar.xz ] && [ -e sigutils-$sigutils.tar.gz ] &&
	[ -e sped-$pythonsped.tar.gz ] && [ -e nfe-$pythonnfe.tar.gz ] &&
	[ -e sigaddons-7.0.$sigerp.tar.gz ]

	then
	echo "Download efetuado com sucesso!"
	echo "Download efetuado com sucesso!" >> $log
	else
	echo "Não foi possível efetuar o download..."
	echo "Não foi possível efetuar o download..." >> $log
	echo "Verifique se os links de downloads estão corretos."
	echo "Verifique se os links de downloads estão corretos." >> $log
	exit
	fi
	
	# Instalando Pacotes Necessários
	echo 'Obtendo pacotes Necessários'
	echo 'Obtendo pacotes Necessários...' >> $log
	apt-get update -y
	apt-get install vim -y
	apt-get install openssh-server -y
	apt-get install terminator -y
	apt-get install openvpn -y 

	# Instalar dependencias dos pacote openerp-server
	echo 'Obtendo pacotes para instalação SigERP...' >> $log
	clear
	apt-get install python-apt-get -y
	apt-get install htop -y
	apt-get install python-lxml -y
	apt-get install python-mako -y
	apt-get install python-dateutil -y
	apt-get install python-psycopg2 -y
	apt-get install python-jwt -y
	apt-get install python-dnspython -y
	apt-get install python-numpy -y
	apt-get install python-pychart -y
	apt-get install python-pydot -y
	apt-get install python-tz -y
	apt-get install python-reportlab -y
	apt-get install python-yaml -y
	apt-get install python-vobject -y
	apt-get install python-dev -y
	apt-get install libmysqlclient-dev -y
	apt-get install postgresql -y
	apt-get install pgadmin3 -y
	apt-get install python-setuptools -y
	apt-get install python-mysqldb -y
	apt-get install python-beautifulsoup -y
	apt-get install python-m2crypto -y
	apt-get install python-libxml2 -y
	apt-get install python-feedparser -y
	apt-get install python-gdata -y
	apt-get install python-openid -y
	apt-get install python-webdav -y
	apt-get install python-vatnumber -y
	apt-get install python-werkzeug -y
	apt-get install python-xlwt -y
	apt-get install python-xlrd -y
	apt-get install xltils -y
	apt-get install python-zsi -y
	apt-get install python-ldap -y
	apt-get install python-babel -y
	apt-get install python-mock -y
	apt-get install python-jinja2 -y
	apt-get install python-docutils -y
	apt-get install python-gevent -y
	apt-get install xmlsec1 -y
	apt-get install libxmlsec1-dev -y
	apt-get install python-unittest2 -y
	apt-get install python-simplejson -y
	apt-get install python-psutil -y
	apt-get install python-cups -y
	apt-get install python-requests -y
	apt-get install python-qrcode -y
	apt-get install python-zeep -y
	apt-get install samba* -y
	apt-get install python3-smbc -y
	apt-get install -f -y
	
	easy_install -f http://www.pythonware.com/products/pil/ Imaging

	# Instalado Pacote PyXmlSec
	clear
	echo -e "\n\nInstalando pacote PyXmlSec" >> $log
	echo -e "Instalando pacote PyXmlSec" 
	echo -e "cd /tmp/install_sigerp/" >> $log
	cd /tmp/install_sigerp/ >> $log
	dpkg -i $pyxmlsec >> $log

	# Instalado Pacote XlUtils
	clear
	echo -e "\n\nInstalando pacote XlUtils" >> $log
	echo -e "Instalando pacote XlUtils"
	cd /tmp/install_sigerp/ >> $log
	dpkg -i $xlutils >> $log

	# Instalando Pacote Geraldo-Stable
	clear
	echo -e "\n\nInstalando pacote Geraldo-Stable" >> $log
	echo -e "Instalando pacote Geraldo-Stable"
	cd /tmp/install_sigerp/ >> $log
	tar xfvz $geraldo >> $log
	cd Geraldo-0.4.10-stable-sig >> $log
	python setup.py install >> $log

	# Instalado Pacote atualizado Openerp
	clear
	echo -e "\n\nInstalando pacote openerp-server" >> $log
	echo -e "Instalando pacote openerp-server"
	cd /tmp/install_sigerp/ >> $log
	tar xfv openerp-7.0-$openerp.tar.xz >> $log
	cd openerp-7.0 >> $log
	python setup.py install >> $log

	# Instalado Pacote atualizado Python-Nfe
	clear
	echo -e "\n\nInstalando pacotes da NF-e" >> $log
	echo -e "Instalando pacotes da NF-e"
	cd /tmp/install_sigerp/ >> $log
	tar xfvz nfe-$pythonnfe.tar.gz >> $log
	cd nfe-$pythonnfe >> $log
	python setup.py install >> $log

	# Instalado Pacote atualizado Python-Sped
	clear
	echo -e "\n\nInstalando pacotes do sped" >> $log
	echo -e "Instalando pacotes do sped" 
	cd /tmp/install_sigerp/ >> $log
	tar xfvz sped-$pythonsped.tar.gz >> $log
	cd sped-$pythonsped >> $log
	python setup.py install >> $log

	# Instalado Pacote atualizado SigUtils
	clear
	echo -e "\n\nInstalando SigUtils" >> $log
	echo -e "Instalando SigUtils"
	cd /tmp/install_sigerp/ >> $log
	tar xfvz sigutils-$sigutils.tar.gz >> $log
	cd sigutils-$sigutils
	python setup.py install >> $log
	
	# Configurações Postgresql
	clear
	echo -e "\n\nconfiguração do postgres alterado para utilização do Sigerp" >> $log
	echo -e "configuração do postgres alterado para utilização do Sigerp"
	cd /etc/postgresql/$vpostgresql/main/ >> $log
	sed -i "s/#listen_addresses = 'localhost'/listen_addresses = '*'/g" postgresql.conf
	sed -i "s/md5/trust/g" pg_hba.conf
	sed -i "s/peer/trust/g" pg_hba.conf
	/etc/init.d/postgresql restart
	echo -e "\n\nCriando usuario no banco de dados posgresql" >> $log
	echo -e "Criando usuario no banco de dados posgresql"
	createuser -U postgres --createdb --no-adduser -P openerp
	clear

	# Configurações Openerp
	clear
	echo -e "\n\nCriando usuario openerp" >> $log
	echo -e "Criando usuario openerp" 
	useradd openerp >> $log
	echo -e "\n\nConfigurando o openerp-server como serviço" >> $log
	echo -e "Configurando o openerp-server como serviço"
	echo -e "Copiando script de inicialização" >> $log
	cp /tmp/install_sigerp/openerp-server /etc/init.d/ >> $log
	chmod +x /etc/init.d/openerp-server >> $log
	echo -e "\n\nCopiando arquivo de configuração" >> $log
	cp /tmp/install_sigerp/openerp-server.conf /etc >> $log
	echo -e "\n\nMudando permissão dos arquivos de configuração para usuário openerp" >> $log
	chown openerp:adm /etc/openerp-server.conf >> $log
	echo -e "\n\nCriando arquivos de logs" >> $log
	mkdir /var/log/openerp/ >> $log
	touch /var/log/openerp/openerp-server.log >> $log
	chmod 777 /var/log/openerp >> $log
	chmod 777 /var/log/openerp/* >> $log
	chown openerp:openerp /var/log/openerp/* >> $log
	chown openerp:openerp /var/log/openerp >> $log
	update-rc.d openerp-server defaults >> $log

	# Instalado Pacote atualizado SigERP
	clear
	echo -e "Criando diretório para modulos sig" >> $log
	echo -e "Criando diretório para modulos sig"
	mkdir /usr/local/sigerp
	chown root:openerp /usr/local/sigerp
	chmod 775 /usr/local/sigerp
	cd /usr/local/sigerp
	cp /tmp/install_sigerp/sigaddons-7.0.$sigerp.tar.gz .
	tar xfvz sigaddons-7.0.$sigerp.tar.gz
	
	/etc/init.d/openerp-server start

	clear
	echo -e "\n\n--------------------------"
	echo -e "\n\n---Instalação concluida---"
	echo -e "\n\n--------------------------"
	exit
else
	clear
	echo "Favor logar com usuário root para executar o script"
fi
