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

user=`whoami`

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

    echo "|----------------------------|"
    echo "|  O que voce deseja fazer?  |"
    echo "|                            |"
    echo "|  (1) - Backup do Caixa.    |"
    echo "|  (2) - Restaurar Caixa.    |"
    echo "|----------------------------|"
    read bkprest

    if [[ $bkprest == 1 ]]; then

        echo "Iniciando processo de backup do caixa."
        sleep 3

        echo "Verificando numero do caixa!!"
        sleep 3
        cat /usr/local/sigpdv/pdvconfig.cfg | grep caixa

        echo "Digite o numero do caixa: "
        read cx

        cat -n /usr/local/sigpdv/pdvconfig.cfg | grep -n ^ | grep ^8: | cut -d: -f2
        cat -n /usr/local/sigpdv/pdvconfig.cfg | grep -n ^ | grep ^9: | cut -d: -f2
        cat -n /usr/local/sigpdv/pdvconfig.cfg | grep -n ^ | grep ^10: | cut -d: -f2
        cat -n /usr/local/sigpdv/pdvconfig.cfg | grep -n ^ | grep ^11: | cut -d: -f2
        cat -n /usr/local/sigpdv/pdvconfig.cfg | grep -n ^ | grep ^12: | cut -d: -f2
        cat -n /usr/local/sigpdv/pdvconfig.cfg | grep -n ^ | grep ^13: | cut -d: -f2
        cat -n /usr/local/sigpdv/pdvconfig.cfg | grep -n ^ | grep ^14: | cut -d: -f2
        cat -n /usr/local/sigpdv/pdvconfig.cfg | grep -n ^ | grep ^15: | cut -d: -f2
        cat -n /usr/local/sigpdv/pdvconfig.cfg | grep -n ^ | grep ^16: | cut -d: -f2
        cat -n /usr/local/sigpdv/pdvconfig.cfg | grep -n ^ | grep ^17: | cut -d: -f2
        cat -n /usr/local/sigpdv/pdvconfig.cfg | grep -n ^ | grep ^18: | cut -d: -f2
        cat -n /usr/local/sigpdv/pdvconfig.cfg | grep -n ^ | grep ^19: | cut -d: -f2

        psql -U postgres -l

        echo "Qual o nome do banco para backup?"
        read bpdv

        sleep 3

        echo " "
        echo "Fazendo backup da pasta sigpdv:"
        echo " "
        sleep 3

        mkdir /tmp/bkpcx$cx/
        chmod 777 /tmp/bkpcx$cx/
        cd /usr/local/
        tar cfvz sigpdv_$cx.tar.gz sigpdv
        cp sigpdv_$cx.tar.gz /tmp/bkpcx$cx

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

        echo " "
        echo "Criando dump. . ."
        echo " "
        sleep 3

        pg_dump --host localhost --port 5432 --username "postgres" --role "postgres" --format custom --blobs --verbose --file /tmp/bkpcx$cx/sigpdv_$cx.dump $bpdv

        echo " "
        echo "Arquivos criados na pasta de backup:"
        echo " "
        ls -lha /tmp/bkpcx$cx/
        sleep 3

        echo " "
        echo "Salvando pasta bkpcx$cx e seu conteúdo na pasta /recebe/"
        echo " "
        cp -r /tmp/bkpcx$cx/ /recebe/
        sleep 3

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

    elif [[ $bkprest == 2 ]]; then

        echo "Iniciando processo de restauração do caixa."
        sleep 3

        echo "Digite o numero do caixa: "
        read cx
        sleep 2

        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

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

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

    else

        echo "        Opção incorreta!"
        sleep 3
        echo " "
        echo " "
        echo "          ----------"
        echo "          |  ERRO  |"
        echo "          ----------"
        echo " "
        exit

    fi

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