#!/bin/bash
###############################################################################
# Script de desinstalacion de BulmaGes
# (C) Tomeu Borras, y Rene Merou
# Este documento es GPL
#------------------------------------------------------------------------------
###############################################################################
version="v0.9.1"


#Buscamos el parametro para saber si es texto o no
if [ "$1" = "--texto" ]; then
  texto="1"
  ptexto="--texto"
else
  texto="0"
  ptexto="--dialog"
fi

function mensaje() {
   if [ ! -e /usr/bin/dialog -a ! -e /usr/bin/xdialog -o $texto = 1 ]; then
      echo -e "$2" "$1"  "$3"
   elif [ -e /usr/bin/xdialog ]; then
      xdialog --title "Instalacion de BulmaGes $version" --backtitle "Asociacion Iglues" --msgbox "$1" 10 60
   else
      dialog --title "Instalacion de BulmaGes $version" --backtitle "Asociacion Iglues" --msgbox "$1" 10 60
   fi
}

function info() {
   if [ ! -e /usr/bin/dialog -a ! -e /usr/bin/xdialog -o $texto = 1 ]; then
      echo -e  "$2" "$1"  "$3"
   elif [ -e /usr/bin/xdialog ]; then
      xdialog --title "Instalacion de BulmaGes $version" --backtitle "Asociacion Iglues" --infobox "$1" 10 60
   else
      dialog --title "Instalacion de BulmaGes $version" --backtitle "Asociacion Iglues" --infobox "$1" 10 60
   fi
}

function tiempo {
  sleep 1.5
}

info "BulmaGes esta siendo desinstalado"
tiempo

info "Borrando archivos"
tiempo
rm -f /etc/bulmages/bulmages.conf
rm -f /etc/bulmages/bulmacont.conf
rm -f /etc/bulmages/bulmafact.conf
rm -f /etc/bulmages/bulmages-server.conf
rm -rf /usr/share/bulmages
rm -rf /usr/bin/bulmages
rm -rf /usr/bin/cargaemp
rm -rf /usr/bin/guardaemp
rm -rf /usr/bin/nuevaemp
rm -rf /usr/bin/bulmafact
rm -rf /usr/bin/bulmacont
rm -rf /usr/bin/bulmatpv
rm -rf /usr/lib/bulmages


info "Eliminando bases de datos del programa"
tiempo

LANG_PSQL=`su postgres -c "psql -l" | grep bulmages`
if [ "$LANG_PSQL" != "" ]; then
	info "Eliminando base de datos bulmages"
	su postgres -c "dropdb bulmages" > /dev/null 2> /dev/null
else
	info "La base de datos bulmages no existe en el sistema"
fi
tiempo

LANG_PSQL=`su postgres -c "psql -l" | grep bgplangcont`
if [ "$LANG_PSQL" != "" ]; then
	info "Eliminando base de datos bgplangcont"
	su postgres -c "dropdb bgplangcont"  > /dev/null 2> /dev/null
else
	info "La base de datos bgplangcont no existe en el sistema"
fi
tiempo

LANG_PSQL=`su postgres -c "psql -l" | grep bulmafact`
if [ "$LANG_PSQL" != "" ]; then
	info "Eliminando base de datos bulmafact"
	su postgres -c "dropdb bulmafact" > /dev/null 2> /dev/null
else
	info "La base de datos bulmafact no existe en el sistema"
fi;
tiempo

mensaje "BulmaGes ha sido completamente desinstalado.
Puede que queden bases de datos en su ordenador que debe borrar manualmente."
if [ $texto != 1 ]; then
   clear
fi
