#!/bin/bash
#author:Cristiano Siqueira
#descricao:Automatiza voltar o backup da pasta sigpdv e banco postgres.
#Revisao:

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

echo "Digite o numero do caixa supervisor: "
read cx

echo "Digite o nome do dump do supervisor: "
read bsup

cd /tmp/
chmod 777 bkpcx$cx/
chown caixa:caixa bkpcx$cx/

cd /tmp/bkpcx$cx/
cp sigpdv_$cx.tar.gz /usr/local/
sleep 3 

cd /usr/local/
rm -rf sigpdv/
tar xfvz sigpdv_$cx.tar.gz
sleep 3

cd /tmp/bkpcx$cx/
cp start-sigpdv /usr/bin/
cat /usr/bin/start-sigpdv
sleep 3

cd /tmp/bkpcx$cx/
tar xfvz nfe-4.51.11.tls12.tar.gz
cd /tmp/bkpcx$cx/nfe-4.51.11
sudo python setup.py install

cp /tmp/bkpcx$cx/registry /etc/wnbtlscli/
cp -r /tmp/bkpcx$cx/scanntech /usr/local/
cp -r /tmp/bkpcx$cx/backup /
cp /tmp/bkpcx$cx/openerp-server.conf /etc/
cp /tmp/bkpcx$cx/rc.local /etc/
cp /tmp/bkpcx$cx/crontab /etc/

echo " "
echo "Subindo base sigpdv do caixa $cx. . ."
echo " "
sleep 3

pg_restore --host localhost --port 5432 --username "postgres" --role "postgres" --dbname sigpdv --verbose /tmp/bkpcx$cx/sigpdv_$cx.dump

echo " "
echo "Subindo base $bsup do caixa $cx. . ."
echo " "
sleep 3

pg_restore --host localhost --port 5432 --username "openerp" --role "openerp" --dbname $bsup --verbose /tmp/bkpcx$cx/$bsup.dump

cd /tmp/
cp -r bkpcx$cx/ /recebe/

echo " "
echo " "
echo "          ---------"
echo "          |  FIM  |"
echo "          ---------"
echo " "
exit

else
    clear
    echo "Execute este aplicatvo como Root, por favor."
    exit
fi
